Пример #1
0
        public async Task <CreateFlatDto> CreateFlatAsync(CreateFlatDto createFlatDto)
        {
            try
            {
                var flat = _dbContext.Flats.Include(x => x.House).FirstOrDefault(x => x.FlatNumber == createFlatDto.FlatNumber && x.House.Id == Guid.Parse(createFlatDto.HouseId));
                if (flat != null)
                {
                    return new CreateFlatDto
                           {
                               Errors      = new[] { $"Flat with number: '{createFlatDto.FlatNumber}' in house: '{flat.House.Country}, {flat.House.City}, {flat.House.StreetName}, {flat.House.HouseNumber}' already exist" },
                               Status      = false,
                               ServerError = false
                           }
                }
                ;

                var house = _dbContext.Houses.FirstOrDefault(x => x.Id == Guid.Parse(createFlatDto.HouseId));

                if (house is null)
                {
                    return new CreateFlatDto
                           {
                               Errors      = new[] { $"House with Id: '{createFlatDto.HouseId}' not found" },
                               Status      = false,
                               ServerError = false
                           }
                }
                ;

                Guid id;
                do
                {
                    id = Guid.NewGuid();
                } while (_dbContext.Flats.FirstOrDefault(x => x.Id == id) != null);

                BBIT.Domain.Entities.Flat.Flat newFlat = createFlatDto.CreateFlatDtoToFlat();
                newFlat.Id              = id;
                newFlat.House           = house;
                newFlat.AmountOfTenants = 0; //Just make sure Flat have 0 tenant on creation.

                await _dbContext.Flats.AddAsync(newFlat);

                await _dbContext.SaveChangesAsync();

                createFlatDto        = newFlat.FlatToCreateFlatDto();
                createFlatDto.Status = true;

                return(createFlatDto);
            }
            catch (Exception e)
            {
                _logger.LogError($"Error on adding new flat into database. Exception message: {e.Message};\nInner message: {e.InnerException?.Message}");
                return(new CreateFlatDto
                {
                    Errors = new[] { "Error on adding new flat into database." },
                    ServerError = true,
                    Status = false
                });
            }
        }
Пример #2
0
        public async Task <CreateTenantDto> CreateTenantAsync(CreateTenantDto createTenantDto)
        {
            try
            {
                if (createTenantDto.FlatId != null && !CheckFlatExist(createTenantDto.FlatId))
                {
                    return new CreateTenantDto {
                               Errors = new[] { $"Flat with Id: '{createTenantDto.FlatId}' not exist." }
                    }
                }
                ;

                if (createTenantDto.FlatId != null && !CheckHouseExistByFlatId(createTenantDto.FlatId))
                {
                    return new CreateTenantDto {
                               Errors = new[] { $"House with (Flat Id: '{createTenantDto.FlatId}') not exist." }
                    }
                }
                ;

                if (CheckTenantExist(createTenantDto))
                {
                    return new CreateTenantDto {
                               Errors = new[] { $"Tenant with provided details already exist." }
                    }
                }
                ;

                BBIT.Domain.Entities.Tenant.Tenant tenant = CreateNewTenant(createTenantDto);

                var flat = GetFlatById(createTenantDto.FlatId);

                flat.AmountOfTenants++;

                tenant.Flat = flat;

                _dbContext.Flats.Update(flat);

                await _dbContext.Tenants.AddAsync(tenant);

                await _dbContext.SaveChangesAsync();

                var createTenantDtoToReturn = tenant.TenantDtoToCreateTenantDto();
                createTenantDtoToReturn.Status = true;

                return(createTenantDtoToReturn);
            }
            catch (Exception e)
            {
                _logger.LogError($"Error on creating Tenant in database. Exception message: {e.Message};\nInner message: {e.InnerException?.Message}");
                return(new CreateTenantDto
                {
                    Errors = new[] { "Error on creating Tenant in database." },
                    ServerError = true
                });
            }
        }
