public async Task <HousingDto> GetHousingAsync(string id) { var housing = await _repositoryHousing .GetAll().Include(x => x.Place).FirstOrDefaultAsync(h => h.Id == id); if (housing is null) { throw new KeyNotFoundException(ErrorResource.HousingNotFound); } var housingsDto = new HousingDto { Id = housing.Id, UserId = housing.UserId, Place = housing.Place, PlaceId = housing.PlaceId, Address = housing.Address, Name = housing.Name, Description = housing.Description, Scenery = housing.Scenery, PricePerDay = housing.PricePerDay }; return(housingsDto); }
public async Task <IActionResult> DetailsHousing(HousingViewModel housingViewModel) { var placeDto = new Place() { Name = housingViewModel.Place, }; if (ModelState.IsValid) { var userId = await _usManager.GetUserIdByEmailAsync(User.Identity.Name); var housingDto = new HousingDto() { Id = housingViewModel.Id, Place = placeDto, Description = housingViewModel.Description, PricePerDay = housingViewModel.Price, Address = housingViewModel.Address, Name = housingViewModel.Name, }; return(RedirectToAction("Index", "Housing")); } return(View(housingViewModel)); }
public async Task CreateAsync(HousingDto housingDto) { housingDto = housingDto ?? throw new ArgumentNullException(nameof(housingDto)); housingDto.Place.Type = "Housing type"; await _repositoryPlace.CreateAsync(housingDto.Place); var housing = new Housing { Place = housingDto.Place, Name = housingDto.Name, UserId = housingDto.UserId, User = housingDto.User, BookedFrom = housingDto.BookedFrom, BookedTo = housingDto.BookedTo, PricePerDay = housingDto.PricePerDay, Scenery = housingDto.Scenery, Description = housingDto.Description, Address = housingDto.Address }; await _repositoryHousing.CreateAsync(housing); await _repositoryHousing.SaveChangesAsync(); }
public bool SaveHousing(HousingDto housing) { var operationSucceded = false; if (housing.ID > 0) { //this is an update operation operationSucceded = UpdateHousing(housing); } else { //this is an add operation operationSucceded = AddNewHousing(housing); } return(operationSucceded); }
public async Task <IActionResult> Create(HousingViewModel housingViewModel) { if (ModelState.IsValid) { var userId = await _usManager.GetUserIdByEmailAsync(User.Identity.Name); var placeDto = new Place() { Name = housingViewModel.Place, }; var housingDto = new HousingDto() { UserId = userId, Name = housingViewModel.Name, Place = placeDto, PricePerDay = housingViewModel.Price, Description = housingViewModel.Description, Address = housingViewModel.Address, Scenery = housingViewModel.Scenery }; if (housingViewModel.NewScenery != null) { byte[] imageData = null; using (var binaryReader = new BinaryReader(housingViewModel.NewScenery.OpenReadStream())) { imageData = binaryReader.ReadBytes((int)housingViewModel.NewScenery.Length); } housingDto.Scenery = imageData; } await _housingManager.CreateAsync(housingDto); return(RedirectToAction("Index", "Housing")); } return(View(housingViewModel)); }
private static bool ValidateToUpdate(Housing housing, HousingDto housingDto) { bool updated = false; if (housing.Name != housingDto.Name) { housing.Name = housingDto.Name; updated = true; } if (housing.Place.Name != housingDto.Place.Name) { housing.Place.Name = housingDto.Place.Name; updated = true; } if (housing.Address != housingDto.Address) { housing.Address = housingDto.Address; updated = true; } if (housing.Scenery != housingDto.Scenery && housingDto.Scenery != null) { housing.Scenery = housingDto.Scenery; updated = true; } if (housing.Description != housingDto.Description) { housing.Description = housingDto.Description; updated = true; } if (housing.PricePerDay != housingDto.PricePerDay) { housing.PricePerDay = housingDto.PricePerDay; updated = true; } return(updated); }
public async Task UpdateHousingAsync(HousingDto housingDto, string userId) { housingDto = housingDto ?? throw new ArgumentNullException(nameof(housingDto)); var housing = await _repositoryHousing .GetAll() .Include(x => x.Place) .FirstOrDefaultAsync(housings => housings.Id == housingDto.Id && housings.UserId == userId); if (housing is null) { throw new KeyNotFoundException(ErrorResource.HousingNotFound); } var result = ValidateToUpdate(housing, housingDto); if (result) { _repositoryPlace.Update(housing.Place); _repositoryHousing.Update(housing); await _repositoryHousing.SaveChangesAsync(); } }
public HousingViewModel(HousingDto housing) { _housingDto = housing; }
private bool UpdateHousing(HousingDto housing) { return(false); }
private bool AddNewHousing(HousingDto housing) { return(false); }