public IActionResult CreateOrder([FromBody] OrderToCreateDto orderToCreate)
        {
            try
            {
                if (orderToCreate == null)
                {
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    return(new UnprocessableEntityObjectResult(ModelState));
                }

                var orderEntity = Mapper.Map <Order>(orderToCreate);
                _repo.AddOrder(orderEntity);
                if (!_repo.Save())
                {
                    throw new Exception("Error creating order.");
                }

                var eventMessage = new OrderCreatedIntegrationEvent(orderEntity);
                _eventBus.Publish(eventMessage);

                var orderToReturn = Mapper.Map <OrderDto>(orderEntity);
                return(CreatedAtRoute("GetOrder", new { id = orderToReturn.Id }, orderToReturn));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
예제 #2
0
        public async Task <IActionResult> CreateOrder([FromBody] OrderToCreateDto orderToCreate)
        {
            if (orderToCreate == null)
            {
                return(BadRequest());
            }

            //basically dont wanna have to worry about deserializing 2 possible types of response object, so just gonna do the validation here
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            MemoryStream orderProtoStream = new MemoryStream();

            Serializer.Serialize(orderProtoStream, orderToCreate);
            ByteArrayContent bArray = new ByteArrayContent(orderProtoStream.ToArray());

            var orderResponse = await _client.PostAsync(_client.BaseAddress, bArray);

            if (orderResponse.IsSuccessStatusCode)
            {
                var ordersStream = await orderResponse.Content.ReadAsStreamAsync();

                var order = Serializer.Deserialize <OrderDto>(ordersStream);
                if (order != null)
                {
                    return(StatusCode((int)orderResponse.StatusCode, order));
                }
            }
            return(StatusCode((int)orderResponse.StatusCode));
        }
예제 #3
0
        public async Task <IActionResult> MakeOrder(OrderToCreateDto orderToCreate)
        {
            var order        = _mapper.Map <Order>(orderToCreate);
            var createdOrder = await _repo.MakeOrder(order);

            var orderToReturn = _mapper.Map <OrderToReturnDto>(createdOrder);

            return(Ok(orderToReturn));
        }
예제 #4
0
        public async Task <ActionResult <Order> > UpdateOrderAdmin(int id, OrderToCreateDto orderDto)
        {
            var email = HttpContext.User.RetrieveEmailFromPrincipal();
            var order = await _orderSevice.UpdateOrderAdminAsync(id, email, orderDto.DeliveryMethodId, _mapper.Map <List <OrderItemDto>, List <BasketItem> >(orderDto.OrderItems), orderDto.ShipToAddress);

            if (order == null)
            {
                return(BadRequest(new ApiResponse(400, "Problem creating order")));
            }
            return(Ok(order));
        }