public IActionResult Index(AddEntityDto model) { //todo: check all required attribute if (ModelState.IsValid) { var contentType = _db.ContentTypes .Include(t => t.Attrs) .FirstOrDefault(t => t.Id == model.ContentTypeId); if (contentType == null) { //todo: to log return(NotFound()); } if (!AllAttrsExists(model.Attrs, contentType)) { //todo: to log return(NotFound()); } var content = new Entity { EntityTypeId = model.ContentTypeId, AttrValues = model.Attrs.Select(CreateAttribute).ToList() }; _db.Content.Add(content); _db.SaveChanges(); return(RedirectToAction(nameof(Index), new { id = model.ContentTypeId })); } return(View(model)); }
public IActionResult Index(int id) { if (id < 1) { return(NotFound()); } var contentType = _db.ContentTypes .Include(t => t.Attrs) .FirstOrDefault(t => t.Id == id); if (contentType == null) { return(NotFound()); } var model = new AddEntityDto { ContentTypeId = contentType.Id, Attrs = contentType.Attrs.Any() ? contentType.Attrs.Select(a => new AttributeValueDto { AttributeName = a.Name, AttributeType = a.AttrType, AttributeId = a.Id, Required = a.Required, DictionaryItems = a.AttrType == AttrType.Dictionary ? GetDictionaryItems(a.DictionaryId) : null }).ToList() : new List <AttributeValueDto>() }; return(View(model)); }
public void Index_Test_Post() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.Content, GetContentList()); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentController(mockRepo.Object); var model = new AddEntityDto { Id = null, ContentTypeId = 1, Attrs = new List <AttributeValueDto> { new AttributeValueDto { AttributeId = 1, Value = "333", AttributeType = AttrType.String } } }; Assert.Single(mockRepo.Object.Content); var result = controller.Index(model); var viewResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", viewResult.ActionName); mockRepo.Verify(db => db.SaveChanges(), Times.Once()); Assert.Equal(2, mockRepo.Object.Content.Count()); Assert.Single(mockRepo.Object.Content.First().AttrValues); }
public void Index_Test_Post_InvalidContentTypeOrAttributeId() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.Content, GetContentList()); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentController(mockRepo.Object); var model = new AddEntityDto { ContentTypeId = 11 }; var result = controller.Index(model); Assert.IsType <NotFoundResult>(result); model = new AddEntityDto { ContentTypeId = 1, Attrs = new List <AttributeValueDto> { new AttributeValueDto { AttributeId = 44 } } }; result = controller.Index(model); Assert.IsType <NotFoundResult>(result); }
public void Index_Test_Post_InvalidModel() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.Content, GetContentList()); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentController(mockRepo.Object); controller.ModelState.AddModelError("Name", "Required"); var model = new AddEntityDto { Id = null, ContentTypeId = 1, Attrs = new List <AttributeValueDto> { new AttributeValueDto { AttributeId = 1, Value = "attr value 1" } } }; var result = controller.Index(model); var viewResult = Assert.IsType <ViewResult>(result); var resultModel = Assert.IsAssignableFrom <AddEntityDto>( viewResult.ViewData.Model); Assert.Null(resultModel.Id); Assert.Equal(1, resultModel.ContentTypeId); Assert.Equal("attr value 1", resultModel.Attrs.First().Value); }
public void AddEntityWithDictionaryAttributeValue() { var dbMock = GetContentTypeListWithDictionaries(); var controller = new ContentController(dbMock.Object); var model = new AddEntityDto { Id = null,//new entity ContentTypeId = 1, Attrs = new List <AttributeValueDto> { new AttributeValueDto { AttributeId = 1, DictionaryItemId = 1, AttributeType = AttrType.Dictionary } } }; Assert.Equal(0, dbMock.Object.Content.Count()); var result = controller.Index(model); var viewResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", viewResult.ActionName); dbMock.Verify(db => db.SaveChanges(), Times.Once()); Assert.Equal(1, dbMock.Object.Content.Count()); var attrValues = dbMock.Object.Content.First().AttrValues; Assert.Single(attrValues); Assert.Equal(1, attrValues.First().DictionaryItemId); Assert.Null(attrValues.First().Value); }