Esempio n. 1
0
        public async Task RentByFamilyRentTypeMoreThanOneMethodSelected()
        {
            HttpConfiguration config = new HttpConfiguration();

            WebApiConfig.Register(config);
            using (HttpServer server = new HttpServer(config))
            {
                HttpClient client = new HttpClient(server);

                string url = "http://localhost/api/rental/RentByFamily";

                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url))
                {
                    RentByFamilyRequest rent = new RentByFamilyRequest();
                    rent.RentByHourRequest = new RentByHourRequest()
                    {
                        Hours = 1
                    };
                    rent.RentByWeekRequest = new RentByWeekRequest()
                    {
                        Weeks = 1
                    };
                    rent.FamilyMembers = 1;

                    request.Content = new StringContent(JsonConvert.SerializeObject(rent), Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
                    }
                }
            }
        }
Esempio n. 2
0
        public async Task RentByFamilyOk()
        {
            HttpConfiguration config = new HttpConfiguration();

            WebApiConfig.Register(config);
            using (HttpServer server = new HttpServer(config))
            {
                HttpClient client = new HttpClient(server);

                string url = "http://localhost/api/rental/RentByFamily";

                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url))
                {
                    RentByFamilyRequest rent = new RentByFamilyRequest();
                    rent.FamilyMembers     = 4;
                    rent.RentByHourRequest = new RentByHourRequest()
                    {
                        Hours = 23
                    };

                    request.Content = new StringContent(JsonConvert.SerializeObject(rent), Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        BikeRentalInvoice invoice = response.Content.ReadAsAsync <BikeRentalInvoice>().Result;

                        Assert.IsTrue(invoice.InvoiceId != null && invoice.ReturnDate != DateTime.MinValue && invoice.Total > 0);
                    }
                }
            }
        }
Esempio n. 3
0
        public IHttpActionResult RentByFamily(RentByFamilyRequest rentByFamilyRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            int numberOfSelectedRentalTypes = 0;

            if (rentByFamilyRequest.RentByHourRequest != null)
            {
                numberOfSelectedRentalTypes++;
            }
            if (rentByFamilyRequest.RentByDayRequest != null)
            {
                numberOfSelectedRentalTypes++;
            }
            if (rentByFamilyRequest.RentByWeekRequest != null)
            {
                numberOfSelectedRentalTypes++;
            }

            if (numberOfSelectedRentalTypes == 0)
            {
                return(BadRequest("You have to select one of the rental options"));
            }

            if (numberOfSelectedRentalTypes != 1)
            {
                return(BadRequest("You can´t select more than one rental option"));
            }

            return(Ok(Dao.RentByFamily(rentByFamilyRequest)));
        }
Esempio n. 4
0
        public BikeRentalInvoice RentByFamily(RentByFamilyRequest rentByFamilyRequest)
        {
            BikeRentalInvoice invoice = new BikeRentalInvoice();

            BikeRentalType bikeRentalType;
            int            number;

            if (rentByFamilyRequest.RentByHourRequest != null)
            {
                RentByHourRequest rentByHourRequest = rentByFamilyRequest.RentByHourRequest;

                bikeRentalType = BikeRentalType.Hour;
                number         = rentByHourRequest.Hours;
            }
            else if (rentByFamilyRequest.RentByDayRequest != null)
            {
                RentByDayRequest rentByDayRequest = rentByFamilyRequest.RentByDayRequest;

                bikeRentalType = BikeRentalType.Day;
                number         = rentByDayRequest.Days;
            }
            else if (rentByFamilyRequest.RentByWeekRequest != null)
            {
                RentByWeekRequest rentByDayRequest = rentByFamilyRequest.RentByWeekRequest;

                bikeRentalType = BikeRentalType.Week;
                number         = rentByDayRequest.Weeks;
            }
            else
            {
                throw new NotImplementedException();
            }

            invoice.ReturnDate = CalculateReturnDate(number, bikeRentalType);
            invoice.Total      = CalculateTotal(number, bikeRentalType, FamilyDiscountPercent);
            invoice.InvoiceId  = CreateInvoiceId();
            return(invoice);
        }