public AuthorUpdateViewModel GetBookEdit(int id) { List <AuthorInBook> bookInAurhor = _authorInBookRepository.GetAuthor(id).ToList(); var authorView = new AuthorUpdateViewModel(); if (bookInAurhor != null) { authorView = bookInAurhor.GroupBy(x => x.Author.Id).Select(x => new AuthorUpdateViewModel() { Id = x.First().Author.Id, BirthDate = x.First().Author.BirthDate, DaiedDate = x.First().Author.DaiedDate, FirstName = x.First().Author.FirstName, LastName = x.First().Author.LastName }).First(); authorView.Books = new List <BookTitleViewModel>(); foreach (AuthorInBook authorInBook in bookInAurhor) { authorView.Books.Add(new BookTitleViewModel() { Id = authorInBook.Book.Id, NameBook = authorInBook.Book.NameBook, NumberPages = authorInBook.Book.NumberPages }); } } return(authorView); }
public void Update(AuthorUpdateViewModel authorView) { Author author = _authorRepository.Get(authorView.Id); if (author != null) { author.FirstName = authorView.FirstName; author.LastName = authorView.LastName; author.DaiedDate = authorView.DaiedDate; author.BirthDate = authorView.BirthDate; _authorRepository.Update(author); } //IEnumerable<AuthorInBook> authorInBook = GetAuthorInBook(authorView); //IEnumerable<AuthorInBook> author = _authorInBookRepository.GetAuthor(authorView.Id).ToList(); //var updateAuthor = authorInBook.GroupBy(x => x.Id).Select(z => z.First().Id = author.First().Id); //foreach (AuthorInBook a in authorInBook) //{ // a.Book.Id = author.First().Author.Id; //} //if(author != null) //{ // _authorInBookRepository.Update(authorInBook); //} }
public IActionResult Update(AuthorUpdateViewModel model) { var entity = _mapper.Map <Author>(model); var affectedRowsCount = _service.Update(entity); ViewData["AffectedRowsCount"] = affectedRowsCount; return(View(model)); }
private List <AuthorInBook> GetAuthorInBook(AuthorUpdateViewModel bookView /*, IEnumerable<int> authorId*/) { var authorInBook = new List <AuthorInBook>(); var mapper = new AutoMapperForAuthor(); Author author = mapper.Mapp(bookView); authorInBook.Add(new AuthorInBook { Author = author }); return(authorInBook); }
public ActionResult Edit(AuthorUpdateViewModel authorView) { if (authorView == null) { return(HttpNotFound()); } if (ModelState.IsValid) { _authorService.Update(authorView); } return(RedirectToAction("Index")); }
public ActionResult Create(AuthorUpdateViewModel authorView, IEnumerable <int> booksMultiSelect) { if (authorView == null) { return(HttpNotFound()); } if (ModelState.IsValid) { _authorService.CreateAuthor(authorView /*, booksMultiSelect*/); } return(RedirectToAction("Index")); }
public ActionResult Edit(int?id) { if (id == null) { return(HttpNotFound()); } //AuthorUpdateViewModel author = _authorService.GetBookEdit(id.Value); AuthorUpdateViewModel author = _authorService.GetAuthor(id.Value); if (author == null) { return(HttpNotFound()); } ViewData["Books"] = _authorService.GetBooks(); return(View(author)); }
public AuthorUpdateViewModel GetAuthor(int id) { Author authorRepository = _authorRepository.Get(id); var mapp = new AutoMapperForAuthor(); AuthorUpdateViewModel authorView = mapp.Mapp(authorRepository); //List<AuthorInBook> authors = _authorInBookRepository.GetAuthor(id).ToList(); //var authorView = new AuthorGetViewModel(); //if (authors != null) //{ // authorView = authors.GroupBy(x => x.Author.Id).Select(x => new AuthorGetViewModel() // { // Author = x.First().Author, // Books = x.Select(a => a.Book).ToList() // }).First(); //} return(authorView); }
public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //DONE: add check if the author already exists var author = await this.repository.GetByIdAsync(id); if (author == null) { return(NotFound()); } author.FullName = model.FullName; await this.repository.UpdateAsync(author); return(NoContent()); }
public void CreateAuthor(AuthorUpdateViewModel authorView /*, IEnumerable<int> bookId*/) { IEnumerable <AuthorInBook> authorInBook = GetAuthorInBook(authorView /*, bookId*/); _authorInBookRepository.Create(authorInBook); //var authorInBook = new List<AuthorInBook>(); //if (authorView.Books.Count != 0) //{ // foreach (Book b in authorView.Books) // { // authorInBook.Add(new AuthorInBook { Author = authorView.Author, Book = b }); // } //} //if (authorView.Books.Count == 0) //{ // authorInBook.Add(new AuthorInBook { Author = authorView.Author }); //} //_authorInBookRepository.Create(authorInBook); }
public Author Mapp(AuthorUpdateViewModel author) { Mapper.Initialize(m => m.CreateMap <AuthorUpdateViewModel, Author>()); return(Mapper.Map <AuthorUpdateViewModel, Author>(author)); }