Ejemplo n.º 1
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
                });
            }
        }
Ejemplo n.º 2
0
 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,
         }
     });
 }
Ejemplo n.º 3
0
 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
         }
     });
 }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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
                });
            }
        }
Ejemplo n.º 6
0
 public static HouseDto HouseToHouseDto(this BBIT.Domain.Entities.House.House house) =>
 ConvertHouseToHouseDto(house);
Ejemplo n.º 7
0
 public static CreateHouseDto HouseToCreateHouseDto(this BBIT.Domain.Entities.House.House house) =>
 ConvertCreateHouseToCreateHouseDto(house);
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
 public static BBIT.Domain.Entities.House.House UpdateHouseDtoToHouse(this UpdateHouseDto updateHouseDto, ref BBIT.Domain.Entities.House.House house) =>
 ConvertUpdateHouseDtoToHouse(updateHouseDto, ref house);