public async Task <int> AddBookAuthorAsync(AddBookAuthorBindingModel model) { var checkForDuplicate = this.DbContext .BookAuthors .FirstOrDefault(x => x.FullName == model.FullName); if (checkForDuplicate != null) { return(ErrorId); } var newAuthor = this.Mapper.Map <BookAuthor>(model); var authorGenre = this.DbContext .BookAuthorGenres .FirstOrDefault(x => x.AuthorGenreName == model.BookAuthorGenreStr); if (authorGenre == null) { authorGenre = new BookAuthorGenre() { AuthorGenreName = model.BookAuthorGenreStr }; await this.DbContext.BookAuthorGenres.AddAsync(authorGenre); await this.DbContext.SaveChangesAsync(); } newAuthor.BookAuthorGenreId = authorGenre.Id; newAuthor.BookAuthorGenre = authorGenre; if (newAuthor.HighLightVideoURL.Contains(CommonConstants.OriginalVideoUrlPart)) { newAuthor.HighLightVideoURL = ModifyVideoURL_Embeded.ModifyEmbed(newAuthor.HighLightVideoURL); } await this.DbContext.BookAuthors.AddAsync(newAuthor); await this.DbContext.SaveChangesAsync(); return(newAuthor.Id); }
public async Task <IActionResult> AddBookAuthor(AddBookAuthorBindingModel model) { if (!this.ModelState.IsValid) { SetErrorMessage(CommonConstants.DangerMessage); return(this.AddBookAuthor()); } int generatedId = await this.bookService.AddBookAuthorAsync(model); if (generatedId < 1) { SetErrorMessage(CommonConstants.DuplicateMessage); return(this.AddBookAuthor()); } SetSuccessMessage(string.Format(CommonConstants.SuccessMessage, CommonConstants.BookAuthorDisplay)); return(RedirectToAction(RedirectConstants.BookAuthorSuffx, RedirectConstants.BooksSuffix, generatedId)); }