Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }