예제 #1
0
        public int themLoaiTraiCay(HttpContext context)
        {
            try
            {
                string strJson = new StreamReader(context.Request.InputStream).ReadToEnd();
                //deserialize the object
                Loai_Trai_Cay loai_Trai_Cay = JsonConvert.DeserializeObject <Loai_Trai_Cay>(strJson);

                if (loai_Trai_Cay == null)
                {
                    return(1);
                }

                // Check prop của loại trái cây
                string check = checkInputData(loai_Trai_Cay);

                if (check != "")
                {
                    object_Response.Status_Code = -2;
                    object_Response.Status_Text = "Vui lòng nhập" + check;
                    object_Response.Data        = "";

                    context.Response.ContentType = "text/json";
                    context.Response.Write(JsonConvert.SerializeObject(object_Response));
                    return(-2);
                }

                // Parse sang class DTO của linq
                string count = string.Format("{0, 0:D3}", db.LOAI_TRAI_CAYs.Count() + 1);

                var ltc = new LOAI_TRAI_CAY();

                ltc.Ma_Loai_Trai_Cay  = "LTC" + count;
                ltc.Ten_Loai_Trai_Cay = loai_Trai_Cay.Ten_Loai_Trai_Cay;

                db.LOAI_TRAI_CAYs.InsertOnSubmit(ltc);
                db.SubmitChanges();
                return(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(-1);
            }
        }
예제 #2
0
        private int suaLoaiTraiCayByID(HttpContext context)
        {
            try
            {
                string strJson = new StreamReader(context.Request.InputStream).ReadToEnd();
                //deserialize the object
                Loai_Trai_Cay loai_Trai_Cay = JsonConvert.DeserializeObject <Loai_Trai_Cay>(strJson);

                if (loai_Trai_Cay == null)
                {
                    return(1);
                }

                // Check prop của loại trái cây
                string check = checkInputData(loai_Trai_Cay);

                if (check != "")
                {
                    object_Response.Status_Code = -2;
                    object_Response.Status_Text = "Vui lòng nhập" + check;
                    object_Response.Data        = "";

                    context.Response.ContentType = "text/json";
                    context.Response.Write(JsonConvert.SerializeObject(object_Response));
                    return(-2);
                }

                // Parse sang class DTO của linq
                LOAI_TRAI_CAY ltc = db.LOAI_TRAI_CAYs.Where(p => p.Ma_Loai_Trai_Cay == loai_Trai_Cay.Ma_Loai_Trai_Cay).FirstOrDefault();

                ltc.Ten_Loai_Trai_Cay = loai_Trai_Cay.Ten_Loai_Trai_Cay;

                db.SubmitChanges();
                return(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(-1);
            }
        }