public async Task RentByHourOk() { HttpConfiguration config = new HttpConfiguration(); WebApiConfig.Register(config); using (HttpServer server = new HttpServer(config)) { HttpClient client = new HttpClient(server); string url = "http://localhost/api/rental/RentByHour"; using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)) { RentByHourRequest rent = new RentByHourRequest(); rent.Hours = 4; 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); } } } }
public IHttpActionResult RentByHour(RentByHourRequest rentByHourRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(Ok(Dao.RentByHour(rentByHourRequest))); }
public BikeRentalInvoice RentByHour(RentByHourRequest rentByHourRequest) { BikeRentalInvoice invoice = new BikeRentalInvoice(); invoice.ReturnDate = CalculateReturnDate(rentByHourRequest.Hours, BikeRentalType.Hour); invoice.Total = CalculateTotal(rentByHourRequest.Hours, BikeRentalType.Hour, 0); invoice.InvoiceId = CreateInvoiceId(); return(invoice); }
public async Task RentByHourBadRequest() { HttpConfiguration config = new HttpConfiguration(); WebApiConfig.Register(config); using (HttpServer server = new HttpServer(config)) { HttpClient client = new HttpClient(server); string url = "http://localhost/api/rental/RentByHour"; using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)) { RentByHourRequest rent = new RentByHourRequest(); request.Content = new StringContent(JsonConvert.SerializeObject(rent), Encoding.UTF8, "application/json"); using (HttpResponseMessage response = await client.SendAsync(request)) { Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); } } } }
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); }