Пример #1
0
        public HttpResponseMessage PutOrder(int id, OrderDto orderDto)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != orderDto.OrderId)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            Order order = orderDto.ToEntity();

            if (db.Entry(order).Entity.UserId != User.Identity.Name)
            {
                // Trying to modify a record that does not belong to the user
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }
            db.Entry(order).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Пример #2
0
        public virtual async Task <ApiResult <Order> > CreateOrder(OrderDto orderDto, CancellationToken cancellationToken)
        {
            //var order = Mapper.Map<Order>(orderDto);
            var order = orderDto.ToEntity();
            await orderRepository.AddAsync(order, cancellationToken);

            return(order);
        }
        public async Task PayAsync(OrderDto orderDto)
        {
            var  order   = orderDto.ToEntity();
            bool success = await _repository.PayAsync(order);

            if (!success)
            {
                throw new Exception("Order does not exist.");
            }
        }
Пример #4
0
        public virtual async Task <ApiResult> UpdateOrderBySpecialCode(OrderDto orderDto, CancellationToken cancellationToken)
        {
            var updateOrder = await orderRepository.GetBySpecialCode(orderDto.SpecialCode, cancellationToken);

            //Mapper.Map(orderDto, updateOrder);
            updateOrder = orderDto.ToEntity(updateOrder);
            await orderRepository.UpdateAsync(updateOrder, cancellationToken);

            return(Ok());
        }
        public async Task <OrderDto> UpdateOrderAsync(OrderDto orderDto)
        {
            var order   = orderDto.ToEntity();
            var updated = await _repository.UpdateOrderAsync(order);

            if (updated == null)
            {
                throw new Exception("Order does not exist");
            }
            return(new OrderDto(updated));
        }
        public void Handle(DbContext dbContext, OrderDto dto)
        {
            var entity = dto.ToEntity();

            dbContext.Add(entity);
            dbContext.SaveChanges();

            var orderPlacedEvent = entity.ToOrderPlacedEvent();

            _orderPlacedPublisher.Publish(orderPlacedEvent);
        }
        public async Task <OrderDto> CreateOrderAsync(OrderDto orderDto)
        {
            var order = orderDto.ToEntity();

            order.Date = DateTime.Now;
            var created = await _repository.AddOrderAsync(order);

            if (created == null)
            {
                throw new Exception("Order already exists.");
            }
            return(new OrderDto(created));
        }
Пример #8
0
        public async Task <OrderDto> CreateOrderAsync(OrderDto model)
        {
            var entity = await _orderRepository
                         .InsertAsync(model.ToEntity());

            foreach (var item in model.OrderDetails)
            {
                item.OrderId = entity.Id;
                await _orderDetailRepository.InsertAsync(item.ToEntity());
            }

            return(entity.ToViewModel());
        }
Пример #9
0
        public IHttpActionResult Post([FromBody] OrderDto order)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            order.Id = _orderService.AddOrder(order.ToEntity());
            if (order.Id > 0)
            {
                string uri = Url.Link("DefaultApi", new { id = order.Id });
                return(Created(uri, order));
            }

            return(BadRequest());
        }
Пример #10
0
        public IHttpActionResult Put(int id, [FromBody] OrderDto Order)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var entity = _orderService.GetOrder(id);

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

            _orderService.UpdateOrder(id, Order.ToEntity(entity));

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #11
0
        public HttpResponseMessage PostOrder(OrderDto orderDto)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            orderDto.UserId = User.Identity.Name;
            Order order = orderDto.ToEntity();

            db.Orders.Add(order);
            db.SaveChanges();
            orderDto.OrderId = order.OrderId;

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, orderDto);

            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = orderDto.OrderId }));
            return(response);
        }
Пример #12
0
        public virtual IActionResult PlaceOrder(OrderDto order)
        {
            if (order == null)
            {
                Log(LogLevel.Error, "Null order object");

                return(new BadRequestResult());
            }

            var item       = order.ToEntity();
            var orderitems = order.OrderItems.Select(oi => oi.ToEntity()).ToList();

            //Calculate order amount based on order items prices and quantity
            decimal orderAmount = orderitems.Sum(x => x.Price * x.Quantity);

            item.Amount = orderAmount;
            _orderService.InsertOrder(item);

            Log(LogLevel.Information, "The order has been placed successfully.");

            return(new JsonResult(item.ToDto()));
        }