Exemplo n.º 1
0
 public async Task <Rent> CheckIfSuchRentAlreadyExist(RentForCreationDto rent)
 {
     return(await FindByCondition(r => (
                                      r.PropertyId.Equals(rent.PropertyId) &&
                                      r.TenantId.Equals(rent.TenantId) &&
                                      r.LandlordId.Equals(rent.LandlordId) &&
                                      r.StartRent.Equals(rent.StartRent) &&
                                      r.EndRent.Equals(rent.EndRent) &&
                                      r.RentPurpose.Equals(rent.RentPurpose)
                                      )
                                  ).FirstOrDefaultAsync());
 }
        public async Task <IActionResult> CreateRent([FromBody] RentForCreationDto rent)
        {
            try
            {
                if (rent == null)
                {
                    _logger.LogError("Rent received is a Null Object.");
                    return(BadRequest("Rent object is null. Please send full request."));
                }
                else if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid Rent object sent from client.");
                    return(BadRequest("Rent object is not Valid"));
                }
                else if (rent.StartRent > rent.EndRent)
                {
                    _logger.LogError("Invalid Rent object sent from client. StartRent date is <= EndRent date.");
                    return(BadRequest("StartRent must be before EndRent."));
                }

                Boolean propertyExist = await _repositoryWrapper.Property.CheckIfPropertyExistByPropertyId(rent.PropertyId);

                Boolean tenantExist = await _repositoryWrapper.Tenant.CheckIfTenantExistByTenantId(rent.TenantId);

                Boolean landlordExist = await _repositoryWrapper.Landlord.CheckIfLandlordExistByLandlordId(rent.LandlordId);

                if (!propertyExist)
                {
                    _logger.LogError("User tried to create new Rent with non existing PropertyId");
                    return(BadRequest($"Not created. Property with id: {rent.PropertyId} does not exist in DB."));
                }
                else if (!tenantExist)
                {
                    _logger.LogError("User tried to create new Rent with non existing TenantId");
                    return(BadRequest($"Not created. Tenant with id: {rent.TenantId} does not exist in DB."));
                }
                else if (!landlordExist)
                {
                    _logger.LogError("User tried to create new Rent with non existing LanlordId");
                    return(BadRequest($"Not created. Landlord with id: {rent.LandlordId} does not exist in DB."));
                }

                var rentAlreadyInDB = await _repositoryWrapper.Rent.CheckIfSuchRentAlreadyExist(rent);

                if (rentAlreadyInDB != null)
                {
                    _logger.LogError("Rent with given PropertyId already exist in Db, cannot create.");
                    return(BadRequest("Rent already in Database, cannot insert same rent twice."));
                }

                var rentEntity = _mapper.Map <Rent>(rent);

                _repositoryWrapper.Rent.CreateRent(rentEntity);
                await _repositoryWrapper.Save();

                var createdRent = _mapper.Map <RentDto>(rentEntity);
                return(CreatedAtRoute("RentById", new { id = createdRent.Id }, createdRent));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside CreateRent(RentForCreationDto) action: {e.Message}");
                return(StatusCode(500, e.Message));
            }
        }