public async Task <IActionResult> DeliverOrder([FromBody] UpdatedOrder order) { try { HttpContext.Request.Headers.TryGetValue("Authorization", out var token); var currentPhotographerEmail = HttpContext.User; Order orderInDb = _database.GetOrderById(order.orderId); if (!OrderBelongsToPhotographer(currentPhotographerEmail, orderInDb.photographerEmail)) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "Requested order does not belong to the provided photographer." })); } if (orderInDb.status == StatusType.Delivered) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "The order is already delivered." })); } if (order.status != StatusType.Delivered) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "A photographer can only change the status of an order to delivered." })); } _database.UpdateOrderStatus(order.orderId, order.status); string deliverMsg = $"Order {order.status} by {order.photographerEmail}."; bool orderstatusChangedOnHub = await ChangeOrderstatusOnHub(order, deliverMsg, token, _config); if (orderstatusChangedOnHub) { return(StatusCode(StatusCodes.Status200OK, new { message = deliverMsg })); } else { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "Hub request did not succeed." })); } } catch (ArgumentNullException) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "Put request must contain an orderId." })); } catch (OrderDoesNotExistException) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "The provided orderId does not exist." })); } catch (Exception err) { Console.WriteLine(err); return(StatusCode(500)); } }
public async Task <IActionResult> UpdateOrder([FromBody] UpdatedOrder order) { try { HttpContext.Request.Headers.TryGetValue("Authorization", out var token); var orderId = HttpContext.User.FindFirst("orderId").Value; Order orderInDb = _database.GetOrderById(orderId); if (order == null) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "Requested order does not exist." })); } if (orderInDb.status == StatusType.Cancelled) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "Order is already cancelled. You can't edit details of an order after it is cancelled." })); } if (orderInDb.status == StatusType.Created) { if (order.status == StatusType.InProgress) { orderInDb.photographerEmail = order.photographerEmail; orderInDb.description = order.description; orderInDb.status = order.status; _database.UpdateOrder(orderInDb); string updatedMsg = $"Order {order.status} with {order.photographerEmail} assigned as Photographer."; // Calling hub bool orderstatusChangedOnHub = await OrderController.ChangeOrderstatusOnHub(order, updatedMsg, token, _config); if (orderstatusChangedOnHub) { return(StatusCode(StatusCodes.Status200OK, new { message = updatedMsg })); } else { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "Hub request did not succeed." })); } } } if (order.status == StatusType.Cancelled) { _database.UpdateOrderStatus(orderId, order.status); string cancelledMsg = "Order 'Cancelled'."; // Calling hub bool orderstatusChangedOnHub = await OrderController.ChangeOrderstatusOnHub(order, cancelledMsg, token, _config); if (orderstatusChangedOnHub) { return(StatusCode(StatusCodes.Status200OK, new { message = cancelledMsg })); } else { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "Hub request did not succeed." })); } } IActionResult badRequest = BadRequest(new { message = "Select a valid status of the order." }); return(badRequest); } catch (ArgumentNullException) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "Put request must contain an orderId." })); } catch (OrderDoesNotExistException) { return(StatusCode(StatusCodes.Status400BadRequest, new { message = "The provided orderId does not exist." })); } catch (Exception err) { Console.WriteLine(err); return(StatusCode(500)); } }