//returns books and authors data to edit public FullInfoOutput GetFullInfoToEdit(int BookId) { if (_regRepository.BooksDetails.Any(x => x.Id == BookId)) { var bookToEdit = _regRepository.BooksDetails.Where(x => x.Id == BookId).FirstOrDefault(); FullInfoOutput fullInfo = new FullInfoOutput(); fullInfo.BookId = bookToEdit.Id; fullInfo.BookName = bookToEdit.BookName; fullInfo.WrittenTime = bookToEdit.WrittenTime; if (bookToEdit.AuthorId != null) { bookToEdit.AuthorsInfo = _regRepository.AuthorsDetails.Where(x => x.Id == bookToEdit.AuthorId).FirstOrDefault(); fullInfo.AuthorId = bookToEdit.AuthorsInfo.Id; fullInfo.Name = bookToEdit.AuthorsInfo.Name; fullInfo.Lastname = bookToEdit.AuthorsInfo.Lastname; fullInfo.BirthCounty = bookToEdit.AuthorsInfo.BirthCountry; fullInfo.BirthDate = bookToEdit.AuthorsInfo.BirthDate; if (bookToEdit.AuthorsInfo.DeathDate == null) { fullInfo.isAlive = true; } else { fullInfo.DeathDate = bookToEdit.AuthorsInfo.DeathDate; } } return(fullInfo); } else { return(new FullInfoOutput()); } }
//changes book and author data with new info public void UpdateBookAndAuthorInfo(FullInfoOutput updatedInfo) { if (updatedInfo != null) { var bookToUpdate = _regRepository.BooksDetails.Where(x => x.Id == updatedInfo.BookId).FirstOrDefault(); bookToUpdate.BookName = updatedInfo.BookName; bookToUpdate.WrittenTime = updatedInfo.WrittenTime; if (_regRepository.AuthorsDetails.Any(x => x.Id == updatedInfo.AuthorId)) { bookToUpdate.AuthorsInfo.Name = updatedInfo.Name; bookToUpdate.AuthorsInfo.Lastname = updatedInfo.Lastname; bookToUpdate.AuthorsInfo.BirthCountry = updatedInfo.BirthCounty; bookToUpdate.AuthorsInfo.BirthDate = updatedInfo.BirthDate; bookToUpdate.AuthorsInfo.DeathDate = updatedInfo.DeathDate; } _regRepository.SaveChanges(); } }
//returns the list of books with authors info to output public List <FullInfoOutput> BooksWithAuthor() { if (_regRepository.AuthorsDetails != null) { List <FullInfoOutput> booksWithAuthorsList = new List <FullInfoOutput>(); var booksTable = _regRepository.BooksDetails; foreach (var item in booksTable) { if (item.AuthorId != null) { FullInfoOutput bookWithAuthor = new FullInfoOutput(); bookWithAuthor.BookId = item.Id; bookWithAuthor.BookName = item.BookName; bookWithAuthor.WrittenTime = item.WrittenTime; item.AuthorsInfo = _regRepository.AuthorsDetails.Where(x => x.Id == item.AuthorId).FirstOrDefault(); bookWithAuthor.AuthorId = item.AuthorsInfo.Id; bookWithAuthor.Name = item.AuthorsInfo.Name; bookWithAuthor.Lastname = item.AuthorsInfo.Lastname; bookWithAuthor.BirthCounty = item.AuthorsInfo.BirthCountry; bookWithAuthor.BirthDate = item.AuthorsInfo.BirthDate; if (item.AuthorsInfo.DeathDate == null) { bookWithAuthor.isAlive = true; } else { bookWithAuthor.DeathDate = item.AuthorsInfo.DeathDate; } booksWithAuthorsList.Add(bookWithAuthor); } } return(booksWithAuthorsList); } else { return(new List <FullInfoOutput>()); } }
public IActionResult Update(FullInfoOutput updatedInfo) { _authorsDetails.UpdateBookAndAuthorInfo(updatedInfo); return(RedirectToAction("Index")); }
public IActionResult Edit(int BookId) { FullInfoOutput result = _booksDetails.GetFullInfoToEdit(BookId); return(View(result)); }