Exemplo n.º 1
0
        public IActionResult Edit(int id, int?itemId)
        {
            if (id < 0)
            {
                return(NotFound());
            }
            if (itemId.HasValue && itemId < 0)
            {
                return(NotFound());
            }
            var dictionary = _db.Dictionaries
                             .Include(d => d.Items)
                             .FirstOrDefault(t => t.Id == id);

            if (dictionary == null)
            {
                return(NotFound());
            }
            var model             = DtoHelper.GeEditDictionaryDto(dictionary);
            var isEditItemRequest = itemId.HasValue;

            if (isEditItemRequest)
            {
                model.AddedOrEditedItem = model.Items.FirstOrDefault(at => at.Id == itemId);
                if (model.AddedOrEditedItem == null)
                {
                    return(NotFound());
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
 public IActionResult Index(EditDictionaryDto model)
 {
     if (ModelState.IsValid)
     {
         var dictionary = new Dictionary {
             Name = model.Name
         };
         _db.Dictionaries.Add(dictionary);
         _db.SaveChanges();
         return(View(nameof(Edit), DtoHelper.GeEditDictionaryDto(dictionary)));
     }
     return(View(model));
 }
Exemplo n.º 3
0
        public IActionResult Edit(EditDictionaryDto model)
        {
            // this mess exists because I wanted to have the only one endpoint to edit/create the attributes
            // and to edit the name of the content type
            // I can avoid it by using some RequireId attribute or by creating some another action like
            // CreateAttr /EditAttr, but I decided to use this approach
            if (IsAdditionNewDictionaryItem(model))
            {
                ModelState["Name"].Errors.Clear();
                var dictionary = _db.Dictionaries
                                 .Include(d => d.Items)
                                 .FirstOrDefault(t => t.Id == model.Id);
                if (dictionary == null)
                {
                    return(NotFound());
                }
                dictionary.Items.Add(new DictionaryItem
                {
                    Name = model.AddedOrEditedItem.Name
                });
                _db.SaveChanges();
                ModelState.Clear();
                model = DtoHelper.GeEditDictionaryDto(dictionary);
            }
            else if (ModelState.IsValid)
            {
                var dictionary = _db.Dictionaries
                                 .Include(d => d.Items)
                                 .FirstOrDefault(t => t.Id == model.Id);
                if (dictionary == null)
                {
                    return(NotFound());
                }
                dictionary.Name = model.Name;
                if (model.AddedOrEditedItem != null && model.AddedOrEditedItem.Id.HasValue)
                {
                    var editedItem = dictionary.Items.First(at => at.Id == model.AddedOrEditedItem.Id);
                    editedItem.Name = model.AddedOrEditedItem.Name;
                }
                _db.SaveChanges();
                ModelState.Clear();
                model = DtoHelper.GeEditDictionaryDto(dictionary);
            }

            return(View(model));
        }