Esempio n. 1
0
        public async Task <IActionResult> Post([FromBody] NewOrderDto newOrderDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _orderId++;

            var pickupPosition = await GetByAddress(newOrderDto.PickUp);

            if (pickupPosition == null)
            {
                return(BadRequest($"address = \"{newOrderDto.PickUp}\"  not found"));
            }

            var dropoffPosition = await GetByAddress(newOrderDto.DropOff);

            if (dropoffPosition == null)
            {
                return(BadRequest($"address = \"{newOrderDto.DropOff}\" address not found"));
            }

            var newOrder = new Order
            {
                Id     = _orderId.ToString(),
                Status = OrderStatus.unassigned,
                Pickup = new GLocation
                {
                    Address  = newOrderDto.PickUp,
                    Position = pickupPosition,
                },
                Dropoff = new GLocation
                {
                    Address  = newOrderDto.DropOff,
                    Position = dropoffPosition,
                }
            };

            Orders.Add(newOrder);

            BrowserSocket.SendToAllAsync(new OrdersSocketMessage
            {
                Payload = Orders
            });

            MobileSocket.SendToAllMobileSockets(new OrderAvailablePayload
            {
                Payload = newOrder
            });

            return(Ok(newOrder));
        }
Esempio n. 2
0
        public void Delete(string id)
        {
            var order = Orders.SingleOrDefault(o => o.Id == id.ToString());

            if (order == null)
            {
                return;
            }

            Orders.Remove(order);
            BrowserSocket.SendToAllAsync(new OrdersSocketMessage
            {
                Payload = Orders
            });
        }
Esempio n. 3
0
        public async Task <IActionResult> Put(int id, [FromBody] Order updOrder)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            OrderStatus newStatus = updOrder.Status;
            var         order     = Orders.SingleOrDefault(o => o.Id == id.ToString());

            if (order == null)
            {
                return(NotFound());
            }

            var rule = StatusRules[order.Status];

            if (rule.IsOwned && order.Driver_id != updOrder.Driver_id)
            {
                return(BadRequest($"access denied"));
            }

            if (!rule.Next.Contains(newStatus))
            {
                return(BadRequest($"only {rule.ShowNext()} after {order.Status}"));
            }

            order.Status     = updOrder.Status;
            order.Driver_id  = updOrder.Driver_id;
            updOrder.Pickup  = order.Pickup;
            updOrder.Dropoff = order.Dropoff;
            updOrder.Id      = order.Id;

            BrowserSocket.SendToAllAsync(new OrdersSocketMessage
            {
                Payload = Orders
            });

            return(Ok(updOrder));
        }