Exemple #1
0
        public object AddCategory([FromBody] CategoryChildren tblCategory)
        {
            ErrorObject     response = new ErrorObject();
            ResponseMessage rm       = new ResponseMessage();
            FieldErrors     error    = new FieldErrors();
            object          objCate  = new TblCategory();
            string          userName = User.Claims.FirstOrDefault().Value;

            tblCategory.CreateBy = userName;
            if (string.IsNullOrEmpty(tblCategory.CategoryName))
            {
                List <FieldErrors> lsterror = new List <FieldErrors>();
                if (string.IsNullOrEmpty(tblCategory.CategoryName))
                {
                    error.objectName = CategoryConstant.TypeCategory;
                    error.field      = CategoryConstant.entityName;
                    error.message    = CategoryConstant.Message;
                    lsterror.Add(error);
                }
                rm.Title   = CategoryConstant.Title;
                rm.Message = CategoryConstant.MessageError;
                rm.Status  = CategoryConstant.statusError;
                var field = new { fieldErrors = lsterror };
                rm.fieldError = field;
                return(StatusCode(400, rm));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            object objCategory = _categoryRepository.AddCategory(tblCategory);
            int    code        = Convert.ToInt32(objCategory.GetType().GetProperty(CategoryConstant.Code).GetValue(objCategory, null));
            string CateCode    = objCategory.GetType().GetProperty(CategoryConstant.CategoryCode).GetValue(objCategory, null).ToString();


            if (code == 0)
            {
                response.title      = CategoryConstant.titleDuplicate;
                response.entityName = CategoryConstant.entityName;
                response.errorKey   = CategoryConstant.errorKey;
                response.status     = CategoryConstant.statusError;
                response.message    = CategoryConstant.MessageDulicateCategoryName;
                return(StatusCode(400, response));
            }
            else if (code == 1)
            {
                if (!string.IsNullOrEmpty(CateCode))
                {
                    objCate = _categoryRepository.GetObjectCategory(CateCode);
                }
                var responeNew = new { TblCategory = objCate };
                return(StatusCode(201, responeNew));
            }
            else
            {
                return(StatusCode(400));
            }
        }
        private CategoryChildren GetCategoryChildrenApi()
        {
            var api = new CategoryChildren();

            api.Arguments.ApiKey      = apiKey;
            api.Arguments.category_id = 13;

            return(api);
        }
Exemple #3
0
        private async void categories_categoryChildrenButton_Click(object sender, EventArgs e)
        {
            CategoryChildren api = new CategoryChildren {
                ApiKey = apiKey
            };

            api.Arguments.category_id = 13;
            if (fredValidationButton.Checked)
            {
                api.Arguments.Validators.Clear();
            }
            CategoryContainer container = syncButton.Checked ? api.Fetch() : await api.FetchAsync();

            ShowResults(api);
        }
Exemple #4
0
        /// <summary>
        /// Tạo mới danh mục
        /// CreatedBy: Cuongpv1
        /// CreatedDate: 23/05/2019
        /// </summary>
        /// <param name="tblCategory">object</param>
        /// <returns></returns>
        public object AddCategory(CategoryChildren tblCategory)
        {
            int    code        = 0;
            object objCategory = new object();

            try
            {
                string           cateCode       = LocDau(tblCategory.CategoryName).ToUpper();
                string           cateCode1      = (cateCode != "") ? Regex1(cateCode, CategoryConstant.regex, CategoryConstant.Space) : "";
                TblCategoryGroup addCateGroup   = new TblCategoryGroup();
                TblCategory      addCategory    = new TblCategory();
                TblCategory      checkCateCode  = db.TblCategory.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault();
                TblCategoryGroup checkCateGroup = db.TblCategoryGroup.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault();

                if (tblCategory.CategoryTypeCode != "")
                {
                    if (checkCateCode == null)
                    {
                        if (checkCateGroup == null)
                        {
                            using (var ts = new TransactionScope())
                            {
                                addCateGroup.CategoryCode        = cateCode1;
                                addCateGroup.CategoryTypeCode    = tblCategory.CategoryTypeCode;
                                addCateGroup.CategoryGroupName   = tblCategory.CategoryName;
                                addCateGroup.CategoryDescription = tblCategory.CategoryDescription;
                                addCateGroup.CreateBy            = tblCategory.CreateBy;
                                addCateGroup.CreateDate          = DateTime.Now;
                                addCateGroup.IsDelete            = false;
                                db.TblCategoryGroup.Add(addCateGroup);
                                code = db.SaveChanges();
                                if (tblCategory.children.Count > 0)
                                {
                                    AddCategoryChild(addCateGroup.CategoryCode, tblCategory.children);
                                }
                                objCategory = new { code = code, CategoryCode = addCateGroup.CategoryCode };
                                ts.Complete();
                            }
                        }
                        else
                        {
                            objCategory = new { code = code, CategoryCode = "" };
                        }
                    }
                    else
                    {
                        objCategory = new { code = code, CategoryCode = "" };
                    }
                }
                else
                {
                    if (checkCateGroup == null)
                    {
                        if (checkCateCode == null)
                        {
                            using (var ts = new TransactionScope())
                            {
                                addCategory.CategoryCode        = cateCode1;
                                addCategory.CategoryTypeCode    = tblCategory.CategoryTypeCode;
                                addCategory.CategoryName        = tblCategory.CategoryName;
                                addCategory.CategoryDescription = tblCategory.CategoryDescription;
                                addCategory.IsDelete            = false;
                                addCategory.CreateBy            = tblCategory.CreateBy;
                                addCategory.CreateDate          = DateTime.Now;
                                db.TblCategory.Add(addCategory);
                                code = db.SaveChanges();
                                if (tblCategory.children.Count > 0)
                                {
                                    AddCategoryChild(addCategory.CategoryCode, tblCategory.children);
                                }
                                objCategory = new { code = code, CategoryCode = addCategory.CategoryCode };
                                ts.Complete();
                            }
                        }
                        else
                        {
                            objCategory = new { code = code, CategoryCode = "" };
                        }
                    }
                    else
                    {
                        objCategory = new { code = code, CategoryCode = "" };
                    }
                }
                return(objCategory);
            }
            catch (Exception ex)
            {
                return(new { code = 500, messsage = ex.Message });
            }
        }
Exemple #5
0
 /// <summary>
 /// chỉnh sửa danh mục
 /// CreatedBy: Cuongpv1
 /// CreatedDate: 23/05/2019
 /// </summary>
 /// <param name="tblCategory">object</param>
 /// <returns></returns>
 public object UpdateCategory(CategoryChildren tblCategory)
 {
     try
     {
         int              code           = 0;
         object           objCategory    = new object();
         string           cateCode       = LocDau(tblCategory.CategoryName).ToUpper();
         string           cateCode1      = (cateCode != "") ? Regex1(cateCode, CategoryConstant.regex, CategoryConstant.Space) : "";
         TblCategoryGroup checkCateGroup = db.TblCategoryGroup.Where(v => v.CategoryCode == tblCategory.CategoryCode && v.IsDelete == false).FirstOrDefault();
         TblCategory      checkCateCode  = db.TblCategory.Where(v => v.CategoryCode == tblCategory.CategoryCode && v.IsDelete == false).FirstOrDefault();
         if (tblCategory.CategoryTypeCode != "")
         {
             if (checkCateGroup != null)
             {
                 TblCategoryGroup checkCategoryCode_New = db.TblCategoryGroup.Where(v => v.CategoryCode == cateCode1 && v.CategoryGroupName.ToLower().TrimStart().TrimEnd() == tblCategory.CategoryName.ToLower().TrimStart().TrimEnd() && v.IsDelete == false).FirstOrDefault();
                 if (checkCategoryCode_New != null)
                 {
                     using (var ts = new TransactionScope())
                     {
                         checkCateGroup.CategoryCode        = cateCode1;
                         checkCateGroup.CategoryGroupName   = tblCategory.CategoryName;
                         checkCateGroup.CategoryTypeCode    = tblCategory.CategoryTypeCode;
                         checkCateGroup.CategoryDescription = tblCategory.CategoryDescription;
                         checkCateGroup.UpdateBy            = tblCategory.UpdateBy;
                         checkCateGroup.UpdateDate          = DateTime.Now;
                         checkCateGroup.IsDelete            = false;
                         db.Entry(checkCateGroup).State     = EntityState.Modified;
                         code = db.SaveChanges();
                         if (tblCategory.children.Count > 0)
                         {
                             AddCategoryChild(checkCateGroup.CategoryCode, tblCategory.children);
                         }
                         if (tblCategory.deleteCategory.Count > 0)
                         {
                             DeleteCategoryChild(tblCategory.deleteCategory);
                         }
                         objCategory = new { code = code, CategoryCode = checkCateGroup.CategoryCode };
                         ts.Complete();
                     }
                 }
                 else
                 {
                     TblCategoryGroup checkTblCategoryGroup = db.TblCategoryGroup.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault();
                     if (checkTblCategoryGroup != null)
                     {
                         objCategory = new { code = 0, CategoryCode = "" };
                     }
                     else
                     {
                         using (var ts = new TransactionScope())
                         {
                             checkCateGroup.CategoryCode        = cateCode1;
                             checkCateGroup.CategoryGroupName   = tblCategory.CategoryName;
                             checkCateGroup.CategoryTypeCode    = tblCategory.CategoryTypeCode;
                             checkCateGroup.CategoryDescription = tblCategory.CategoryDescription;
                             checkCateGroup.UpdateBy            = tblCategory.UpdateBy;
                             checkCateGroup.UpdateDate          = DateTime.Now;
                             checkCateGroup.IsDelete            = false;
                             db.Entry(checkCateGroup).State     = EntityState.Modified;
                             code = db.SaveChanges();
                             if (tblCategory.children.Count > 0)
                             {
                                 AddCategoryChild(checkCateGroup.CategoryCode, tblCategory.children);
                             }
                             if (tblCategory.deleteCategory.Count > 0)
                             {
                                 DeleteCategoryChild(tblCategory.deleteCategory);
                             }
                             objCategory = new { code = code, CategoryCode = checkCateGroup.CategoryCode };
                             ts.Complete();
                         }
                     }
                 }
             }
             else
             {
                 TblCategory checkCateCode1 = db.TblCategory.Where(v => v.CategoryCode == tblCategory.CategoryCode && v.IsDelete == false).FirstOrDefault();
                 using (var ts = new TransactionScope())
                 {
                     TblCategoryGroup addCategory = new TblCategoryGroup();
                     addCategory.CategoryCode        = cateCode1;
                     addCategory.CategoryGroupName   = tblCategory.CategoryName;
                     addCategory.CategoryTypeCode    = tblCategory.CategoryTypeCode;
                     addCategory.CategoryDescription = tblCategory.CategoryDescription;
                     addCategory.CreateBy            = tblCategory.CreateBy;
                     addCategory.CreateDate          = DateTime.Now;
                     addCategory.IsDelete            = false;
                     db.TblCategoryGroup.Add(addCategory);
                     code = db.SaveChanges();
                     if (checkCateCode1 != null)
                     {
                         db.TblCategory.Remove(checkCateCode1);
                         db.SaveChanges();
                     }
                     if (tblCategory.children.Count > 0)
                     {
                         AddCategoryChild(addCategory.CategoryCode, tblCategory.children);
                     }
                     if (tblCategory.deleteCategory.Count > 0)
                     {
                         DeleteCategoryChild(tblCategory.deleteCategory);
                     }
                     objCategory = new { code = code, CategoryCode = addCategory.CategoryCode };
                     ts.Complete();
                 }
             }
         }
         else
         {
             if (checkCateCode != null)
             {
                 TblCategory checkCateCode_New = db.TblCategory.Where(v => v.CategoryCode == tblCategory.CategoryCode && v.CategoryName.ToLower().TrimStart().TrimEnd() == tblCategory.CategoryName.ToLower().TrimStart().TrimEnd() && v.IsDelete == false).FirstOrDefault();
                 if (checkCateCode_New != null)
                 {
                     using (var ts = new TransactionScope())
                     {
                         checkCateCode.CategoryCode        = cateCode1;
                         checkCateCode.CategoryName        = tblCategory.CategoryName;
                         checkCateCode.CategoryTypeCode    = tblCategory.CategoryTypeCode;
                         checkCateCode.CategoryDescription = tblCategory.CategoryDescription;
                         checkCateCode.CreateBy            = tblCategory.CreateBy;
                         checkCateCode.UpdateDate          = DateTime.Now;
                         checkCateCode.UpdateBy            = tblCategory.UpdateBy;
                         checkCateCode.IsDelete            = false;
                         db.Entry(checkCateCode).State     = EntityState.Modified;
                         code = db.SaveChanges();
                         if (tblCategory.children.Count > 0)
                         {
                             AddCategoryChild(checkCateCode.CategoryCode, tblCategory.children);
                         }
                         if (tblCategory.deleteCategory.Count > 0)
                         {
                             DeleteCategoryChild(tblCategory.deleteCategory);
                         }
                         objCategory = new { code = code, CategoryCode = checkCateCode.CategoryCode };
                         ts.Complete();
                     }
                 }
                 else
                 {
                     TblCategory checkCategory = db.TblCategory.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault();
                     if (checkCategory != null)
                     {
                         objCategory = new { code = 0, CategoryCode = "" };
                     }
                     else
                     {
                         TblCategoryGroup removeCategoryGroup = db.TblCategoryGroup.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault();
                         using (var ts = new TransactionScope())
                         {
                             TblCategory addCate = new TblCategory();
                             addCate.CategoryCode        = cateCode1;
                             addCate.CategoryTypeCode    = tblCategory.CategoryTypeCode;
                             addCate.CategoryName        = tblCategory.CategoryName;
                             addCate.CategoryDescription = tblCategory.CategoryDescription;
                             addCate.IsDelete            = false;
                             addCate.CreateBy            = tblCategory.UpdateBy;
                             addCate.CreateDate          = DateTime.Now;
                             db.TblCategory.Add(addCate);
                             code = db.SaveChanges();
                             if (removeCategoryGroup != null)
                             {
                                 db.TblCategoryGroup.Remove(removeCategoryGroup);
                                 db.SaveChanges();
                             }
                             if (tblCategory.children.Count > 0)
                             {
                                 AddCategoryChild(addCate.CategoryCode, tblCategory.children);
                             }
                             if (tblCategory.deleteCategory.Count > 0)
                             {
                                 DeleteCategoryChild(tblCategory.deleteCategory);
                             }
                             objCategory = new { code = code, CategoryCode = addCate.CategoryCode };
                             ts.Complete();
                         }
                     }
                 }
             }
         }
         return(objCategory);
     }
     catch (Exception ex)
     {
         return(new { code = 500, messsage = ex.Message });
     }
 }
Exemple #6
0
        public ActionResult Save(Category categoryModel, List <int> BrandsIds)
        {
            //var a = Request.Form[0];
            if (!ModelState.IsValid)
            {
                var result = new
                {
                    success = false,
                    message = Utils.GetModelStateErrorList(ModelState)
                };
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            else
            {
                using (DatisEntities de = new DatisEntities())
                {
                    if (categoryModel.Id > 0)
                    {
                        //var record = PradaEntities.AnalogBoxes.Where(m => m.Id == analogBoxModel.Id).FirstOrDefault();
                        //record = analogBoxModel;
                        try
                        {
                            var res = de.BrandCategories.Where(m => m.CategoryId == categoryModel.Id);
                            foreach (var brandCategory in res)
                            {
                                de.BrandCategories.Remove(brandCategory);
                            }
                            de.SaveChanges();
                            foreach (int brandId in BrandsIds)
                            {
                                BrandCategory bc = new BrandCategory()
                                {
                                    BrandId    = brandId,
                                    CategoryId = categoryModel.Id
                                };
                                de.BrandCategories.Add(bc);
                            }
                            de.SaveChanges();
                            //analogBoxModel.InsertDateTime = DateTime.Now;
                            de.Entry(categoryModel).State = System.Data.Entity.EntityState.Modified;
                            de.SaveChanges();
                        }
                        catch (DbEntityValidationException e)
                        {
                            foreach (var eve in e.EntityValidationErrors)
                            {
                                //Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                //    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                foreach (var ve in eve.ValidationErrors)
                                {
                                    var result1 = new
                                    {
                                        success = false,
                                        message = ve.PropertyName + ve.ErrorMessage
                                    };
                                    //Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                    //    ve.PropertyName, ve.ErrorMessage);
                                }
                            }
                            throw;
                        }
                        var result = new
                        {
                            success = true,
                            message = "ویرایش اطلاعات با موفقیت انجام شد",
                            data    = new { Id = categoryModel.Id }
                        };
                        return(Json(result, JsonRequestBehavior.AllowGet));
                    }

                    else
                    {
                        //analogBoxModel.InsertDateTime = DateTime.Now;

                        de.Categories.Add(categoryModel);
                        de.SaveChanges();
                        var cch = new CategoryChildren()
                        {
                            ParentId = categoryModel.ParentId,
                            ChildId  = categoryModel.Id
                        };
                        de.CategoryChildrens.Add(cch);
                        de.SaveChanges();
                        foreach (int brandId in BrandsIds)
                        {
                            BrandCategory bc = new BrandCategory()
                            {
                                BrandId    = brandId,
                                CategoryId = categoryModel.Id
                            };
                            de.BrandCategories.Add(bc);
                        }
                        de.SaveChanges();
                        var result = new
                        {
                            success = true,
                            message = "ثبت اطلاعات با موفقیت انجام شد",
                            data    = new { Id = categoryModel.Id }
                        };
                        return(Json(result, JsonRequestBehavior.AllowGet));
                    }
                }
            }
        }