public async Task AddShouldAddTest() { // Arrange var options = TestDbInitializer.InitializeDbOptions("AddShouldAddTest"); using var db = TestDbInitializer.CreateTestDb(options); var mapper = new Mapper(); var repo = new TenantRepository(db, mapper); var tenant = new Lib.Models.Tenant { Id = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9ac7d"), Email = "*****@*****.**", Gender = "Male", FirstName = "Clary", LastName = "Colton", AddressId = Guid.Parse("fa4d6c6e-9650-45c9-8c6b-5aebd3f9a67c"), RoomId = Guid.Parse("fa4d6c6e-9650-44c9-5c6b-5aebd3f9a67c"), CarId = 0, BatchId = 0, TrainingCenter = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"), Car = new Lib.Models.Car { Id = 0, LicensePlate = "LicensePlate", Make = "Make", Model = "Model", Color = "Color", Year = "Year", State = "TX" }, Batch = new Lib.Models.Batch { Id = 0, BatchCurriculum = "C#", TrainingCenter = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"), } }; tenant.Batch.SetStartAndEndDate(DateTime.MinValue, DateTime.Now); // Act await repo.AddAsync(tenant); await repo.SaveAsync(); var check = await repo.GetByIdAsync(Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9ac7d")); // Assert Assert.NotNull(check); Assert.Equal(tenant.Email, check.Email); Assert.Equal(tenant.Gender, check.Gender); Assert.Equal(tenant.FirstName, check.FirstName); Assert.Equal(tenant.LastName, check.LastName); Assert.Equal(tenant.AddressId, check.AddressId); Assert.Equal(tenant.RoomId, check.RoomId); Assert.Equal(tenant.TrainingCenter, check.TrainingCenter); }
/// <summary> /// Updates a new tenant object as well as its associated properties. /// </summary> /// <param name="tenant">The Tenant</param> public void Put(Lib.Models.Tenant tenant) { var newTenant = _mapper.MapTenant(tenant); _context.Tenant.Update(newTenant); if (tenant.Car != null) { var newCar = _mapper.MapCar(tenant.Car); _context.Car.Update(newCar); } }
public async Task HasCarShouldReturnTrueIfHasCar() { //Arrange var options = TestDbInitializer.InitializeDbOptions("HasCarShouldReturnTrueIfHasCar"); using var db = TestDbInitializer.CreateTestDb(options); var mapper = new Mapper(); var repo = new TenantRepository(db, mapper); var tenant = new Lib.Models.Tenant { Id = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-4aebd3f9a67d"), Email = "*****@*****.**", Gender = "Male", FirstName = "Clary", LastName = "Colton", AddressId = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd2f9a67c"), RoomId = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9267c"), CarId = 4, BatchId = 4, TrainingCenter = Guid.Parse("32bbf6b3-2d47-4823-8bb9-2087491cc491"), Car = new Lib.Models.Car { // Id = 4, LicensePlate = "LicensePlate", Make = "Make", Model = "Model", Color = "Color", Year = "Year", State = "TX" }, Batch = new Lib.Models.Batch { Id = 4, BatchCurriculum = "C#", TrainingCenter = Guid.Parse("32bbf6b3-2d47-4823-8bb2-d087491cc491"), } }; tenant.Batch.SetStartAndEndDate(DateTime.MinValue, DateTime.Now); //Act await repo.AddAsync(tenant); await repo.SaveAsync(); var check = await repo.HasCarAsync(Guid.Parse("fa4d6c6e-9650-44c9-8c6b-4aebd3f9a67d")); //Assert Assert.True(check); }
/// <summary> /// Updates values associated to a tenant. /// </summary> /// <param name="tenant">The tenant with changed values</param> public async Task UpdateAsync(Lib.Models.Tenant tenant) { var currentTenant = await _context.Tenant.FindAsync(tenant.Id); if (currentTenant == null) { throw new InvalidOperationException("Invalid Tenant Id"); } var newTenant = _mapper.MapTenant(tenant); _context.Entry(currentTenant).CurrentValues.SetValues(newTenant); }
public void LibTenantToDbTenantTest() { var tenant = new Lib.Models.Tenant { Id = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"), Email = "*****@*****.**", Gender = "m", FirstName = "Joe", LastName = "Mohrbacher", AddressId = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"), RoomId = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"), CarId = 0, BatchId = 1, TrainingCenter = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"), Car = new Lib.Models.Car { Id = 0, LicensePlate = "LicensePlate", Make = "Make", Model = "Model", Color = "Color", Year = "Year", State = "TX" }, Batch = new Lib.Models.Batch { Id = 0, BatchCurriculum = "C#", TrainingCenter = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"), } }; tenant.Batch.SetStartAndEndDate(DateTime.MinValue, DateTime.Now); var tenants = mapper.MapTenant(tenant); Assert.Equal(tenant.Id, tenants.Id); Assert.Equal(tenant.Email, tenants.Email); Assert.Equal(tenant.FirstName, tenants.FirstName); Assert.Equal(tenant.LastName, tenants.LastName); Assert.Equal(tenant.AddressId, tenants.AddressId); Assert.Equal(tenant.RoomId, tenants.RoomId); Assert.Equal(tenant.CarId, tenants.CarId); Assert.Equal(tenant.BatchId, tenants.BatchId); Assert.Equal(tenant.TrainingCenter, tenants.TrainingCenter); }
/// <summary> /// Map a Entity Tenant from a Model Tenant /// </summary> /// <param name="tenant">A Tenant Model who may have a nested Car Model and/or a Batch Model</param> /// <returns>A Tenant Entity who may have a nested Car Model and/or a Batch Model</returns> public Entities.Tenant MapTenant(Lib.Models.Tenant tenant) { Entities.Car newCar; int? newCarId = 0; if (tenant.Car != null) { newCar = new Entities.Car { Id = 0, LicensePlate = tenant.Car.LicensePlate, Make = tenant.Car.Make, Model = tenant.Car.Model, Color = tenant.Car.Color, Year = tenant.Car.Year, State = tenant.Car.State //maybe add tenant }; } else { newCar = null; newCarId = null; } return(new Entities.Tenant { Id = tenant.Id, Email = tenant.Email, Gender = tenant.Gender, FirstName = tenant.FirstName, LastName = tenant.LastName, AddressId = tenant.AddressId, RoomId = tenant.RoomId, CarId = newCarId, BatchId = tenant.BatchId, TrainingCenter = tenant.TrainingCenter, Car = newCar }); }
public async Task <ActionResult> UpdateAsync([FromBody] ApiTenant tenant) { try { _logger.LogInformation("PUT - Updating tenant with tenantid {tenantId}.", tenant.Id); _logger.LogInformation("Posting Address to Address Service..."); var postedAddress = await this._addressService.GetAddressAsync(tenant.ApiAddress); //cast ApiTenant in Logic Tenant var newTenant = new Lib.Models.Tenant { Id = (Guid)tenant.Id, Email = tenant.Email, Gender = tenant.Gender, FirstName = tenant.FirstName, LastName = tenant.LastName, AddressId = (Guid)tenant.AddressId, RoomId = tenant.RoomId, CarId = tenant.CarId, BatchId = tenant.BatchId, TrainingCenter = tenant.TrainingCenter }; if (tenant.ApiCar != null) { newTenant.Car = new Lib.Models.Car { Id = tenant.ApiCar.Id, Color = tenant.ApiCar.Color, Make = tenant.ApiCar.Make, Model = tenant.ApiCar.Model, LicensePlate = tenant.ApiCar.LicensePlate, State = tenant.ApiCar.State, Year = tenant.ApiCar.Year }; } //Call repository method Put and Save Async _tenantRepository.Put(newTenant); await _tenantRepository.SaveAsync(); _logger.LogInformation("PUT persisted to dB"); //Return NoContent return(StatusCode(StatusCodes.Status204NoContent)); } catch (ArgumentException) { _logger.LogWarning("PUT request failed. Not Found Exception"); return(NotFound()); } catch (InvalidOperationException e) { _logger.LogError("PUT request failed. Error: " + e.Message); return(Conflict(e.Message)); } catch (Exception e) { _logger.LogError("PUT request failed. Error: " + e.Message); return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public async Task <ActionResult <ApiTenant> > PostAsync([FromBody] ApiTenant tenant) { _logger.LogInformation("POST - Making tenant for tenant ID {tenantId}.", tenant.Id); try { _logger.LogInformation("Posting Address to Address Service..."); var postedAddress = await this._addressService.GetAddressAsync(tenant.ApiAddress); //cast ApiTenant in Logic Tenant var newTenant = new Lib.Models.Tenant { Id = Guid.NewGuid(), Email = tenant.Email, Gender = tenant.Gender, FirstName = tenant.FirstName, LastName = tenant.LastName, AddressId = Guid.NewGuid(), //TODO postedAddress.AddressId, RoomId = null, //Room Service will set this later CarId = null, BatchId = tenant.BatchId, TrainingCenter = tenant.TrainingCenter, }; if (tenant.ApiCar.LicensePlate != null) { newTenant.Car = new Lib.Models.Car { Color = tenant.ApiCar.Color, Make = tenant.ApiCar.Make, Model = tenant.ApiCar.Model, LicensePlate = tenant.ApiCar.LicensePlate, State = tenant.ApiCar.State, Year = tenant.ApiCar.Year }; newTenant.CarId = 0; } else { newTenant.Car = null; newTenant.CarId = null; } //Call Repository Methods AddAsync and SaveAsync await _tenantRepository.AddAsync(newTenant); await _tenantRepository.SaveAsync(); _logger.LogInformation("POST Persisted to dB"); //Return Created and the model of the new tenant return(Created($"api/Tenant/{newTenant.Id}", newTenant)); } catch (ArgumentException) { _logger.LogWarning("Not Found"); return(NotFound()); } catch (InvalidOperationException e) { _logger.LogError("POST request failed. Error: " + e.Message); return(Conflict(e.Message)); } catch (Exception e) { _logger.LogError("POST request failed. Error: " + e.Message); return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
/// <summary> /// Adds a new tenant object as well as its associated properties. /// </summary> /// <param name="tenant">The Tenant</param> public async Task AddAsync(Lib.Models.Tenant tenant) { var newTenant = _mapper.MapTenant(tenant); await _context.Tenant.AddAsync(newTenant); }