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 }); } }
private static UpdateFlatDto ConvertFlatToUpdateFlatDto(this BBIT.Domain.Entities.Flat.Flat flat) { return(new UpdateFlatDto { Flat = new FlatDto { Id = flat.Id.ToString(), FlatNumber = flat.FlatNumber, Level = flat.Floor, AmountOfRooms = flat.AmountOfRooms, AmountOfTenants = flat.AmountOfTenants, TotalArea = flat.TotalArea, HouseRoom = flat.HouseRoom, House = flat.House.HouseToHouseDto() }, }); }
private static FlatDto ConvertFlatToFlatDto(BBIT.Domain.Entities.Flat.Flat flat) { return(new FlatDto { Id = flat.Id.ToString(), FlatNumber = flat.FlatNumber, Level = flat.Floor, AmountOfRooms = flat.AmountOfRooms, AmountOfTenants = flat.AmountOfTenants, TotalArea = flat.TotalArea, HouseRoom = flat.HouseRoom, House = new HouseDto { Id = flat.House.Id.ToString(), HouseNumber = flat.House.HouseNumber, StreetName = flat.House.StreetName, City = flat.House.City, Country = flat.House.Country, PostCode = flat.House.PostCode } }); }
public static FlatDto FlatToFlatDto(this BBIT.Domain.Entities.Flat.Flat flat) => ConvertFlatToFlatDto(flat);
private static BBIT.Domain.Entities.Flat.Flat ConvertUpdateFlatRequestToUpdateFlatDto(ref BBIT.Domain.Entities.Flat.Flat flat, UpdateFlatDto updateFlatDto) { flat.Id = Guid.Parse(updateFlatDto.Flat.Id); flat.FlatNumber = updateFlatDto.Flat.FlatNumber; flat.Floor = updateFlatDto.Flat.Level; flat.AmountOfRooms = updateFlatDto.Flat.AmountOfRooms; flat.TotalArea = updateFlatDto.Flat.TotalArea; flat.HouseRoom = updateFlatDto.Flat.HouseRoom; return(flat); }
public static BBIT.Domain.Entities.Flat.Flat UpdateFlatDtoToFlat(this BBIT.Domain.Entities.Flat.Flat flat, UpdateFlatDto updateFlatDto) => ConvertUpdateFlatRequestToUpdateFlatDto(ref flat, updateFlatDto);