public async Task <int> Handle(CreateBookCommand request, CancellationToken cancellationToken) { Book book = Book.CreateBook(request.Title, request.Author, request.PublishedDate, request.BookFormat); _db.Books.Add(book); await _db.SaveChangesAsync(cancellationToken); return(book.BookId); }
public async Task <Response> Handle(Request request, CancellationToken cancellationToken) { var digitalAssets = new HashSet <DigitalAssetApiModel>(); if (!MultipartRequestHelper.IsMultipartContentType(_httpContext.Request.ContentType)) { throw new Exception($"Expected a multipart request, but got {_httpContext.Request.ContentType}"); } var boundary = MultipartRequestHelper.GetBoundary( MediaTypeHeaderValue.Parse(_httpContext.Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, _httpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); while (section != null) { ContentDispositionHeaderValue contentDisposition; var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition); if (hasContentDispositionHeader) { if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { using (var targetStream = new MemoryStream()) { await section.Body.CopyToAsync(targetStream); var bytes = StreamHelper.ReadToEnd(targetStream); var tenant = await _context.Tenants.FindAsync(new Guid(_httpContext.Request.GetHeaderValue("Tenant"))); var digitalAsset = new DigitalAsset(); digitalAsset.Tenant = tenant; digitalAsset.FileName = $"{contentDisposition.FileName}".Trim(new char[] { '"' }).Replace("&", "and"); digitalAsset.Name = digitalAsset.FileName; digitalAsset.Bytes = bytes; digitalAsset.ContentType = section.ContentType; digitalAsset.UploadedOn = DateTime.UtcNow; _context.DigitalAssets.Add(digitalAsset); await _context.SaveChangesAsync(cancellationToken, request.Username); digitalAssets.Add(DigitalAssetApiModel.FromDigitalAsset(digitalAsset)); } } } section = await reader.ReadNextSectionAsync(); } return(new Response() { }); }
public async Task <int> Handle(DislikeBookCommand request, CancellationToken cancellationToken) { Book book = await _db.Books.Where(p => p.BookId == request.BookId).SingleOrDefaultAsync(); if (book != null) { book.DisLike(); await _db.SaveChangesAsync(cancellationToken); } else { throw new ApplicationException($"Book with id {request.BookId} not found."); } return(book.DislikeCount); }
public async Task <int> Handle(UpdateBookCommand request, CancellationToken cancellationToken) { Book book = await _db.Books.FindAsync(request.BookId); if (book == null) { throw new NotFoundException(nameof(UpdateBookCommand), request.BookId); } else { book.Update(request.Title, request.Author, request.PublishedDate, request.BookFormat); await _db.SaveChangesAsync(cancellationToken); return(book.BookId); } }