Let me explain the scenario for better understanding.
I've developed API in .NET core 6. There are few layers in API project.
All the data manipulation is happening in service layer and I am managing exception in service layer only. To standardized the response I am returning one object which contains three members.
1.Data - If response is ok then sending data else it will be null.
2.Status - response status code
3.Message - custom message
Now the issues is because I am handing everything in service layer, Controller will always return response with status code 200. Actual status is in the custom object.
Controller Code
[HttpPost]
[Route("login")]
[AllowAnonymous]
public IActionResult Login([FromBody] LoginVM loginVM)
{
loginVM.Password = loginVM.Password.Encrypt();
CurrentResponse response = _accountService.GetValidUser(loginVM);
User user = (User)(response.Data);
if (user != null)
{
response = GetDetails(response, user, loginVM.TimeZone);
return Ok(response);
}
return Ok(response);
}
Service Layer Code
public CurrentResponse GetValidUser(LoginVM loginVM)
{
try
{
User user = _accountRepository.GetValidUser(loginVM.Email, loginVM.Password);
if (user == null)
{
CreateResponse(null, HttpStatusCode.NotFound, "Invalid Credentials");
return _currentResponse;
}
UserVSCompany userVSCompany = _userVSCompanyRepository.GetDefaultCompanyByUserId(user.Id);
string companyId = "0";
if(userVSCompany != null)
{
companyId = userVSCompany.CompanyId.ToString();
user.CompanyId = userVSCompany.CompanyId;
}
user.ImageName = $"{Configuration.ConfigurationSettings.Instance.UploadDirectoryPath}/{UploadDirectories.UserProfileImage}/{companyId}/{user.ImageName}";
Company company = _companyRepository.FindByCondition(p => p.Id == user.CompanyId);
if(company != null)
{
if(!company.IsActive)
{
CreateResponse(null, HttpStatusCode.NotFound, "Your organisation is not activated");
return _currentResponse;
}
if(company.IsDeleted)
{
CreateResponse(null, HttpStatusCode.NotFound, "Organization has been deleted");
return _currentResponse;
}
}
if (!user.IsActive)
{
CreateResponse(null, HttpStatusCode.NotFound, "Your account is not activated");
}
else if (user.IsDeleted)
{
CreateResponse(null, HttpStatusCode.NotFound, "Your account has been deleted");
}
else
{
if (company != null)
{
user.CompanyName = company.Name;
}
var userRoleDetails = _userRoleRepository.FindByUserIdAndCompanyId(user.Id, user.CompanyId);
user.RoleName = userRoleDetails.Name;
user.RoleId = userRoleDetails.Id;
CreateResponse(user, HttpStatusCode.OK, "User is valid");
}
return _currentResponse;
}
catch (Exception exc)
{
CreateResponse(null, HttpStatusCode.InternalServerError, exc.ToString());
return _currentResponse;
}
}
Now mobile developer has to write condition to check actual status because of my custom object.
Solution I can change the code in controller where I need to check status of my custom object but this is not a proper way as I need to make changes in whole project.
How can I create a response wrapper middleware where I can read the response object and set status code of response?
Any kind of suggestion would be appreciated.