public bool AddNewHeader(ClaimsPrincipal contextUser, NewHeaderVM model) { var user = _userService.GetUserWithRoles(contextUser); var category = _categoryRepository.Get(x => x.IdCode == model.CategoryCode); if (category != null) { if (_headerRepository.Where(x => x.CategoryId == category.Id && x.Title == model.Title).Any()) { model.ValidationErrors.Add("errorMessage", "Bu katogeride bu başlıkta bir konu bulunmaktadır!"); } else { var header = new Header() { CategoryId = category.Id, ClickCount = 0, CreateDate = DateTime.Now, Title = model.Title, UserId = user.Id, IdCode = model.Title.AsIdCode() }; _headerRepository.Add(header); _headerRepository.Save(); model.HeaderCode = header.IdCode; var post = new Post() { Content = model.Content, HeaderId = header.Id, UserId = user.Id, CreateDate = header.CreateDate }; _postService.Add(post); _postService.Save(); return(true); } } return(false); }
public ActionResult Add(int id, string content) { try { if (id == 0) { headerRepository.Add(content); } else { Header footer = new Header(); footer.ID = id; footer.Contents = content; headerRepository.Edit(footer); } return(Json(new { IsSuccess = true, Message = "Lưu thành công" }, JsonRequestBehavior.AllowGet)); } catch (Exception) { return(Json(new { IsSuccess = false, Message = "Lưu thất bại" }, JsonRequestBehavior.AllowGet)); } }
public Header AddHeader(HeaderVM model) { var Header = new Header() { Title = model.Title, CategoryId = model.CategoryId, ClickCount = 0, UserId = model.UserId, CreateDate = DateTime.Now }; var entity = _headerRepository.Add(Header); try { _headerRepository.SaveChanges(); return(Header); } catch (Exception ex) { var errorMessage = ex.Message; return(null); } }
public void TestDocumentDeleteWithOneHeaderOneFooterOneParagraphFromDatabase() { User user4 = new User { UserName = "******", Password = "******", Name = "name4", LastName = "lastname3", Email = "email3", Administrator = true }; UserRepository.Add(user4); Document document = new Document { Id = Guid.NewGuid(), Title = "title", Creator = user4, CreationDate = DateTime.Now, LastModification = DateTime.Now, StyleClass = null }; DocumentRepository.Add(document); Header header = new Header { Id = Guid.NewGuid(), Content = null, DocumentThatBelongs = document, StyleClass = null }; HeaderRepository.Add(header); Footer footer = new Footer { Id = Guid.NewGuid(), Content = null, DocumentThatBelongs = document, StyleClass = null }; FooterRepository.Add(footer); Paragraph paragraph1 = new Paragraph { Id = Guid.NewGuid(), Content = null, DocumentThatBelongs = document, StyleClass = null, Position = 0 }; ParagraphRepository.Add(paragraph1); Paragraph paragraph2 = new Paragraph { Id = Guid.NewGuid(), Content = null, DocumentThatBelongs = document, StyleClass = null, Position = 1 }; ParagraphRepository.Add(paragraph2); DocumentRepository.Delete(document.Id); UserRepository.Delete(user4.Email); bool headerExistsAfterDelete = HeaderRepository.Exists(header.Id); bool paragraph1ExistsAfterDelete = ParagraphRepository.Exists(paragraph1.Id); bool paragraph2ExistsAfterDelete = ParagraphRepository.Exists(paragraph2.Id); bool footerExistsAfterDelete = FooterRepository.Exists(footer.Id); Assert.IsFalse(headerExistsAfterDelete); Assert.IsFalse(paragraph1ExistsAfterDelete); Assert.IsFalse(paragraph2ExistsAfterDelete); Assert.IsFalse(footerExistsAfterDelete); }
public void TestHeaderIsUpdatedCorrectlyFromDatabase() { User userH1 = new User { UserName = "******", Password = "******", Name = "nameH1", LastName = "lastnameH1", Email = "emailH1", Administrator = true }; UserRepository.Add(userH1); StyleClass StyleClassH1 = new StyleClass { Name = "StyleClassH1", BasedOn = null }; StyleClassRepository.Add(StyleClassH1); StyleClass StyleClassH2 = new StyleClass { Name = "StyleClassH2", BasedOn = null }; StyleClassRepository.Add(StyleClassH2); Document document = new Document { Id = Guid.NewGuid(), Title = "title1", Creator = userH1, CreationDate = DateTime.Now, LastModification = DateTime.Now, StyleClass = StyleClassH1 }; DocumentRepository.Add(document); Content content = new Content { Id = Guid.NewGuid(), }; ContentRepository.Add(content); Header header = new Header { Id = Guid.NewGuid(), Content = content, DocumentThatBelongs = document, StyleClass = StyleClassH1 }; HeaderRepository.Add(header); Header updatedHeader = new Header { Id = header.Id, Content = content, DocumentThatBelongs = document, StyleClass = StyleClassH2 }; HeaderRepository.Update(updatedHeader); Header fromDatabase = HeaderRepository.GetById(updatedHeader.Id); StyleClassRepository.Delete(StyleClassH1.Name); StyleClassRepository.Delete(StyleClassH2.Name); DocumentRepository.Delete(document.Id); UserRepository.Delete(userH1.Email); ContentRepository.Delete(content); Assert.AreEqual(updatedHeader.Id, fromDatabase.Id); Assert.AreEqual(updatedHeader.Content.Id, fromDatabase.Content.Id); Assert.AreEqual(updatedHeader.StyleClass.Name, fromDatabase.StyleClass.Name); Assert.AreEqual(updatedHeader.DocumentThatBelongs.Id, fromDatabase.DocumentThatBelongs.Id); }