public async Task <IActionResult> Create([FromBody] BookEditModel book) { if (ModelState.IsValid) { BookResultModel response = await this.bookService.UpdateBook(book.Id, book.Name, book.Description, book.ImageUrl, book.Author, book.ReleaseDate, book.Categories); if (!response.Success) { FailedResponseModel badResponse = new FailedResponseModel() { Errors = response.Errors }; return(BadRequest(badResponse)); } BookSuccessResponseModel successResponse = new BookSuccessResponseModel() { Name = response.Name }; return(Ok(successResponse)); } return(BadRequest(new FailedResponseModel { Errors = ModelState.Values.SelectMany(x => x.Errors.Select(y => y.ErrorMessage)) })); }
public async Task <BookResultModel> CreateBook(string name, string description, string imageUrl, GenericComboBox author, DateTime?releaseDate, IEnumerable <GenericComboBox> categories) { BookResultModel result = new BookResultModel(); if (await this.dbContext.Books.AnyAsync(p => p.Name.ToLower().Trim() == name.ToLower().Trim())) { result.Success = false; result.Errors = new[] { "Book with this name already exist." }; return(result); } Book book = new Book { Id = Guid.NewGuid(), Name = name.Trim(), Description = description.Trim(), ImageUrl = imageUrl.Trim(), ReleaseDate = releaseDate, }; if (author != null && author.Id != default) { book.AuthorId = author.Id; } await this.dbContext.Books.AddAsync(book); if (categories != null && categories.Any()) { IEnumerable <Guid> categoryIds = categories.Where(x => x != null && x.Id != default).Select(x => x.Id); IEnumerable <Guid> categoriesInDB = await this.dbContext.Categories.Where(x => categoryIds.Contains(x.Id)).Select(x => x.Id).ToListAsync(); foreach (Guid categoryId in categoriesInDB) { XRefBookCategory xRefForInsert = new XRefBookCategory() { Id = Guid.NewGuid(), BookId = book.Id, CategoryId = categoryId }; this.dbContext.XRefBookCategories.Add(xRefForInsert); } } await this.dbContext.SaveChangesAsync(); result.Name = book.Name; result.Success = true; return(result); }
public object SearchFiles(string keywords) { var results = BookshelfSearcher.Search(keywords); var data = new List <BookResultModel>(); if (results.Any()) { var books = results.GroupBy(x => x.Fields["book"]); var resultsPerBook = 10; foreach (var book in books) { var bookData = new BookResultModel() { Name = book.Key }; var resultsList = new List <BookResultModel.BookEntry>(); foreach (var result in book.OrderByDescending(x => x.Score).Take(resultsPerBook)) { var hintUrl = HttpUtility.UrlDecode(HttpUtility.UrlDecode(result.Fields["url"])); var title = HttpUtility.UrlDecode(HttpUtility.UrlDecode(result.Fields["title"])); hintUrl = hintUrl.Substring(0, hintUrl.Length - title.Length - 3); var hintWindowLength = 50; if (hintUrl.Length - hintWindowLength > 0) { hintUrl = "…" + hintUrl.Substring(hintUrl.Length - hintWindowLength); } resultsList.Add(new BookResultModel.BookEntry() { Title = title, Url = result.Fields["url"], HintUrl = hintUrl, Score = result.Score.ToString() }); bookData.TotalScore += result.Score; } bookData.Results = resultsList; data.Add(bookData); } } return(data.OrderByDescending(x => x.TotalScore)); }
public async Task <BookResultModel> UpdateBook(Guid id, string name, string description, string imageUrl, GenericComboBox author, DateTime?releaseDate, IEnumerable <GenericComboBox> categories) { BookResultModel result = new BookResultModel(); Book book = await this.dbContext.Books.Include(x => x.BookCategories).FirstOrDefaultAsync(p => p.Id == id); if (book == null) { result.Success = false; result.Errors = new[] { "Book with this Id not exist." }; return(result); } if (book.Name.ToLower().Trim() != name.ToLower().Trim() && await this.dbContext.Categories.AnyAsync(p => p.Name.ToLower().Trim() == name.ToLower().Trim() && p.Id != id)) { result.Success = false; result.Errors = new[] { "Book with this name already exist." }; return(result); } this.dbContext.Books.Attach(book); book.Name = name.Trim(); book.Description = description.Trim(); book.ImageUrl = imageUrl.Trim(); book.ReleaseDate = releaseDate; if (author != null && author.Id != default) { book.AuthorId = author.Id; } else { book.AuthorId = null; } await this.UpdateBookCategoriesAsync(categories, book); await this.dbContext.SaveChangesAsync(); result.Name = book.Name; result.Success = true; return(result); }
public async Task <IActionResult> Delete(Guid id) { BookResultModel response = await this.bookService.DeleteBook(id); if (!response.Success) { FailedResponseModel badResponse = new FailedResponseModel() { Errors = response.Errors }; return(BadRequest(badResponse)); } BookSuccessResponseModel successResponse = new BookSuccessResponseModel() { Name = response.Name }; return(Ok(successResponse)); }
public async Task <BookResultModel> DeleteBook(Guid id) { BookResultModel result = new BookResultModel(); Book book = await this.dbContext.Books.FirstOrDefaultAsync(p => p.Id == id); if (book == null) { result.Success = false; result.Errors = new[] { "Book with this Id not exist." }; return(result); } this.dbContext.Books.Remove(book); await this.dbContext.SaveChangesAsync(); result.Name = book.Name; result.Success = true; return(result); }