public async Task <IActionResult> UpdateStatus([FromBody, CustomizeValidator] OrderToStatusUpdate orderStatus, CancellationToken cancellationToken = default) { if (orderStatus is null || !ModelState.IsValid) { return(BadRequest(ModelState)); } try { var result = await _orderService.UpdateOrderStatusAsync(orderStatus, cancellationToken); return(result.IsError ? throw new InvalidOperationException(result.Message) : Ok(result.IsSuccess)); //return result.IsError ? throw new InvalidOperationException(result.Message) : (IActionResult)Ok(result.Data); } catch (InvalidOperationException ex) { Log.Error(ex, ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError, new CustumResult() { Status = StatusCodes.Status500InternalServerError, Message = ex.Message })); } }
public async Task <Result> UpdateOrderStatusAsync(OrderToStatusUpdate order, CancellationToken cancellationToken = default) { OrderDB orderForUpdate = await _context.Orders.Where(_ => _.Id == Guid.Parse(order.Id)).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); string orderStatus = Enum.GetName(typeof(OrderStatuses), order.StatusIndex); switch (orderStatus) { case "OnWay": orderForUpdate.Status = "OnWay"; orderForUpdate.UpdateTime = DateTime.Now; _context.Entry(orderForUpdate).Property(c => c.Status).IsModified = true; _context.Entry(orderForUpdate).Property(c => c.UpdateTime).IsModified = true; break; case "Delivered": if (orderForUpdate.PaymentTime.HasValue) { orderForUpdate.Status = "Delivered"; orderForUpdate.DeliveryTime = DateTime.Now; orderForUpdate.UpdateTime = DateTime.Now; _context.Entry(orderForUpdate).Property(c => c.Status).IsModified = true; _context.Entry(orderForUpdate).Property(c => c.DeliveryTime).IsModified = true; _context.Entry(orderForUpdate).Property(c => c.UpdateTime).IsModified = true; } else { return(Result.Quite(NotificationConstans.WAITING_FOR_PAYMENT)); } break; case "Canceled": orderForUpdate.Status = "Canceled"; orderForUpdate.UpdateTime = DateTime.Now; _context.Entry(orderForUpdate).Property(c => c.Status).IsModified = true; _context.Entry(orderForUpdate).Property(c => c.UpdateTime).IsModified = true; break; case "Paid": orderForUpdate.PaymentTime = DateTime.Now; _context.Entry(orderForUpdate).Property(c => c.PaymentTime).IsModified = true; break; default: return(Result.Quite(NotificationConstans.NOTHING_TO_CHANGE)); } try { var result = await _context.SaveChangesAsync(cancellationToken); return(Result.Ok()); } catch (DbUpdateConcurrencyException ex) { return(Result.Fail(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message)); } catch (DbUpdateException ex) { return(Result.Fail(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message)); } }