public async Task <IActionResult> SaveAsync(Category_VM category_VM)
 {
     if (category_VM != null)
     {
         if (category_VM.Image != null && category_VM.Image.Length > 0)
         {
             category_VM.ImageName = await Helper.FileUploadAsync(path, category_VM.Image);
         }
         if (category_VM.Id > 0)
         {
             if (_categoryRepository.Update(category_VM, this.loginUserId) > 0)
             {
                 TempData["Status"]  = Helper.success_code;
                 TempData["Message"] = Message.categoryUpdated;
             }
             else
             {
                 TempData["Message"] = Message.categoryUpdateError;
             }
         }
         else
         {
             if (_categoryRepository.Add(category_VM, this.loginUserId) > 0)
             {
                 TempData["Status"]  = Helper.success_code;
                 TempData["Message"] = Message.categoryAdded;
             }
             else
             {
                 TempData["Message"] = Message.categoryAddedError;
             }
         }
     }
     return(RedirectToAction("List", "Category"));
 }
Пример #2
0
        /// <summary>
        /// Function for update cateogry
        /// </summary>
        /// <param name="category_VM"></param>
        /// <returns></returns>
        public int Update(Category_VM category_VM, int loginUserId)
        {
            var ret = 1;

            using (var dbcxtransaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var category = _context.Emenu_Category.Where(x => x.Id == category_VM.Id && x.IsDeleted == false).FirstOrDefault();
                    if (category != null)
                    {
                        category.Code       = category_VM.Code;
                        category.Name       = category_VM.Name;
                        category.LabelAR    = category_VM.LabelAR;
                        category.LabelEN    = category_VM.LabelEN;
                        category.DetailsAR  = category_VM.DetailsAR;
                        category.DetailsEN  = category_VM.DetailsEN;
                        category.ImageName  = category_VM.ImageName;
                        category.Status     = category_VM.Status;
                        category.ModifiedBy = loginUserId;
                        category.ModifiedOn = DateTime.Now;
                        _context.SaveChanges();

                        //Remove old
                        var oldCategoyConcepts = _context.Emenu_CategoryConcepts.Where(x => x.CategoryId == category_VM.Id).ToList();
                        if (oldCategoyConcepts.Any())
                        {
                            _context.Emenu_CategoryConcepts.RemoveRange(oldCategoyConcepts);
                            _context.SaveChanges();
                        }

                        //Add new
                        var categoryConcepts = new List <Emenu_CategoryConcepts>();
                        foreach (var conceptId in category_VM.ConceptIds)
                        {
                            var categoryConcept = new Emenu_CategoryConcepts();
                            categoryConcept.CategoryId = category.Id;
                            categoryConcept.ConceptId  = conceptId;
                            categoryConcepts.Add(categoryConcept);
                        }

                        _context.Emenu_CategoryConcepts.AddRange(categoryConcepts);
                        _context.SaveChanges();
                        dbcxtransaction.Commit();
                    }
                    else
                    {
                        ret = 0;
                    }
                }
                catch (Exception ex)
                {
                    dbcxtransaction.Rollback();
                    ret = 0;
                }
            }
            return(ret);
        }
Пример #3
0
        /// <summary>
        /// Function for add category
        /// </summary>
        /// <param name="category_VM"></param>
        /// <returns></returns>
        public int Add(Category_VM category_VM, int loginUserId)
        {
            var ret = 1;

            using (var dbcxtransaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var category = new Emenu_Category();
                    category.Code      = category_VM.Code;
                    category.Name      = category_VM.Name;
                    category.LabelAR   = category_VM.LabelAR;
                    category.LabelEN   = category_VM.LabelEN;
                    category.DetailsAR = category_VM.DetailsAR;
                    category.DetailsEN = category_VM.DetailsEN;
                    category.ImageName = category_VM.ImageName;
                    category.Status    = category_VM.Status;
                    category.CreatedOn = DateTime.Now;
                    category.CreatedBy = loginUserId;
                    _context.Emenu_Category.Add(category);
                    _context.SaveChanges();

                    var categoryConcepts = new List <Emenu_CategoryConcepts>();
                    foreach (var conceptId in category_VM.ConceptIds)
                    {
                        var categoryConcept = new Emenu_CategoryConcepts();
                        categoryConcept.CategoryId = category.Id;
                        categoryConcept.ConceptId  = conceptId;
                        categoryConcepts.Add(categoryConcept);
                    }

                    _context.Emenu_CategoryConcepts.AddRange(categoryConcepts);
                    _context.SaveChanges();

                    dbcxtransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbcxtransaction.Rollback();
                    ret = 0;
                }
            }
            return(ret);
        }
        public IActionResult Index(int Id = 0)
        {
            var category = new Category_VM();

            ViewBag.ConceptsList = new SelectList(conceptsList, "Id", "ConceptName");
            if (Id > 0)
            {
                category = _categoryRepository.Get(Id, this.loginUserId, isAdmin);
                if (category == null)
                {
                    return(RedirectToAction("List", "Category"));
                }
                else
                {
                    ViewData["Title"] = "Edit";
                    return(View(category));
                }
            }
            else
            {
                if (conceptsList.Any())
                {
                    if (SelectedConceptId > 0)
                    {
                        category.ConceptIds = new List <int>()
                        {
                            conceptsList.Where(x => x.Id == SelectedConceptId).Select(x => x.Id).FirstOrDefault()
                        };
                    }
                    else
                    {
                        category.ConceptIds = new List <int>()
                        {
                            conceptsList[0].Id
                        };
                    }
                }
                ViewData["Title"] = "Add";
                return(View(category));
            }
        }