public async Task <IActionResult> Create(CreateRentModel model) { if (!ModelState.IsValid) { TempData.AddErrorMessage(WrongInput); return(this.View(model)); } bool?isCreated = await this.rents.CreateAsync(model); if (isCreated != null && !isCreated.Value) { TempData.AddErrorMessage(WrongInput); return(this.RedirectToAction("Index")); } else if (isCreated == null) { TempData.AddErrorMessage("Договорът за наем беше въведен, но първото плащане НЕ беше генерирано"); return(this.RedirectToAction("Index")); } TempData.AddSuccessMessage("Договорът за наем беше успешно създаден! Първото плащане беше генерирано!"); return(this.RedirectToAction("Index")); }
public async Task <bool?> CreateAsync(CreateRentModel model) { var rentAgreement = new RentAgreement() { StartDate = model.StartDate, EndDate = model.EndDate, ClientId = model.ClientId, Description = model.Description, ParkingSlotDescription = model.ParkingSlotDescription, MonthlyPrice = model.MonthlyPrice }; if (model.PropertiesIds != null) { var listProperties = new List <PropertyRent>(); foreach (var propertyId in model.PropertiesIds) { listProperties.Add(new PropertyRent() { PropertyId = propertyId }); } rentAgreement.PropertyRents = listProperties; } var listParkingSlots = new List <ParkingSlot>(); if (model.CarSlots > 0) { listParkingSlots.Add(new ParkingSlot() { Type = ParkingSlotType.Car, Quantity = model.CarSlots, Price = model.CarMonthPrice, Area = model.CarsArea }); } if (model.BusSlots > 0) { listParkingSlots.Add(new ParkingSlot() { Type = ParkingSlotType.Bus, Quantity = model.BusSlots, Price = model.BusMonthPrice, Area = model.BusesArea }); } if (model.TruckSlots > 0) { listParkingSlots.Add(new ParkingSlot() { Type = ParkingSlotType.Truck, Quantity = model.TruckSlots, Price = model.TruckMonthPrice, Area = model.TrucksArea }); } if (model.BigTruckSlots > 0) { listParkingSlots.Add(new ParkingSlot() { Type = ParkingSlotType.BigTruck, Quantity = model.BigTruckSlots, Price = model.BigTruckMonthPrice, Area = model.BigTrucksArea }); } if (model.CarCageSlots > 0) { listParkingSlots.Add(new ParkingSlot() { Type = ParkingSlotType.CarCage, Quantity = model.CarCageSlots, Price = model.CarCageMonthPrice, Area = model.CarCagesArea }); } if (model.OtherSlots > 0) { listParkingSlots.Add(new ParkingSlot() { Type = ParkingSlotType.Other, Quantity = model.OtherSlots, Price = model.OtherMonthPrice, Area = model.OthersArea }); } rentAgreement.ParkingSlots = listParkingSlots; var result = await this.Db.RentAgreements.AddAsync(rentAgreement); try { await this.Db.SaveChangesAsync(); } catch (Exception) { return(false); } int deadlineYear = DateTime.UtcNow.Year; int deadlineMonth = DateTime.UtcNow.Month; var deadline = new DateTime(deadlineYear, deadlineMonth, 5); var totalPrice = result.Entity.MonthlyPrice + result.Entity.ParkingSlots.Sum(x => x.Price * x.Quantity); var isPaymentCreated = await this.monthlyRents.CreateAsync(result.Entity.Id, totalPrice, deadline); if (!isPaymentCreated) { return(null); } return(true); }
public async Task CreateAsync_SouldReturn_True_IfAllArguentsAreCorrect() { var db = this.GetDatabase(); var mapper = this.GetMapper(); var thirdRent = new RentAgreement { Id = 3, MonthlyPrice = 1900, IsActual = true }; var forthRent = new RentAgreement { Id = 4, MonthlyPrice = 2900, IsActual = true }; await db.RentAgreements.AddRangeAsync(thirdRent, forthRent); var client = new Client { Id = 1, Name = "Ivan" }; await db.Clients.AddAsync(client); var propertyOne = new Property { Id = 1, Area = 800, IsActual = true }; var propertyTwo = new Property { Id = 2, Area = 600, IsActual = true }; await db.Properties.AddRangeAsync(propertyTwo, propertyOne); await db.SaveChangesAsync(); var createModel = new CreateRentModel { StartDate = new DateTime(2018, 3, 3), ClientId = 1, Description = "No Desc", ParkingSlotDescription = "Some Desc", MonthlyPrice = 2000, PropertiesIds = new List <int>() { 1, 2 }, CarSlots = 1, CarMonthPrice = 1, BusSlots = 2, BusMonthPrice = 2, TruckSlots = 3, TruckMonthPrice = 3, BigTruckSlots = 4, BigTruckMonthPrice = 4, CarCageSlots = 5, CarCageMonthPrice = 5, OtherSlots = 6, OtherMonthPrice = 6 }; var monthlyRentMock = new Mock <IMonthlyRentsService>(); monthlyRentMock .Setup(mr => mr.CreateAsync(It.IsAny <int>(), It.IsAny <decimal>(), It.IsAny <DateTime>())) .ReturnsAsync(true); var rentService = new RentsService(mapper, db, monthlyRentMock.Object); //Act var result = await rentService.CreateAsync(createModel); var createdRent = await db.RentAgreements .FirstOrDefaultAsync(x => x.Description == "No Desc"); //Assert result .Should() .BeTrue(); createdRent.Should().BeOfType <RentAgreement>().And .Match <RentAgreement>(r => r.ClientId == 1 && r.StartDate == new DateTime(2018, 3, 3) && r.ParkingSlotDescription == "Some Desc" && r.MonthlyPrice == 2000M && r.PropertyRents.Any(x => x.Property.Id == 2) && r.ParkingSlots.Any(x => x.Price == 1M || x.Price == 2M || x.Price == 3M || x.Price == 4M || x.Price == 5M || x.Price == 6M)); }