public async Task <ActionResult> DeclineAdoptOrder(BookOrderForDeclineDto adoptOrder) { if (adoptOrder == null) { return(BadRequest()); } try { await _bookOrderService.DeclineBookOrder(adoptOrder); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task DeclineBookOrder(BookOrderForDeclineDto order) { var bookOrder = _bookOrderRepository.Entities.FirstOrDefault(x => x.Id == order.Id); var animal = _animalRepository.Entities.FirstOrDefault(x => x.Id == bookOrder.AnimalId); if (OrderStatus.Declined == bookOrder.Status) { throw new ObjectException(nameof(bookOrder.AnimalId), "animal is declined already"); } if (bookOrder == null || bookOrder.Status == OrderStatus.Declined) { throw new ObjectNotFoundException("Threre isn't book order or it's declined already"); } animal.Status = AnimalStatus.None; _animalRepository.Update(animal); _mapper.Map(order, bookOrder); bookOrder.Status = OrderStatus.Declined; bookOrder.ClosingDate = DateTime.Now; _bookOrderRepository.Update(bookOrder); await _bookOrderRepository.SaveAsync(); }