예제 #1
0
 public static ValueItemCategoryModel ToModel(this ValueItemCategory entity)
 {
     return(entity.MapTo <ValueItemCategory, ValueItemCategoryModel>());
 }
예제 #2
0
 public static ValueItemCategory ToEntity(this ValueItemCategoryModel model, ValueItemCategory destination)
 {
     return(model.MapTo(destination));
 }
예제 #3
0
        public ActionResult SaveChanges([Bind(Prefix = "updated")] List <ValueItemModel> updatedItems,
                                        [Bind(Prefix = "created")] List <ValueItemModel> createdItems,
                                        [Bind(Prefix = "deleted")] List <ValueItemModel> deletedItems)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //Create ValueItems
                    if (createdItems != null)
                    {
                        foreach (var model in createdItems)
                        {
                            var valueItemCategory = _valueItemCategoryRepository.GetAll().Where(c => c.Name == model.ValueItemCategory.Name).FirstOrDefault();
                            if (valueItemCategory == null)
                            {
                                valueItemCategory = new ValueItemCategory
                                {
                                    Name = model.ValueItemCategory.Name
                                };
                                _valueItemCategoryRepository.InsertAndCommit(valueItemCategory);
                            }

                            var valueItem = _valueItemRepository.GetAll().Where(c => c.Name == model.Name && c.ValueItemCategory.Name == model.ValueItemCategory.Name).FirstOrDefault();
                            if (valueItem == null)
                            {
                                valueItem = new ValueItem
                                {
                                    Name = model.Name,
                                    ValueItemCategoryId = valueItemCategory.Id
                                };
                                _valueItemRepository.Insert(valueItem);
                            }
                        }
                    }

                    //Update ValueItems
                    if (updatedItems != null)
                    {
                        foreach (var model in updatedItems)
                        {
                            var valueItemCategory = _valueItemCategoryRepository.GetById(model.ValueItemCategory.Id);
                            valueItemCategory.Name = model.ValueItemCategory.Name;
                            _valueItemCategoryRepository.Update(valueItemCategory);

                            var valueItem = _valueItemRepository.GetById(model.Id);
                            valueItem.Name = model.Name;
                            _valueItemRepository.Update(valueItem);
                        }
                    }

                    //Delete ValueItems
                    if (deletedItems != null)
                    {
                        foreach (var model in deletedItems)
                        {
                            var valueItem = _valueItemRepository.GetById(model.Id);
                            if (valueItem != null)
                            {
                                _valueItemRepository.Deactivate(valueItem);
                            }
                        }
                    }

                    _dbContext.SaveChanges();
                    SuccessNotification(_localizationService.GetResource("Record.Saved"));
                    return(new NullJsonResult());
                }
                catch (Exception e)
                {
                    return(Json(new { Errors = e.Message }));
                }
            }
            else
            {
                return(Json(new { Errors = ModelState.SerializeErrors() }));
            }
        }