Beispiel #1
0
        public async Task UpdateAsyncShouldReturnStatusCode204()
        {
            //Arrange (create a moq repo and use it for the controller)
            var mockRepo = ApiTestData.MockBatchRepo(ApiTestData.Batches.ToList());
            var options  = TestDbInitializer.InitializeDbOptions("GetAllBatchesByTCShouldGetAllByTCAsync");

            using var db = TestDbInitializer.CreateTestDb(options);
            var mapper = new Mapper();

            var mockLogger         = new Mock <ILogger <TenantController> >();
            var mockAddressService = new Mock <IAddressService>();
            var controller         = new TenantController(mockRepo.Object, mockAddressService.Object, mockLogger.Object);

            //Act
            var apiTenant = new ApiTenant
            {
                Id             = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"),
                Email          = "*****@*****.**",
                Gender         = "male",
                FirstName      = "Colton",
                LastName       = "Clary",
                AddressId      = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"),
                TrainingCenter = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"),
                ApiBatch       = new ApiBatch
                {
                    TrainingCenter  = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"),
                    Id              = 1,
                    BatchCurriculum = "c#"
                },
                ApiCar = new ApiCar
                {
                    Id           = 1,
                    Color        = "y",
                    LicensePlate = "123",
                    Make         = "s",
                    Model        = "2",
                    State        = "w",
                    Year         = "l"
                },
                ApiAddress = new ApiAddress
                {
                    State     = "sdl",
                    AddressId = Guid.Parse("fa4d6c6e-9650-44c9-8c6b-5aebd3f9a67d"),
                    City      = "l",
                    Country   = "l",
                    Street    = "s",
                    ZipCode   = "l"
                }
            };
            var result = await controller.UpdateAsync(apiTenant);

            //Assert
            _ = Assert.IsAssignableFrom <StatusCodeResult>(result);
        }
Beispiel #2
0
        public async Task <ActionResult <IEnumerable <ApiTenant> > > GetAllAsync([FromQuery] string firstName = null, [FromQuery] string lastName = null, [FromQuery] string gender = null, [FromQuery] string trainingCenter = null)
        {
            //Parse training center string to guid if it exists
            Guid?trainingCenterGuid;

            if (trainingCenter != null)
            {
                trainingCenterGuid = Guid.Parse(trainingCenter);
            }
            else
            {
                trainingCenterGuid = null;
            }

            //Call repository GetAllAsync
            _logger.LogInformation("GET - Getting tenants");
            var tenants = await _tenantRepository.GetAllAsync(firstName, lastName, gender, trainingCenterGuid);

            //Maps batches and cars correctly, based on null or not null
            var newTenants = new List <Lib.Models.Tenant>();

            foreach (var tenant in tenants)
            {
                Lib.Models.Batch batch;
                int?batchId;
                if (tenant.Batch != null)
                {
                    batch = new Lib.Models.Batch
                    {
                        Id = tenant.Batch.Id,
                        BatchCurriculum = tenant.Batch.BatchCurriculum,
                        TrainingCenter  = tenant.Batch.TrainingCenter
                    };
                    batch.SetStartAndEndDate(tenant.Batch.StartDate, tenant.Batch.EndDate);
                    batchId = tenant.BatchId;
                }
                else
                {
                    batch   = null;
                    batchId = null;
                }
                tenant.Batch   = batch;
                tenant.BatchId = batchId;
                Lib.Models.Car car;
                int?           carId;
                if (tenant.Car != null)
                {
                    car = new Lib.Models.Car()
                    {
                        Id           = tenant.Car.Id,
                        LicensePlate = tenant.Car.LicensePlate,
                        Make         = tenant.Car.Make,
                        Model        = tenant.Car.Model,
                        Color        = tenant.Car.Color,
                        Year         = tenant.Car.Year,
                        State        = tenant.Car.State
                    };
                    carId        = tenant.CarId;
                    tenant.CarId = carId;
                }
                else
                {
                    car = null;
                }
                tenant.Car = car;

                newTenants.Add(tenant);
            }

            //Cast all Logic Tenants into ApiTenants
            var apiTenants = new List <ApiTenant>();

            foreach (var apiTenant in newTenants)
            {
                var newApiTenant = new ApiTenant
                {
                    Id             = apiTenant.Id,
                    Email          = apiTenant.Email,
                    Gender         = apiTenant.Gender,
                    FirstName      = apiTenant.FirstName,
                    LastName       = apiTenant.LastName,
                    AddressId      = apiTenant.AddressId,
                    RoomId         = apiTenant.RoomId,
                    CarId          = apiTenant.CarId,
                    BatchId        = apiTenant.BatchId,
                    TrainingCenter = apiTenant.TrainingCenter
                };
                apiTenants.Add(newApiTenant);
            }
            //Return OK with a list of tenants
            return(Ok(apiTenants));
        }
Beispiel #3
0
        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));
            }
        }
Beispiel #4
0
        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));
            }
        }
Beispiel #5
0
        public async Task <ActionResult <ApiTenant> > GetByIdAsync([FromRoute] Guid id)
        {
            _logger.LogInformation("GET - Getting notifications by Tenant ID: {TenantId}", id);
            try
            {
                //Call repository method GetByIdAsync
                var tenant = await _tenantRepository.GetByIdAsync(id);

                //cast tenant into Api Tenant
                var apiTenant = new ApiTenant
                {
                    Id             = tenant.Id,
                    Email          = tenant.Email,
                    Gender         = tenant.Gender,
                    FirstName      = tenant.FirstName,
                    LastName       = tenant.LastName,
                    AddressId      = tenant.AddressId,
                    RoomId         = tenant.RoomId,
                    CarId          = tenant.CarId,
                    BatchId        = tenant.BatchId,
                    TrainingCenter = tenant.TrainingCenter
                };

                if (apiTenant.CarId != null)
                {
                    apiTenant.ApiCar = new ApiCar
                    {
                        Id           = tenant.Car.Id,
                        Color        = tenant.Car.Color,
                        Make         = tenant.Car.Make,
                        Model        = tenant.Car.Model,
                        LicensePlate = tenant.Car.LicensePlate,
                        State        = tenant.Car.State,
                        Year         = tenant.Car.Year
                    };
                }

                if (apiTenant.BatchId != null)
                {
                    apiTenant.ApiBatch = new ApiBatch
                    {
                        Id = tenant.Batch.Id,
                        BatchCurriculum = tenant.Batch.BatchCurriculum,
                        StartDate       = tenant.Batch.StartDate,
                        EndDate         = tenant.Batch.EndDate,
                        TrainingCenter  = tenant.Batch.TrainingCenter
                    };
                }
                //return OK with the ApiTenant Model, including car and batch if applicable
                return(Ok(apiTenant));
            }
            catch (ArgumentException)
            {
                _logger.LogWarning("Tenant was not found");

                return(NotFound());
            }
            catch (Exception e)
            {
                _logger.LogError("Get request failed. Error: " + e.Message);

                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }