示例#1
0
        public void GetSpecialtyPizzaByID_InvalidID()
        {
            #region ASSIGN

            PizzaTypesRepo          testRepo       = new PizzaTypesRepo();
            PizzaTypesAPIController testController = new PizzaTypesAPIController(testRepo);

            #endregion

            #region ACT

            var taskReturn = testController.GetSpecialtyPizza(3);
            taskReturn.Wait();
            var result = taskReturn.Result.Value;

            SpecialtyPizza testData = result;

            #endregion

            #region ASSERT

            Assert.AreNotEqual(testData.ID, 1);
            Assert.AreNotEqual(testData.Name, "Special A");
            Assert.AreNotEqual(testData.Description, "TopA,TopB,TopC");
            Assert.AreNotEqual(testData.Price, 10.99M);

            #endregion
        }
        public async Task <SpecialtyPizza> GetSpecialtyPizza(int id)
        {
            SpecialtyPizza result = null;

            var query = specialtyPizzaList.Where(s => s.ID == id);
            await Task.Delay(10);

            if (query.Count() > 0)
            {
                result = query.FirstOrDefault();
            }

            return(result);
        }
        public async Task <SpecialtyPizza> GetSpecialtyPizza(int id)
        {
            SpecialtyPizza result = null;

            var query = _context.SpecialtyPizzas.Where(c => c.ID == id);

            if (query.Count() > 0)
            {
                result = await query.FirstOrDefaultAsync();
            }
            else
            {
                throw new NullReferenceException("EMPTY QUERY IN SPECIALTY PIZZA!");
            }

            return(result);
        }
示例#4
0
        protected async Task <Object> GetPizzaOrderTask(string Username, int?id)
        {
            //Instantiate Objects
            Customer       customer       = new Customer();
            Orders         order          = new Orders();
            SpecialtyPizza specialtyPizza = new SpecialtyPizza();
            OrderDetail    orderDetail    = new OrderDetail();


            //Consume API calls
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://chajdpizza.azurewebsites.net/api/");

                //GetCustomerId
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add
                    (new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage ResC = await client.GetAsync("CustomersApi/ByUser/" + Username);

                if (ResC.IsSuccessStatusCode)
                {
                    var customerRes = ResC.Content.ReadAsStringAsync().Result;

                    customer = JsonConvert.DeserializeObject <Customer>(customerRes);
                }
                else if (!ResC.IsSuccessStatusCode)
                {
                    return(new Exception("Get Customer has failed!"));
                }

                var custId = customer.Id;

                //Check if Customer has multiple open orders
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add
                    (new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage ResCh = await client.GetAsync("OrdersApi/CheckMultByCust/" + custId);

                var isMult = 0;
                if (ResCh.IsSuccessStatusCode)
                {
                    var CheckRes = ResCh.Content.ReadAsStringAsync().Result;

                    isMult = JsonConvert.DeserializeObject <int>(CheckRes);
                }
                else if (!ResCh.IsSuccessStatusCode)
                {
                    return(new Exception("Check mult has failed!"));
                }
                if (isMult > 1)
                {
                    return(new Exception("There are multiple open orders for this customer."));
                }


                if (isMult == 0)
                {
                    order.CustomerId  = custId;
                    order.isCompleted = false;
                    //Post new Order
                    client.DefaultRequestHeaders.Clear();
                    client.DefaultRequestHeaders.Accept.Add
                        (new MediaTypeWithQualityHeaderValue("application/json"));

                    var newData    = JsonConvert.SerializeObject(order);
                    var newContent = new StringContent(newData, Encoding.UTF8, "application/json");
                    HttpResponseMessage ResPost = await client.PostAsync("OrdersApi", newContent);

                    if (!ResPost.IsSuccessStatusCode)
                    {
                        return(new Exception("Post new Order has failed!"));
                    }
                }

                //Get orders by CustId
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add
                    (new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage ResO = await client.GetAsync("OrdersApi/ByCust/" + custId);

                if (ResO.IsSuccessStatusCode)
                {
                    var ordersRes = ResO.Content.ReadAsStringAsync().Result;

                    order = JsonConvert.DeserializeObject <Orders>(ordersRes);
                }
                else if (!ResO.IsSuccessStatusCode)
                {
                    return(new Exception("Get order has failed!"));
                }



                if (!(id is null))
                {
                    //Get SpecialtyPizzaDetails
                    client.DefaultRequestHeaders.Clear();
                    client.DefaultRequestHeaders.Accept.Add
                        (new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage ResP = await client.GetAsync("PizzaTypesApi/special/" + id);

                    if (ResP.IsSuccessStatusCode)
                    {
                        var SpecialRes = ResP.Content.ReadAsStringAsync().Result;

                        specialtyPizza = JsonConvert.DeserializeObject <SpecialtyPizza>(SpecialRes);
                    }
                    else if (!ResP.IsSuccessStatusCode)
                    {
                        return(new Exception("Get Specialty Pizza Details has failed!"));
                    }

                    orderDetail.Price            = specialtyPizza.Price;
                    orderDetail.SizeId           = 2;
                    orderDetail.ToppingsSelected = specialtyPizza.Description;
                    orderDetail.Id = specialtyPizza.ID;
                }

                orderDetail.OrdersId = order.Id;
                return(orderDetail);
            }
        }