Exemplo n.º 1
0
        public async Task <ActionFeedback> UpdateOrderAsync(OrderFormVm orderRequest)
        {
            if (orderRequest.OrderItems == null || !orderRequest.OrderItems.Any())
            {
                return(ActionFeedback.Fail("Shopping cart cannot be empty"));
            }

            var order = await _orderRepository.Query()
                        .Include(item => item.OrderItems).ThenInclude(item => item.Product)
                        .FirstOrDefaultAsync(item => item.Id == orderRequest.OrderId);

            if (order == null)
            {
                return(ActionFeedback.Fail($"Cannot find order with id {orderRequest.OrderId}"));
            }
            if (!CanEditOrder(order))
            {
                return(ActionFeedback.Fail($"Order has been completed already!"));
            }

            var feedback = await UpdateOrderDetailsAsync(order, orderRequest);

            if (!feedback.Success)
            {
                return(feedback);
            }

            await _orderRepository.SaveChangesAsync();

            return(ActionFeedback.Succeed());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit([FromBody] OrderFormVm orderForm)
        {
            var getOrderFeedback = await _orderService.GetOrderAsync(orderForm.OrderId);

            if (!getOrderFeedback.Success)
            {
                return(BadRequest(new { Error = getOrderFeedback.ErrorMessage }));
            }

            var currentUser = await _workContext.GetCurrentUser();

            var            order    = getOrderFeedback.Result;
            ActionFeedback feedback = ActionFeedback.Succeed();

            if (!CanEditFullOrder(User.IsInRole(RoleName.Admin), currentUser, order.CreatedById, order.VendorId))
            {
                if (order.OrderStatus != orderForm.OrderStatus ||
                    order.TrackingNumber != orderForm.TrackingNumber ||
                    order.PaymentProviderId != orderForm.PaymentProviderId)
                {
                    feedback = await _orderService.UpdateOrderStateAsync(orderForm);
                }
            }
            else
            {
                feedback = await _orderService.UpdateOrderAsync(orderForm);
            }

            return(feedback.Success ? (IActionResult)Accepted() : BadRequest(new { Error = feedback.ErrorMessage }));
        }
Exemplo n.º 3
0
        [InlineData(9, 10, false, OrderFormRestrictedView)]  // Is not owner, cannot edit
        public async void GetOrderFormEdit_WithSellerUser_ShouldReturnView(long userId, long ownerId,
                                                                           bool canEditOrder, string expectedView)
        {
            // Arrange
            const int OrderId  = 1;
            var       mockUser = new Mock <ClaimsPrincipal>();

            mockUser.Setup(user => user.IsInRole(RoleName.Admin)).Returns(false);
            _controller.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext {
                    User = mockUser.Object
                }
            };
            _mockWorkContext.Setup(context => context.GetCurrentUser()).Returns(Task.FromResult(new User {
                Id = userId
            }));

            var order = new GetOrderVm()
            {
                CanEdit     = canEditOrder,
                CreatedById = ownerId
            };
            var orderResult = ActionFeedback <GetOrderVm> .Succeed(order);

            _mockOrderService.Setup(service => service.GetOrderAsync(OrderId)).Returns(Task.FromResult(orderResult));

            // Action
            var result = await _controller.GetOrderFormEdit(OrderId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Equal(expectedView, viewResult.ViewName);
        }
Exemplo n.º 4
0
        public async Task <ActionFeedback <long> > CreateOrderAsync(OrderFormVm orderRequest)
        {
            if (orderRequest.OrderItems == null || !orderRequest.OrderItems.Any())
            {
                return(ActionFeedback <long> .Fail("Shopping cart cannot be empty"));
            }

            var user = await _workContext.GetCurrentUser();

            var order = new Order()
            {
                CreatedById = user.Id
            };

            var feedback = await UpdateOrderDetailsAsync(order, orderRequest);

            if (!feedback.Success)
            {
                return(ActionFeedback <long> .Fail(feedback.ErrorMessage));
            }

            _orderRepository.Add(order);
            await _orderRepository.SaveChangesAsync();

            return(ActionFeedback <long> .Succeed(order.Id));
        }
Exemplo n.º 5
0
        public async Task <ActionFeedback <GetOrderVm> > GetOrderAsync(long orderId)
        {
            var order = await _orderRepository.QueryAsNoTracking()
                        .Include(item => item.OrderItems).ThenInclude(item => item.Product).ThenInclude(item => item.ThumbnailImage)
                        .FirstOrDefaultAsync(item => item.Id == orderId);

            if (order == null)
            {
                return(ActionFeedback <GetOrderVm> .Fail($"Cannot find order with id {orderId}"));
            }

            var user = await _workContext.GetCurrentUser();

            var result = new GetOrderVm
            {
                OrderStatus    = order.OrderStatus,
                CustomerId     = order.CustomerId,
                SubTotal       = order.SubTotal,
                SubTotalCost   = order.OrderItems.Sum(item => item.Quantity * item.Product.Cost),
                ShippingAmount = order.ShippingAmount,
                ShippingCost   = order.ShippingCost,
                Discount       = order.Discount,
                OrderTotal     = order.OrderTotal,
                OrderTotalCost = order.OrderTotalCost,
                TrackingNumber = order.TrackingNumber,
                CreatedById    = order.CreatedById,
                VendorId       = order.VendorId,
                Note           = order.Note,
                IsShopeeOrder  = order.IsShopeeOrder,
                OrderItems     = order.OrderItems
                                 .Select(item => new OrderItemVm
                {
                    Id            = item.Id,
                    ProductId     = item.ProductId,
                    ProductName   = item.Product.Name,
                    ProductSku    = item.Product.Sku,
                    ProductCost   = item.Product.Cost,
                    OriginalPrice = item.Product.Price,
                    ProductPrice  = item.ProductPrice,
                    Stock         = item.Product.Stock,
                    ProductImage  = _mediaService.GetThumbnailUrl(item.Product.ThumbnailImage),
                    Quantity      = item.Quantity
                })
                                 .OrderBy(item => item.Id).ToList(),
                PaymentProviderId   = order.PaymentProviderId,
                PaymentProviderList = (await _paymentProviderService.GetListAsync(true))
                                      .Select(item => new SelectListItem
                {
                    Value = item.Id.ToString(),
                    Text  = item.Description
                }).ToList(),
                CanEdit = CanEditOrder(order)
            };

            return(ActionFeedback <GetOrderVm> .Succeed(result));
        }
Exemplo n.º 6
0
        public ActionFeedback <IEnumerable <ImportingOrderDto> > Parse(Stream fileStream)
        {
            try
            {
                var result = new List <ImportingOrderDto>();

                using (var reader = ExcelReaderFactory.CreateReader(fileStream))
                {
                    var sheets = reader.AsDataSet();

                    if (!sheets.Tables.Cast <object>().Any())
                    {
                        return(ActionFeedback <IEnumerable <ImportingOrderDto> > .Succeed(result));
                    }

                    sheets.Tables[0].Rows.RemoveAt(0); // Remove header row
                    foreach (DataRow row in sheets.Tables[0].Rows)
                    {
                        var externalId = row[ColumnIndex.ExternalId].ToString();
                        if (externalId.HasValue())
                        {
                            var dto = new ImportingOrderDto
                            {
                                ExternalId      = externalId,
                                OrderedDate     = DateTime.Parse(row[ColumnIndex.OrderedDate].ToString()),
                                Status          = OrderStatusMapping[row[ColumnIndex.Status].ToString()],
                                TrackingNumber  = row[ColumnIndex.TrackingNumber].ToString(),
                                Sku             = row[ColumnIndex.Sku].ToString(),
                                Price           = decimal.Parse(row[ColumnIndex.Price].ToString()),
                                Quantity        = int.Parse(row[ColumnIndex.Quantity].ToString()),
                                ShippingCost    = decimal.Parse(row[ColumnIndex.ShippingCost].ToString()),
                                ShippingAmount  = decimal.Parse(row[ColumnIndex.ShippingAmount].ToString()),
                                OrderTotal      = decimal.Parse(row[ColumnIndex.OrderTotal].ToString()),
                                Username        = row[ColumnIndex.Username].ToString(),
                                Phone           = row[ColumnIndex.Phone].ToString(),
                                ShippingAddress = row[ColumnIndex.ShippingAddress].ToString()
                            };
                            result.Add(dto);
                        }
                    }
                }

                return(ActionFeedback <IEnumerable <ImportingOrderDto> > .Succeed(result));
            }
            catch (Exception exception)
            {
                _telemetry.TrackException(exception);

                return(ActionFeedback <IEnumerable <ImportingOrderDto> > .Fail(exception.Message));
            }
        }
Exemplo n.º 7
0
        public async Task <ActionFeedback> UpdateOrderStateAsync(OrderFormVm orderRequest)
        {
            var order = await _orderRepository.Query()
                        .Include(item => item.OrderItems).ThenInclude(item => item.Product)
                        .FirstOrDefaultAsync(item => item.Id == orderRequest.OrderId);

            if (order == null)
            {
                return(ActionFeedback.Fail($"Cannot find order with Id: {orderRequest.OrderId}"));
            }
            if (!CanEditOrder(order))
            {
                return(ActionFeedback.Fail($"Order has been completed already!"));
            }

            order.TrackingNumber    = orderRequest.TrackingNumber;
            order.PaymentProviderId = orderRequest.PaymentProviderId;
            await UpdateStatusAndOrderTotalAsync(order, orderRequest.OrderStatus);

            await _orderRepository.SaveChangesAsync();

            return(ActionFeedback.Succeed());
        }
Exemplo n.º 8
0
        public async Task <ActionFeedback> UpdateTrackingNumberAsync(long orderId, string trackingNumber)
        {
            var order = await _orderRepository.Query().FirstOrDefaultAsync(x => x.Id == orderId);

            if (order == null)
            {
                return(ActionFeedback.Fail($"Cannot find order with Id: {orderId}"));
            }
            if (!CanEditOrder(order))
            {
                return(ActionFeedback.Fail($"Order has been completed already!"));
            }

            var feedback = UpdateTrackingNumber(order, trackingNumber);

            if (!feedback.Success)
            {
                return(feedback);
            }

            await _orderRepository.SaveChangesAsync();

            return(ActionFeedback.Succeed());
        }
Exemplo n.º 9
0
        protected override async Task <ActionFeedback <ImportResult> > ImportAsync(IEnumerable <ImportingOrderDto> data)
        {
            var importResult = await _orderImportService.ImportAsync(data);

            return(ActionFeedback <ImportResult> .Succeed(importResult));
        }