public async Task <IActionResult> Get(string login)
        {
            CustomerPublicInfo customer = new CustomerPublicInfo();
            var role = GetCurrentUserRoleId();

            customer = (CustomerPublicInfo)(await this.publicRepo.ExecuteOperationAsync("GetCustomerByName", new[] { new KeyValuePair <string, object>("login", login) }));
            if (role == 3)
            {
                var userId = GetCurrentUserId();
                if (userId != ((CustomerInfo)(await this.repo.ExecuteOperationAsync("GetCustomerByName", new[] { new KeyValuePair <string, object>("login", login) }))).UserId || customer == null)
                {
                    return(NotFound());
                }
            }
            return(Ok(customer));
        }
        public async Task <IActionResult> GetOrdersByCustomerId(int id)
        {
            List <int>   orderIds = new List <int>();
            List <Order> orders   = new List <Order>();
            int          customerId;
            var          userId = GetCurrentUser();

            using (var customerClient = InitializeClient("http://localhost:5001/"))
            {
                HttpResponseMessage resp = await customerClient.GetAsync("/api/customers/users/" + userId);

                if (!resp.IsSuccessStatusCode)
                {
                    return(NotFound());
                }
                CustomerPublicInfo customer = (CustomerPublicInfo)((await resp.Content.ReadAsAsync(typeof(CustomerPublicInfo))));
                customerId = customer.Id;
            }
            if (id == customerId)
            {
                orderIds = (List <int>) await this.repo.ExecuteOperationAsync("GetOrdersByCustomerId", new[] { new KeyValuePair <string, object>("id", id) });

                if (orderIds.Count == 0)
                {
                    return(Ok("No orders"));
                }
                using (var orderClient = InitializeClient("http://localhost:5005/"))
                {
                    foreach (var orderid in orderIds)
                    {
                        HttpResponseMessage response = await orderClient.GetAsync("/api/orders/" + orderid);

                        if (!response.IsSuccessStatusCode)
                        {
                            return(NotFound());
                        }
                        orders.Add((Order)((await response.Content.ReadAsAsync(typeof(Order)))));
                    }
                }
                return(Ok(orders));
            }
            return(NotFound());
        }
示例#3
0
        public async Task <IActionResult> Post([FromBody] int catalogId)
        {
            int currentCustomerId;
            var userId = GetCurrentUser();

            using (var customerClient = InitializeClient("http://localhost:5001/"))
            {
                HttpResponseMessage response = await customerClient.GetAsync("/api/customers/users/" + userId);

                if (!response.IsSuccessStatusCode)
                {
                    return(NotFound());
                }
                CustomerPublicInfo customer = (CustomerPublicInfo)((await response.Content.ReadAsAsync(typeof(CustomerPublicInfo))));
                currentCustomerId = customer.Id;
            }
            var res = await this.repo.ExecuteOperationAsync("AddToShopCart", new[] { new KeyValuePair <string, object>("CustomerId", currentCustomerId), new KeyValuePair <string, object>("CatalogId", catalogId) });

            if (res == null)
            {
                return(NotFound());
            }
            return(Ok());
        }
        public async Task <IActionResult> Post([FromBody] JToken jsonbody)
        { // don't forget ro delete product
            int orderId, customerId, quantity, catalogId;

            using (var orderClient = InitializeClient("http://localhost:5005/"))
            {
                HttpContent         content  = new StringContent(jsonbody.ToString(), Encoding.UTF8, "application/json");
                HttpResponseMessage response = await orderClient.PostAsync("/api/orders/", content);

                if (!response.IsSuccessStatusCode)
                {
                    return(NotFound());
                }
                var temp = JsonConvert.DeserializeObject <int>(await response.Content.ReadAsStringAsync());
                Int32.TryParse(temp.ToString(), out orderId);
                if (orderId == 0)
                {
                    return(NotFound());
                }
                response = await orderClient.GetAsync("/api/orders/quantity/" + orderId);

                temp = JsonConvert.DeserializeObject <int>(await response.Content.ReadAsStringAsync());
                Int32.TryParse(temp.ToString(), out quantity);
                if (quantity == 0)
                {
                    return(NotFound());
                }
                response = await orderClient.GetAsync("/api/orders/catalog/" + orderId);

                temp = JsonConvert.DeserializeObject <int>(await response.Content.ReadAsStringAsync());
                Int32.TryParse(temp.ToString(), out catalogId);
                if (catalogId == 0)
                {
                    return(NotFound());
                }
            }
            var userId = GetCurrentUser();

            using (var customerClient = InitializeClient("http://localhost:5001/"))
            {
                HttpResponseMessage response = await customerClient.GetAsync("/api/customers/users/" + userId);

                if (!response.IsSuccessStatusCode)
                {
                    return(NotFound());
                }
                CustomerPublicInfo customer = (CustomerPublicInfo)((await response.Content.ReadAsAsync(typeof(CustomerPublicInfo))));
                customerId = customer.Id;
            }
            using (var productClient = InitializeClient("http://localhost:5002/"))
            {
                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("quantity", quantity.ToString())
                });
                HttpResponseMessage response = await productClient.PutAsync("/api/products/quantity/" + catalogId, content);

                if (!response.IsSuccessStatusCode)
                {
                    return(NotFound());
                }
            }
            var res = await this.repo.ExecuteOperationAsync("AddCustomerOrder", new[] { new KeyValuePair <string, object>("customerId", customerId), new KeyValuePair <string, object>("orderId", orderId) });

            if (res == null)
            {
                return(NotFound());
            }
            return(Ok(200));
        }