Beispiel #1
0
        public async Task <ActionResult> Create(Tenant tenant)
        {
            tenant.Id = System.Guid.NewGuid().ToString();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                await _repository.AddAsync(tenant);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View(tenant));
            }
        }
Beispiel #2
0
        public async Task <Guid> Create(TenantRequest request)
        {
            if (request == null)
            {
                _logger.LogError($"[Create] Argument {nameof(request)} is null");
                throw new ArgumentNullException(nameof(request));
            }

            var tenants = await _tenantRepository.GetAsync();

            if (tenants.Any(x => x.TenantName.ToLower() == request.Name.ToLower()))
            {
                _logger.LogError($"[Create] Duplicate parameter: {nameof(request.Name)}");
                throw new DuplicateNameException();
            }

            var tenant = request.Adapt <Tenant>();

            tenant.CreatedOn = DateTime.UtcNow;
            return(await _tenantRepository.AddAsync(tenant));
        }
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));
            }
        }