public async Task <ResultWithMessage> BuyAppartamentsAsync(User user, int appartamentId) { ResultWithMessage result = new ResultWithMessage(false); IGenericRepository <Appartament> appartamentRepo = _unitOfWork.Repository <Appartament>(); AppartamentWithUsersSpecification appartamentSpec = new AppartamentWithUsersSpecification(appartamentId); Appartament appartament = await appartamentRepo.GetEntityWithSpec(appartamentSpec); if (appartament.Cost > user.Money) { result.Message = "You can`t buy this appartament. You need more money"; return(result); } if (!await _userService.RecalculateMoney(user, appartament.Cost)) { result.Message = "Cannot recalculate money"; return(result); } UserAppartament ownerAppartament = new UserAppartament { UserId = user.Id, AppartamentId = appartamentId }; _unitOfWork.Repository <UserAppartament>() .Add(ownerAppartament); if (!await _unitOfWork.Complete()) { result.Message = "Cannot complete operation please try later"; return(result); } result.IsSuccess = true; result.Message = "Congratulations! You bought new appartament"; return(result); }
public async Task <AppartamentViewDto> GetAppartament(int id) { Appartament appartament = await _appartamentRepo.GetByIdAsync(id); return(_mapper.Map <Appartament, AppartamentViewDto>(appartament)); }