public void TakeDelivery(int userId, int deliveryId) { var delivery = _queryFacade.Deliveries.FirstOrDefault(d => d.Id == deliveryId); if (delivery == null) { //404 код, если доставки с указанным id не найдено HttpContext.Response.StatusCode = 404; HttpContext.Response.WriteAsync($"Delivery with id {deliveryId} not found").Wait(); } else if (delivery.Status != DeliveySatus.Available) { //422, если доставка не доступна HttpContext.Response.StatusCode = 422; HttpContext.Response.WriteAsync($"Delivery with id {deliveryId} is not available").Wait(); } else { //если все ок - назначем AssignToUserCommand command = new AssignToUserCommand() { DeliveryId = delivery.Id, UserId = userId }; _commandHandler.Handle(command); } }
public void Handle(AssignToUserCommand command) { var delivery = _workUnit.Deliveries.FirstOrDefault(d => d.Id == command.DeliveryId); if (delivery != null) { delivery.Status = DeliveySatus.Taken; delivery.UserId = command.UserId; delivery.ModificationDate = DateTime.Now; _workUnit.SaveChanges(); } }