public virtual async Task <GetCurbsideResponse> PlaceOrder(PostCurbsideRequest request) { // do the "procesing" (??) await Task.Delay(1000 *request.Items.Length); // produce a pickupDateTime for it. // Save it to the database, var order = new CurbsideOrder { For = request.For, Items = String.Join(",", request.Items), PickupReadyAt = DateTime.Now.AddHours(new Random().Next(2, 5)) }; _context.CurbsideOrders.Add(order); await _context.SaveChangesAsync(); // map it, return in. return(new GetCurbsideResponse { Id = order.Id, For = order.For, Items = order.Items.Split(',').Select(int.Parse).ToArray(), PickupReadyAt = order.PickupReadyAt.Value }); }
public async Task <ActionResult> SyncCurbsideOrders([FromBody] PostSyncCurbsideOrdersRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else { // Save it to the DB var orderToSave = new CurbsideOrder { For = request.For, Items = request.Items, }; var numberOfItems = orderToSave.Items.Split(',').Count(); for (var t = 0; t < numberOfItems; t++) { await Task.Delay(1000); } orderToSave.PickupDate = DateTime.Now.AddHours(numberOfItems); // return 201, Location Header, Copy of the Entity _context.CurbsideOrders.Add(orderToSave); await _context.SaveChangesAsync(); var response = new GetCurbsideOrderResponse { Id = orderToSave.Id, For = orderToSave.For, Items = orderToSave.Items, PickupDate = orderToSave.PickupDate.Value }; return(Ok(response)); } }
public async Task <DateTime?> GetPickupDate(CurbsideOrder order) { var request = new PickupService.PickupRequest { For = order.For }; request.Items.AddRange(order.Items.Split(',').Select(int.Parse).ToArray()); try { var channel = GrpcChannel.ForAddress(_options.Value.Url); var client = new PickupService.PickupEstimator.PickupEstimatorClient(channel); var response = await client.GetPickupTimeAsync(request); _logger.LogInformation($"Estimated a pickup date of {response.PickupTime.ToDateTime().ToShortDateString() }"); return(response.PickupTime.ToDateTime()); } catch (Exception ex) { // our plan b _logger.LogError($"Got an exception trying to get the estimated pickup for {order.Id}. Exception Details : {ex}"); return(DateTime.Now.AddDays(5)); } }
public override async Task <GetCurbsideResponse> PlaceOrder(PostCurbsideRequest request) { // Save it to the database, var order = new CurbsideOrder { For = request.For, Items = String.Join(",", request.Items), PickupReadyAt = null }; _context.CurbsideOrders.Add(order); await _context.SaveChangesAsync(); // map it, return in. if (await _channel.AddCurbside(new CurbsideChannelRequest { OrderId = order.Id })) { // it was able to write it. } else { // it wasn't } return(new GetCurbsideResponse { Id = order.Id, For = order.For, Items = order.Items.Split(',').Select(int.Parse).ToArray(), PickupReadyAt = null }); }
public async Task <DateTime> GetPickupDate(CurbsideOrder order) { var request = new PickupService.PickupRequest { For = order.For }; request.Items.AddRange(order.Items.Split(',').Select(int.Parse).ToArray()); try { _logger.LogInformation("Creating GRPC service"); var channel = GrpcChannel.ForAddress(_options.Value.Url); var client = new PickupService.PickupEstimator.PickupEstimatorClient(channel); _logger.LogInformation($"Making the GRPC call for {request.For}"); var response = await client.GetPickupTimeAsync(request); _logger.LogInformation($"Pickup time for {request.For} is {response.PickupTime.ToDateTime().ToShortDateString()}"); return(response.PickupTime.ToDateTime()); } catch (Exception e) { _logger.LogError("GRPC call has failed"); _logger.LogError(e.Message); // Gotta do real things to handle this error. return(DateTime.Now.AddYears(1)); } }
public async Task <ActionResult> PlaceOrder([FromBody] CreateCurbisdeOrder orderToPlace) { //validate the model (return 400 if bad) CurbsideOrder response = await _curbsideMapper.PlaceOrder(orderToPlace); // this should be wrapped in a try catch and if fails remove it from the database etc. await _channel.AddCurbsideOrder(new CurbsideChannelRequest { OrderId = response.Id }); return(Ok(response)); }
public async Task<ActionResult> GetOrderById(int orderId) { CurbsideOrder response = await _curbsideQueries.GetById(orderId); if(response == null) { return NotFound(); } else { return Ok(response); } }
public async Task<ActionResult> AddAnOrder([FromBody] PostCurbsideOrderRequest orderToPlace) { if(!ModelState.IsValid) { return BadRequest(ModelState); } else { CurbsideOrder response = await _curbsideCommands.AddOrder(orderToPlace,true); // I'll pause here.. for dramatic effect. return CreatedAtRoute("curbside#getbyid", new { orderId = response.Id }, response); } }
public async Task <ActionResult> PlaceOrder([FromBody] CreateCurbsideOrder orderToPlace) { // 1. Validate It (bad? return 400) // 2. Save it to the database - (e.g. do domain stuff) // 3. Return // 201 Created // Location Header with the location of the new resource // A copy of the entity they would get if they did a GET to the location CurbsideOrder response = await CurbsideMapper.PlaceOrder(orderToPlace); return(Ok(response)); }
public async Task <ActionResult <CurbsideOrder> > GetById(int id) { CurbsideOrder response = await CurbsideMapper.GetOrderById(id); if (response == null) { return(NotFound()); } else { return(Ok(response)); } }
public async Task <ActionResult> AddAnOrderSync([FromBody] PostCurbsideOrderRequest orderToPlace) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else { CurbsideOrder response = await _curbsideCommands.AddOrder(orderToPlace, false); return(CreatedAtRoute("curbside#getbyid", new { orderId = response.Id }, response)); } }
public async Task <ActionResult> PlaceOrder([FromBody] CreateCurbsideOrder orderToPlace) { // 1. Validate It (bad? return 400) // 2. Save it to the database - (e.g. do domain stuff) // 3. Return // 201 Created // Location Header with the location of the new resource // A copy of the entity they would get if they did a GET to the location CurbsideOrder response = await CurbsideMapper.PlaceOrder(orderToPlace); await TheChannel.AddCurbsideOrder(new CurbsideChannelRequest { OrderId = response.Id }); // this could possibly throw.. await TheHub.Clients.All.SendAsync("ApiOrderPlaced", response); return(Ok(response)); }
public override async Task <GetCurbsideResponse> PlaceOrder(PostCurbsideRequest request) { var order = new CurbsideOrder { For = request.For, Items = string.Join(",", request.Items), }; _context.CurbsideOrders.Add(order); await _context.SaveChangesAsync(); await _channel.AddCurbside(new CurbsideChannelRequest { OrderId = order.Id }); return(new GetCurbsideResponse { Id = order.Id, For = order.For, Items = order.Items.Split(',').Select(int.Parse).ToArray(), }); }
public async Task <ActionResult> AsyncCurbsideOrders([FromBody] PostSyncCurbsideOrdersRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else { // Save it to the DB var orderToSave = new CurbsideOrder { For = request.For, Items = request.Items, Status = CurbsideOrderStatus.Processing }; // return 201, Location Header, Copy of the Entity _context.CurbsideOrders.Add(orderToSave); await _context.SaveChangesAsync(); var response = new GetCurbsideOrderResponse { Id = orderToSave.Id, For = orderToSave.For, Items = orderToSave.Items, PickupDate = null, Status = orderToSave.Status }; var didWrite = await _channel.AddCurbside(new CurbsideChannelRequest { ReservationId = response.Id }); if (!didWrite) { // what to do? } return(CreatedAtRoute("curbsideorders#getbyid", new { id = response.Id }, response)); } }
public virtual async Task <GetCurbsideResponse> PlaceOrder(PostCurbsideRequest request) { await Task.Delay(1000 *request.Items.Length); var order = new CurbsideOrder { For = request.For, Items = String.Join(",", request.Items), PickupReadyAt = DateTime.Now.AddHours(new Random().Next(2, 5)) }; _context.CurbsideOrders.Add(order); await _context.SaveChangesAsync(); return(new GetCurbsideResponse { Id = order.Id, For = order.For, Items = order.Items.Split(',').Select(int.Parse).ToArray(), PickupReadyAt = order.PickupReadyAt.Value }); }
public async Task <DateTime?> GetPickupDate(CurbsideOrder order) { var request = new PickupService.PickupRequest { For = order.For, }; request.Items.AddRange(order.Items.Split(',').Select(int.Parse).ToArray()); try { var channel = GrpcChannel.ForAddress(_options.Value.Url); var client = new PickupService.PickupEstimator.PickupEstimatorClient(channel); var response = await client.GetPickupTimeAsync(request); return(response.PickupTime.ToDateTime()); } catch (Exception) { //our weak-sauce plan b. return(DateTime.Now.AddDays(5)); } }
public async Task PlaceOrder(PostSyncCurbsideOrdersRequest request) { var orderToSave = new CurbsideOrder { For = request.For, Items = request.Items, Status = CurbsideOrderStatus.Processing }; _context.CurbsideOrders.Add(orderToSave); await _context.SaveChangesAsync(); var response = new GetCurbsideOrderResponse { Id = orderToSave.Id, For = orderToSave.For, Items = orderToSave.Items, PickupDate = null, Status = orderToSave.Status }; await _channel.AddCurbside(new CurbsideChannelRequest { ReservationId = response.Id, ConnectionId = Context.ConnectionId }); await Clients.Caller.SendAsync("OrderPlaced", response); }
public async Task PlaceOrder(PostCurbsideOrderRequest orderToBePlaced) { CurbsideOrder order = await _commands.AddOrderWs(orderToBePlaced, Context.ConnectionId); await Clients.Caller.SendAsync("OrderPlaced", order); }
public async Task <ActionResult> GetOrderById(int orderId) { CurbsideOrder response = await _curbsideQueries.GetById(orderId); return(response == null?NotFound() : (ActionResult)Ok(response)); }