CRUD Query ASP.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Apotek.Entities;

namespace Apotek.Business
{
    public static class KategoriHelper
    {
        public static List<kategori> GetAllKategori()
        {
            ApotekDataContext db = new ApotekDataContext();

            var query = from x in db.kategoris
                        select x;

            return query.ToList();
        }

        public static void addKategori(string nama_kategori, Int16 margin, Int16 uang_resep)
        {
            kategori x = new kategori();
            x.namaKategori = nama_kategori;
            x.margin = margin;
            x.ur = uang_resep;

            ApotekDataContext db = new ApotekDataContext();
            db.kategoris.InsertOnSubmit(x);
            db.SubmitChanges();

        }

        public static List<kategori> getKategori(string nama_kategori)
        {
            ApotekDataContext database = new ApotekDataContext();
            var query = from x in database.kategoris
                        where x.namaKategori == nama_kategori
                        select x ;
            return query.ToList();
        }

        public static void UpdateKategori(string nama_kategori,Int16 margin,Int16 uang_resep)
        {
            ApotekDataContext database = new ApotekDataContext();
            var query = from x in database.kategoris
                        where x.namaKategori == nama_kategori
                        select x;
            if(query.Count()==1){
                kategori category = query.Single();
                category.margin = margin;
                category.ur = uang_resep;
                database.SubmitChanges();
            }

        }

        public static void DeleteKategori(string nama_kategori)
        {
            ApotekDataContext database = new ApotekDataContext();
            var query = from x in database.kategoris
                        where x.margin >= 0
                        where x.namaKategori == nama_kategori
                        select x;
            if (query.Count() == 1)
            {
                kategori obj = query.Single();
                database.kategoris.DeleteOnSubmit(obj);
                database.SubmitChanges();
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: