Ejemplo n.º 1
0
        public async Task <AddItemResponse> AddItem(AddItemRequest item)
        {
            string url         = $"/item/create";
            var    requestBody = await Task.Run(() => JsonConvert.SerializeObject(item));

            using (HttpClient httpClient = new HttpClient())
            {
                AddItemResponse data = new AddItemResponse();
                try
                {
                    var authHeader = new AuthenticationHeaderValue("Bearer", await SecureStorage.GetAsync("auth_token"));
                    httpClient.DefaultRequestHeaders.Authorization = authHeader;
                    httpClient.BaseAddress = new Uri(Constants.BaseUrl);
                    StringContent       content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                    HttpResponseMessage result  = await httpClient.PostAsync(url, content);

                    string response = await result.Content.ReadAsStringAsync();

                    data = JsonConvert.DeserializeObject <AddItemResponse>(response);

                    if (result.IsSuccessStatusCode && result.StatusCode == HttpStatusCode.OK)
                    {
                        return(data);
                    }

                    return(null);
                }
                catch (Exception exp)
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 2
0
        public ActionResult <AddItemResponse> AddItemToBasket([FromBody] AddItemRequest value)
        {
            if (!ValidateRequest())
            {
                return(BadRequest(new AddItemResponse()
                {
                    OriginalRequest = value, Failure = new RequestFailure(Error)
                }));
            }

            try
            {
                AddItemRequest request       = value;
                bool           resetQuantity = request.ResetQuantity;

                var item = this.Basket.Products.Where(x => x.ExternalId == request.ItemId).FirstOrDefault();
                if (item != null)
                {
                    if (resetQuantity)
                    {
                        item.Quantity = request.Quantity;
                    }
                    else
                    {
                        item.Quantity += request.Quantity;
                    }
                }
                else if (request.Quantity > 0)
                {
                    this.Basket.Products.Add(new Product()
                    {
                        Price = request.Price, ExternalId = request.ItemId, Quantity = request.Quantity
                    });
                }

                this.DbContext.Save();

                AddItemResponse response = new AddItemResponse()
                {
                    CustomerId      = Customer.CustomerId,
                    SessionId       = Customer.SessionId,
                    OriginalRequest = request,
                    Items           = this.Basket.GetBasketItems(),
                    TotalPrice      = Basket.Products.Sum(x => x.Price)
                };

                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(BadRequest(new AddItemResponse()
                {
                    OriginalRequest = value, Failure = new RequestFailure(ex.Message)
                }));
            }
        }
        public static AddItemResponse Unmarshall(UnmarshallerContext context)
        {
            AddItemResponse addItemResponse = new AddItemResponse();

            addItemResponse.HttpResponse = context.HttpResponse;
            addItemResponse.RequestId    = context.StringValue("AddItem.RequestId");
            addItemResponse.Success      = context.BooleanValue("AddItem.Success");
            addItemResponse.Message      = context.StringValue("AddItem.Message");
            addItemResponse.Code         = context.IntegerValue("AddItem.Code");

            return(addItemResponse);
        }
Ejemplo n.º 4
0
 public static AuctionHandle ToDomain(string lotId, AddItemResponse r) =>
 r.AddItemResult is QueuedRequestResponse x