Пример #3
0
        public async Task <CreateHouseDto> CreateHouseAsync(CreateHouseDto createHouseDto)
        {
            var house = _dbContext.Houses.FirstOrDefault(x =>
                                                         x.Country == createHouseDto.House.Country &&
                                                         x.City == createHouseDto.House.City &&
                                                         x.StreetName == createHouseDto.House.StreetName &&
                                                         x.HouseNumber == createHouseDto.House.HouseNumber);

            if (house != null)
            {
                return new CreateHouseDto
                       {
                           Errors      = new[] { $"House with address: '{house.Country}, {house.City}, {house.StreetName}, {house.HouseNumber}' already exist" },
                           Status      = false,
                           ServerError = false
                       }
            }
            ;

            Guid id;

            do
            {
                id = Guid.NewGuid();
            } while (_dbContext.Houses.FirstOrDefault(x => x.Id == id) != null);

            BBIT.Domain.Entities.House.House newHouse = createHouseDto.CreateDtoToHouse();
            newHouse.Id = id;

            await _dbContext.Houses.AddAsync(newHouse);

            try
            {
                await _dbContext.SaveChangesAsync();

                var newHouseDtoToReturn = newHouse.HouseToCreateHouseDto();
                newHouseDtoToReturn.Status = true;

                return(newHouseDtoToReturn);
            }
            catch (Exception e)
            {
                _logger.LogError($"Error on adding new house into database. Exception message: {e.Message};\nInner message: {e.InnerException?.Message}");
                return(new CreateHouseDto
                {
                    Errors = new[] { "Error on adding new house into database." },
                    ServerError = true,
                    Status = false
                });
            }
        }
Пример #4
0
        private async Task AddHouses()
        {
            var houses = new List <BBIT.Domain.Entities.House.House>
            {
                new BBIT.Domain.Entities.House.House
                {
                    Id          = Guid.NewGuid(),
                    HouseNumber = "1",
                    StreetName  = "High Street",
                    City        = "London",
                    Country     = "United Kingdom",
                    PostCode    = "E1 7AD"
                },
                new BBIT.Domain.Entities.House.House
                {
                    Id          = Guid.NewGuid(),
                    HouseNumber = "2",
                    StreetName  = "High Street",
                    City        = "London",
                    Country     = "United Kingdom",
                    PostCode    = "E1 7AD"
                },
                new BBIT.Domain.Entities.House.House
                {
                    Id          = Guid.NewGuid(),
                    HouseNumber = "3",
                    StreetName  = "High Street",
                    City        = "London",
                    Country     = "United Kingdom",
                    PostCode    = "E1 7AD"
                },
            };

            await _dbContext.Houses.AddRangeAsync(houses);

            await _dbContext.SaveChangesAsync();
        }
Пример #5
0
        public async Task <AuthenticationDto> RefreshTokenAsync(string token, string refreshToken)
        {
            var validatedToken = GetPrincipalFromToken(token);

            if (validatedToken is null)
            {
                return new AuthenticationDto {
                           Errors = new[] { "Invalid Token" }, CriticalError = true
                }
            }
            ;

            var expiryDateUnix =
                long.Parse(validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Exp).Value);

            var expiryDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
                                    .AddSeconds(expiryDateUnix);

            var storedRefreshToken = await _context.RefreshTokens.SingleOrDefaultAsync(x => x.Token == refreshToken);

            if (storedRefreshToken is null)
            {
                return new AuthenticationDto {
                           Errors = new[] { "This refresh token does not exist" }, CriticalError = true
                }
            }
            ;

            if (storedRefreshToken.Invalidated)
            {
                return new AuthenticationDto {
                           Errors = new[] { "This refresh token has been invalidated" }, CriticalError = true
                }
            }
            ;

            if (expiryDateTimeUtc > DateTime.UtcNow)
            {
                return new AuthenticationDto {
                           Errors = new[] { "This token hasn't expired jet" }
                }
            }
            ;

            if (storedRefreshToken.Used)
            {
                return new AuthenticationDto {
                           Errors = new[] { "This refresh token has been used" }, CriticalError = true
                }
            }
            ;

            if (DateTime.UtcNow > storedRefreshToken.ExpiryDate)
            {
                return new AuthenticationDto {
                           Errors = new[] { "This refresh token has expired" }, CriticalError = true
                }
            }
            ;

            var jti = validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Jti).Value;

            if (storedRefreshToken.JwtId != jti)
            {
                return new AuthenticationDto {
                           Errors = new[] { "This refresh token does not match this JWT" }, CriticalError = true
                }
            }
            ;

            storedRefreshToken.Used = true;
            _context.RefreshTokens.Update(storedRefreshToken);
            await _context.SaveChangesAsync();

            var user = await _userManager.FindByIdAsync(validatedToken.Claims
                                                        .Single(x => x.Type == "userId").Value);

            return(await GenerateAuthenticationResultForUserAsync(user));
        }