public async Task <CQRSResult <BookGetDTO> > Handle(BookUpdateCommand request, CancellationToken cancellationToken) { var exists = uow.BooksRepository.Contains(x => x.Id == request.Id); if (exists) { if (request.ClearImage) { var coverImagePath = Path.GetFileName(request.CoverImagePath); if (await fileStorageService.Delete(coverImagePath)) { request.CoverImagePath = ""; } } else if (request.coverImage.Length > 0) { var fExt = Path.GetExtension(request.CoverImagePath); var fNewName = Guid.NewGuid().ToString() + fExt; request.CoverImagePath = await fileStorageService.Save(request.coverImage, fNewName); } var entity = BookUpdateMap.ToModel(request); uow.BooksRepository.Update(entity); await uow.Save(); return(BookGetMap.ToDTO(entity)); } else { return((null as BookGetDTO).AsCQRSResult(code: 404)); } }
public static AuthorGetDTO ToDTO(this Author model, bool withLinks = true) { return(new AuthorGetDTO { Id = model.Id, Name = model.Name, AuthorBooks = model.AuthorBooks?.Select(x => BookAuthorGetMap.ToDTO(x)).ToArray(), Books = withLinks ? model.Books?.Select(x => BookGetMap.ToDTO(x, false)).ToArray() : new BookGetDTO[0] }); }
public async Task <CQRSResult <BookGetDTO> > Handle(BookQuery request, CancellationToken cancellationToken) { var book = uow.BooksRepository.GetById(request.Id); if (book != null) { return(BookGetMap.ToDTO(book)); } else { return(BookGetMap.ToDTO(book).AsCQRSResult(code: 404)); } }
public async Task <CQRSResult <BookGetDTO> > Handle(BookAddCommand request, CancellationToken cancellationToken) { if (request.coverImage.Length > 0) { var fExt = Path.GetExtension(request.CoverImagePath); var fNewName = Guid.NewGuid().ToString() + fExt; request.CoverImagePath = await fileStorageService.Save(request.coverImage, fNewName); } var entity = BookAddMap.ToModel(request); uow.BooksRepository.Add(entity); await uow.Save(cancellationToken); return(BookGetMap.ToDTO(entity)); }
public async Task <CQRSResult <BookGetDTO> > Handle(BookDeleteCommand request, CancellationToken cancellationToken) { var book = uow.BooksRepository.GetById(request.Id); if (book != null) { uow.BooksRepository.Delete(book); } else { return(BookGetMap.ToDTO(book).AsCQRSResult(code: 404)); } await uow.Save(cancellationToken); return(BookGetMap.ToDTO(book)); }
public async Task <CQRSResult <List <BookGetDTO> > > Handle(BooksQuery request, CancellationToken cancellationToken) { var books = uow.BooksRepository.Get(); return(books.Select(x => BookGetMap.ToDTO(x)).ToList()); }