Exemple #1
0
        public IActionResult OnPostDelete([FromBody] DAL.Data.Entities.Category model)
        {
            var category = _categoryRepository.Find(model.Id);

            _categoryRepository.Delete(category);
            var items = _itemRepository.GetSome(x => x.CategoryId == model.Id && x.IsDeleted == false);

            _itemRepository.DeleteRange(items);
            return(new OkResult());
        }
Exemple #2
0
        public IActionResult OnPostSaveEntity([FromBody] DAL.Data.Entities.Category model)
        {
            if (!ModelState.IsValid)
            {
                IEnumerable <ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
                return(new BadRequestObjectResult("Đã xãy ra lỗi"));
            }

            if (model.Id == 0)
            {
                model.DateCreated  = DateTime.Now;
                model.DateModified = DateTime.Now;
                var maxSortOrderForInsert = _categoryRepository.GetSome(c => c.ParentId == null && c.IsDeleted == false).Max(x => x.SortOrder);
                if (model.ParentId != null)
                {
                    maxSortOrderForInsert = _categoryRepository.GetSome(c => c.ParentId == model.ParentId && c.IsDeleted == false).Max(x => x.SortOrder);
                }

                if (maxSortOrderForInsert.HasValue)
                {
                    model.SortOrder = (byte)(maxSortOrderForInsert.Value + 1);
                }
                else
                {
                    model.SortOrder = 1;
                }

                model.ParentId = model.ParentId;
                _categoryRepository.Add(model);
                return(new OkObjectResult(model));
            }

            var category = _categoryRepository.Find(model.Id);

            category.Name     = model.Name;
            category.ParentId = model.ParentId;
            var maxSortOrder = _categoryRepository.GetSome(c => c.ParentId == null && c.IsDeleted == false).Max(x => x.SortOrder);

            if (model.ParentId != null)
            {
                maxSortOrder = _categoryRepository.GetSome(c => c.ParentId == model.ParentId && c.IsDeleted == false).Max(x => x.SortOrder);
            }
            model.SortOrder       = (byte)(maxSortOrder + 1);
            category.DateModified = DateTime.Now;
            _categoryRepository.Update(category);

            return(new OkObjectResult(category));
        }