public async Task <BookOrderedResponse> SaveAsync(BookOrdered bookOrdered)
        {
            var existingBookForSale = await bookForSaleRepository.FindByIdAsync(bookOrdered.BookForSaleId);

            if (existingBookForSale == null)
            {
                return(new BookOrderedResponse("Book for sale not found"));
            }
            if (existingBookForSale.BookOrdered != null)
            {
                return(new BookOrderedResponse("Book for sale was sold or reserved"));
            }

            try
            {
                await bookOrderedRepository.AddAsync(bookOrdered);

                await unitOfWork.CompleteAsync();

                return(new BookOrderedResponse(bookOrdered));
            }
            catch (Exception ex)
            {
                return(new BookOrderedResponse($"Error when saving the BookOrdered: {ex.Message}"));
            }
        }
Exemple #2
0
        public async Task <BookForSaleResponse> DeleteAsync(int id)
        {
            var existingBookForSale = await bookForSaleRepository.FindByIdAsync(id);

            if (existingBookForSale == null)
            {
                return(new BookForSaleResponse("Book for sale not found"));
            }
            if (existingBookForSale.BookOrdered != null)
            {
                return(new BookForSaleResponse("Book for sale is ordered"));
            }
            try
            {
                bookForSaleRepository.Remove(existingBookForSale);
                await unitOfWork.CompleteAsync();

                return(new BookForSaleResponse(existingBookForSale));
            }
            catch (Exception ex)
            {
                return(new BookForSaleResponse($"Error when deleting BookForSale: {ex.Message}"));
            }
        }