public void CreateRentalWillCreateRentalObject() { //Arrange var testHelper = new TestHelper(); var testData = testHelper.CreateEquipmentList(); IRentalRepository rentalRepository = Substitute.For<IRentalRepository>(); IUnitOfWork uow = Substitute.For<IUnitOfWork>(); uow.RentalRepository.Returns(rentalRepository); RentalService rs = new RentalService(uow); Rental rental = new Rental(); rental.CustomerName = "Johan Eriksson"; rental.CustomerPhoneNumber = "0705556677"; rental.StartTime = DateTime.Now; rental.EquipmentId = testData[0].Id; RentalPlan rp = new RentalPlan(); rp.Id = 1; rp.Duration = 8; rp.Discount = 0; rental.RentalPlanId = rp.Id; //Act rs.CreateRental(rental); //Assert rentalRepository.Received().CreateRental(rental); }
public async Task ReturnRentalAsync(Rental rental) { // Update the server. await restService.DeleteAsync("api/rentals/" + rental.Id); // Update the local cache (if the server was updated successfully). rentals.Remove(rentals.Where(t => t.Rental.Id == rental.Id).Single()); availableEquipment.Add(rental.Equipment); }
private async void Rent_Executed(object sender, ExecutedRoutedEventArgs e) { try { var rental = new Rental { EquipmentId = ViewModel.Equipment.Id, CustomerName = ViewModel.CustomerName, CustomerPhoneNumber = ViewModel.CustomerPhoneNumber, StartTime = DateTime.Now, RentalPlanId = ViewModel.SelectedRentalPlan.Id, Equipment = ViewModel.Equipment, RentalPlan = ViewModel.SelectedRentalPlan }; await RentalService.CreateRentalAsync(rental); Close(); } catch (HttpStatusException exception) { MessageBox.Show("Failed to rent item.", "Error", MessageBoxButton.OK, MessageBoxImage.Stop); } }
public CreateRentalResult CreateRental(Rental rental) { if(uow.RentalRepository.GetRentalByEquipmentId(rental.EquipmentId) == null) { uow.RentalRepository.CreateRental(rental); uow.Save(); return CreateRentalResult.Success; } else { return CreateRentalResult.EquipmentIsAlreadyRented; } }
public void CreateRentalShouldStoreRental() { int eqid; int rpid; using (var context = new SkiRentalContext()) { var uow = new UnitOfWork(context); Equipment e1 = new Equipment(); e1.ProductName = "EQtest1"; e1.EquipmentTypeId = 1; e1.EquipmentStateId = 1; e1.PurchaseDate = DateTime.Now; e1.PurchasePrice = 500; RentalPlan rp1 = new RentalPlan(); rp1.Duration = 8; rp1.Discount = 0.25; context.Equipments.Add(e1); context.RentalPlans.Add(rp1); uow.Save(); eqid = e1.Id; rpid = rp1.Id; } using (var context = new SkiRentalContext()) { var uow = new UnitOfWork(context); Rental rental = new Rental(); rental.CustomerName = "Johan Eriksson"; rental.CustomerPhoneNumber = "0705556677"; rental.StartTime = DateTime.Now; rental.EquipmentId = eqid; rental.RentalPlanId = rpid; uow.RentalRepository.CreateRental(rental); uow.Save(); } using (var context = new SkiRentalContext()) { context.Rentals.Should().NotBeNullOrEmpty(); context.Rentals.Where(r => r.CustomerName == "Johan Eriksson").Should().HaveCount(1); } }
public async Task<Rental> CreateRentalAsync(Rental rental) { // Store navigational properties. var equipment = rental.Equipment; var plan = rental.RentalPlan; // Update the server. rental = await restService.PostAsync("api/rentals", rental); rental.RentalPlan = plan; rental.Equipment = equipment; // Update the local cache (if the server was updated successfully). rentals.Add(new RentalItem { Rental = rental, DueTime = GetDueTime(rental) }); availableEquipment.Remove(availableEquipment.Find(e => e.Id == rental.EquipmentId)); return rental; }
public IHttpActionResult PostRental(Rental rental) { if (ModelState.IsValid) { rental.RentalPlan = null; rental.Equipment = null; var result = rentalService.CreateRental(rental); if(result == CreateRentalResult.Success) { return Ok(rental); } else { return Conflict(); } } else { return BadRequest(); } }
private DateTime GetDueTime(Rental rental) { return rental.StartTime.AddHours(rental.RentalPlan.Duration); }
public void GetEquipmentsByRentalStateReturnsEquipmentsWithCorrectRentalState() { int availableId; int rentedId; using (var context = new SkiRentalContext()) { var uow = new UnitOfWork(context); Equipment e1 = new Equipment(); e1.ProductName = "EQtest1"; e1.EquipmentTypeId = 1; e1.EquipmentStateId = 1; e1.PurchaseDate = DateTime.Now; e1.PurchasePrice = 500; Equipment e2 = new Equipment(); e2.ProductName = "EQtest2"; e2.EquipmentTypeId = 2; e2.EquipmentStateId = 2; e2.PurchaseDate = DateTime.Now; e2.PurchasePrice = 600; context.Equipments.Add(e1); context.Equipments.Add(e2); RentalPlan rp1 = new RentalPlan(); rp1.Duration = 8; rp1.Discount = 0.25; context.RentalPlans.Add(rp1); Rental rental = new Rental(); rental.CustomerName = "Johan Eriksson"; rental.CustomerPhoneNumber = "0705556677"; rental.StartTime = DateTime.Now; rental.Equipment = e1; rental.RentalPlan = rp1; uow.RentalRepository.CreateRental(rental); uow.Save(); availableId = e2.Id; rentedId = e1.Id; } using (var context = new SkiRentalContext()) { var uow = new UnitOfWork(context); var availableEq = uow.EquipmentRepository.GetEquipmentsByRentalState(false); var rentedEq = uow.EquipmentRepository.GetEquipmentsByRentalState(true); availableEq.Should().HaveCount(1); rentedEq.Should().HaveCount(1); availableEq[0].Id.Should().Be(availableId); rentedEq[0].Id.Should().Be(rentedId); } }
public void GetAllRentalsShouldContainTwoRentals() { //Arrange var testHelper = new TestHelper(); var testData = testHelper.CreateEquipmentList(); RentalPlan rp = new RentalPlan(); rp.Id = 1; rp.Duration = 8; rp.Discount = 0; Rental rental = new Rental(); rental.CustomerName = "Johan Eriksson"; rental.CustomerPhoneNumber = "0705556677"; rental.StartTime = DateTime.Now; rental.EquipmentId = testData[0].Id; rental.RentalPlanId = rp.Id; Rental rental2 = new Rental(); rental2.CustomerName = "Johan Eriksson"; rental2.CustomerPhoneNumber = "0705556677"; rental2.StartTime = DateTime.Now; rental2.EquipmentId = testData[1].Id; rental2.RentalPlanId = rp.Id; IRentalRepository rentalRepository = Substitute.For<IRentalRepository>(); rentalRepository.GetAllRentals().Returns(new List<Rental> { rental, rental2}); IUnitOfWork uow = Substitute.For<IUnitOfWork>(); uow.RentalRepository.Returns(rentalRepository); RentalService rs = new RentalService(uow); //Act var rentals = rs.GetAllRentals(); //Assert rentals.Should().HaveCount(2); }
public void CreateRental(Rental rental) { context.Rentals.Add(rental); }
public void GetAllRentalsShouldNotBeEmpty() { using (var context = new SkiRentalContext()) { var uow = new UnitOfWork(context); var eqList = CreateEquipmentList(); RentalPlan rp1 = new RentalPlan(); rp1.Duration = 8; rp1.Discount = 0.25; context.RentalPlans.Add(rp1); Rental rental = new Rental(); rental.CustomerName = "Johan Eriksson"; rental.CustomerPhoneNumber = "0705556677"; rental.StartTime = DateTime.Now; rental.EquipmentId = eqList[0].Id; rental.RentalPlanId = rp1.Id; Rental rental2 = new Rental(); rental2.CustomerName = "Anna Nilsson"; rental2.CustomerPhoneNumber = "0738887766"; rental2.StartTime = DateTime.Now; rental2.EquipmentId = eqList[1].Id; rental2.RentalPlanId = rp1.Id; uow.RentalRepository.CreateRental(rental); uow.RentalRepository.CreateRental(rental2); uow.Save(); } using (var context = new SkiRentalContext()) { var uow = new UnitOfWork(context); var rentals = uow.RentalRepository.GetAllRentals(); rentals.Should().NotBeEmpty(); } }