public IActionResult UpdateTenant(int tenantId, [FromBody] TenantForUpdateDto tenantForUpdateDto) { if (!_repo.TenantExists(tenantId)) { return(NotFound()); } if (tenantForUpdateDto == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var tenantEntity = Mapper.Map <Tenant>(tenantForUpdateDto); _repo.UpdateTenant(tenantEntity); if (!_repo.Save()) { return(StatusCode(500, "An error occurred while processing you request.")); } var tenantDto = Mapper.Map <TenantDto>(tenantEntity); return(CreatedAtRoute(tenantId, tenantDto)); }
public async Task <IActionResult> UpdateTenant(int id, [FromBody] TenantForUpdateDto tenant) { try { int landlordId = await _repositoryWrapper.Rent.FindLandlordIdByTenantId(id); if (HttpContext.SelectLoggedUserId() != landlordId) { return(Unauthorized()); } if (tenant == null) { _logger.LogError("Tenant received is a Null Object."); return(BadRequest("Tenant object is null. Please send full request.")); } if (!ModelState.IsValid) { _logger.LogError("Invalid Tenant object sent from client."); return(BadRequest("Tenant object is not Valid")); } // var tenantEntity = await _repositoryWrapper.Tenant.GetTenantsById(id); var tenantEntity = await _repositoryWrapper.Tenant.GetTenantWithAddress(id); // Check if any person returned before updating if (tenantEntity == null) { _logger.LogError($"Tenant with id: {id}, hasn't been found in db."); return(NotFound("Tenant not found.")); } _mapper.Map(tenant, tenantEntity); _repositoryWrapper.Tenant.UpdateTenant(tenantEntity); await _repositoryWrapper.Save(); return(NoContent()); } catch (Exception e) { _logger.LogError($"Something went wrong inside UpdateTenant(id, tenantForUpdateDto) action: {e.ToString()}"); return(StatusCode(500, e.Message)); } }
// [Authorize(Policy = "CanWriteTenant")] public async Task <ActionResult <Tenant> > UpdateTenant(TenantForUpdateDto tenant) { try { if (tenant == null) { _logger.LogError("Tenant object sent from client is null."); return(BadRequest("Tenant object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid tenant object sent from client."); return(BadRequest("Invalid model object")); } var tenantEntity = _repository.Tenant.GetTenantById(tenant.TenantId); if (tenantEntity == null) { _logger.LogError($"Tenant with id: {tenant.TenantId}, hasn't been found in db."); return(NotFound()); } _mapper.Map(tenant, tenantEntity); _repository.Tenant.UpdateTenant(tenantEntity); _repository.Save(); return(CreatedAtRoute("GetTenantById", new { id = tenantEntity.TenantId }, tenantEntity)); //return NoContent(); } catch (Exception ex) { _logger.LogError($"Something went wrong inside UpdateTenant action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }