Exemplo n.º 1
0
        public async Task <BasketServiceModel> GetBasketById(GetBasketByIdServiceModel serviceModel)
        {
            var basket = await this.basketRepository.GetBasketAsync(serviceModel.Id.Value);

            if (basket == null)
            {
                var emptyBasket = new BasketServiceModel
                {
                    Id    = serviceModel.Id.Value,
                    Items = Array.Empty <BasketItemServiceModel>()
                };
                return(emptyBasket);
            }

            var response = new BasketServiceModel
            {
                Id    = basket.Id.Value,
                Items = basket.Items.OrEmptyIfNull().Select(x => new BasketItemServiceModel
                {
                    ProductId         = x.ProductId,
                    ProductSku        = x.ProductSku,
                    ProductName       = x.ProductName,
                    PictureUrl        = x.PictureUrl,
                    Quantity          = x.Quantity,
                    ExternalReference = x.ExternalReference,
                    DeliveryFrom      = x.DeliveryFrom,
                    DeliveryTo        = x.DeliveryTo,
                    MoreInfo          = x.MoreInfo
                })
            };

            return(response);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Get(Guid id)
        {
            var sellerClaim  = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim);
            var serviceModel = new GetBasketByIdServiceModel
            {
                Id             = id,
                Language       = CultureInfo.CurrentCulture.Name,
                Username       = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value,
                OrganisationId = GuidHelper.ParseNullable(sellerClaim?.Value)
            };

            var validator        = new GetBasketByIdModelValidator();
            var validationResult = await validator.ValidateAsync(serviceModel);

            if (validationResult.IsValid)
            {
                var basket = await this.basketService.GetBasketById(serviceModel);

                if (basket != null)
                {
                    var response = new BasketResponseModel
                    {
                        Id    = basket.Id,
                        Items = basket.Items.OrEmptyIfNull().Select(x => new BasketItemResponseModel
                        {
                            ProductId         = x.ProductId,
                            ProductSku        = x.ProductSku,
                            ProductName       = x.ProductName,
                            PictureUrl        = x.PictureUrl,
                            Quantity          = x.Quantity,
                            ExternalReference = x.ExternalReference,
                            DeliveryFrom      = x.DeliveryFrom,
                            DeliveryTo        = x.DeliveryTo,
                            MoreInfo          = x.MoreInfo
                        })
                    };

                    return(this.StatusCode((int)HttpStatusCode.OK, response));
                }
            }

            throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
        }