コード例 #1
0
ファイル: SqlHouseService.cs プロジェクト: ice19942335/BBIT
        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
                });
            }
        }
コード例 #2
0
ファイル: HouseMapper.cs プロジェクト: ice19942335/BBIT
 public static CreateHouseDto ConvertCreateHouseToCreateHouseDto(BBIT.Domain.Entities.House.House house)
 {
     return(new CreateHouseDto
     {
         House = new HouseDto
         {
             Id = house.Id.ToString(),
             HouseNumber = house.HouseNumber,
             StreetName = house.StreetName,
             City = house.City,
             Country = house.Country,
             PostCode = house.PostCode,
         }
     });
 }
コード例 #3
0
ファイル: HouseMapper.cs プロジェクト: ice19942335/BBIT
 private static UpdateHouseDto ConvertHouseToUpdateHouseDto(BBIT.Domain.Entities.House.House house)
 {
     return(new UpdateHouseDto
     {
         House = new HouseDto
         {
             Id = house.Id.ToString(),
             HouseNumber = house.HouseNumber,
             StreetName = house.StreetName,
             City = house.City,
             Country = house.Country,
             PostCode = house.PostCode
         }
     });
 }
コード例 #4
0
ファイル: HouseMapper.cs プロジェクト: ice19942335/BBIT
        private static HouseDto ConvertHouseToHouseDto(BBIT.Domain.Entities.House.House house)
        {
            if (house != null)
            {
                return(new HouseDto
                {
                    Id = house.Id.ToString(),
                    HouseNumber = house.HouseNumber,
                    StreetName = house.StreetName,
                    City = house.City,
                    Country = house.Country,
                    PostCode = house.PostCode
                });
            }

            return(null);
        }
コード例 #5
0
ファイル: SqlHouseService.cs プロジェクト: ice19942335/BBIT
        public async Task <UpdateHouseDto> UpdateHouse(UpdateHouseDto updateHouseDto)
        {
            try
            {
                BBIT.Domain.Entities.House.House house =
                    _dbContext.Houses.FirstOrDefault(x => x.Id == Guid.Parse(updateHouseDto.House.Id));

                if (house is null)
                {
                    return new UpdateHouseDto
                           {
                               Errors = new [] { "Item not found" },
                               Status = false
                           }
                }
                ;

                house = updateHouseDto.UpdateHouseDtoToHouse(ref house);
                _dbContext.Houses.Update(house);
                await _dbContext.SaveChangesAsync();

                var updatedHouseDto = house.HouseToUpdateHouseDto();
                updatedHouseDto.Status = true;

                return(updatedHouseDto);
            }
            catch (Exception e)
            {
                _logger.LogError($"Error on updating house in database. Exception message: {e.Message};\nInner message: {e.InnerException?.Message}");
                return(new UpdateHouseDto
                {
                    Errors = new[] { "Error on updating house in database." },
                    ServerError = true,
                    Status = false
                });
            }
        }
コード例 #6
0
ファイル: HouseMapper.cs プロジェクト: ice19942335/BBIT
 public static HouseDto HouseToHouseDto(this BBIT.Domain.Entities.House.House house) =>
 ConvertHouseToHouseDto(house);
コード例 #7
0
ファイル: HouseMapper.cs プロジェクト: ice19942335/BBIT
 public static CreateHouseDto HouseToCreateHouseDto(this BBIT.Domain.Entities.House.House house) =>
 ConvertCreateHouseToCreateHouseDto(house);
コード例 #8
0
ファイル: HouseMapper.cs プロジェクト: ice19942335/BBIT
        private static BBIT.Domain.Entities.House.House ConvertUpdateHouseDtoToHouse(UpdateHouseDto updateHouseDto, ref BBIT.Domain.Entities.House.House house)
        {
            house.Id          = Guid.Parse(updateHouseDto.House.Id);
            house.HouseNumber = updateHouseDto.House.HouseNumber;
            house.StreetName  = updateHouseDto.House.StreetName;
            house.City        = updateHouseDto.House.City;
            house.Country     = updateHouseDto.House.Country;
            house.PostCode    = updateHouseDto.House.PostCode;

            return(house);
        }
コード例 #9
0
ファイル: HouseMapper.cs プロジェクト: ice19942335/BBIT
 public static BBIT.Domain.Entities.House.House UpdateHouseDtoToHouse(this UpdateHouseDto updateHouseDto, ref BBIT.Domain.Entities.House.House house) =>
 ConvertUpdateHouseDtoToHouse(updateHouseDto, ref house);