コード例 #1
0
 public IActionResult Index(CreateContentTypeModel model)
 {
     if (ModelState.IsValid)
     {
         var contentType = new EntityType {
             Name = model.Name
         };
         _db.ContentTypes.Add(contentType);
         _db.SaveChanges();
         var editModel = DtoHelper.GetEditContentTypeModel(contentType);
         return(View(nameof(Edit), editModel));
     }
     return(View(model));
 }
コード例 #2
0
        public IActionResult Edit(int id, int?attr)
        {
            if (id < 0)
            {
                return(NotFound());
            }
            if (attr.HasValue && attr < 0)
            {
                return(NotFound());
            }
            var contentType = _db.ContentTypes
                              .Include(ct => ct.Attrs)
                              .FirstOrDefault(t => t.Id == id);

            if (contentType == null)
            {
                return(NotFound());
            }
            var model = DtoHelper.GetEditContentTypeModel(contentType);

            if (attr.HasValue)
            {
                model.AddedOrEditedAttr = model.Attrs.FirstOrDefault(at => at.Id == attr);
                if (model.AddedOrEditedAttr == null)
                {
                    return(NotFound());
                }
            }

            model.Dictionaries = _db.Dictionaries.Select(d => new SelectListItem
            {
                Value = d.Id.ToString(),
                Text  = d.Name
            }).ToList();

            return(View(model));
        }
コード例 #3
0
 public IActionResult Edit(EditContentTypeModel 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 (IsAdditionNewAttribute(model))
     {
         ModelState["Name"].Errors.Clear();
         var contentType = _db.ContentTypes
                           .Include(ct => ct.Attrs)
                           .FirstOrDefault(t => t.Id == model.Id);
         if (contentType == null)
         {
             return(NotFound());
         }
         if (model.AddedOrEditedAttr.AttributeType == AttrType.Dictionary)
         {
             if (!_db.Dictionaries.Any(d => d.Id == model.AddedOrEditedAttr.DictionaryId))
             {
                 return(NotFound($"Dictionary with id = { model.AddedOrEditedAttr.DictionaryId } not found"));
             }
         }
         contentType.Attrs.Add(new Attribute
         {
             Name          = model.AddedOrEditedAttr.Name,
             AttrType      = model.AddedOrEditedAttr.AttributeType,
             ContentTypeId = contentType.Id,
             Required      = model.AddedOrEditedAttr.Required,
             DictionaryId  = model.AddedOrEditedAttr.DictionaryId
         });
         _db.SaveChanges();
         ModelState.Clear();
         model = DtoHelper.GetEditContentTypeModel(contentType);
     }
     else if (ModelState.IsValid)
     {
         var contentType = _db.ContentTypes
                           .Include(ct => ct.Attrs)
                           .FirstOrDefault(t => t.Id == model.Id);
         if (contentType == null)
         {
             return(NotFound());
         }
         contentType.Name = model.Name;
         if (model.AddedOrEditedAttr != null && model.AddedOrEditedAttr.Id.HasValue)
         {
             var editedAttr = contentType.Attrs.First(at => at.Id == model.AddedOrEditedAttr.Id);
             editedAttr.Name     = model.AddedOrEditedAttr.Name;
             editedAttr.AttrType = model.AddedOrEditedAttr.AttributeType;
             editedAttr.Required = model.AddedOrEditedAttr.Required;
         }
         _db.SaveChanges();
         ModelState.Clear();
         model = DtoHelper.GetEditContentTypeModel(contentType);
     }
     model.Dictionaries = _db.Dictionaries.Select(d => new SelectListItem
     {
         Value = d.Id.ToString(),
         Text  = d.Name
     }).ToList();
     return(View(model));
 }