Skip to content

Instantly share code, notes, and snippets.

@fxcosta
Created July 22, 2021 19:01
Show Gist options
  • Save fxcosta/ca0daef458c13fb461cbd3366a37052e to your computer and use it in GitHub Desktop.
Save fxcosta/ca0daef458c13fb461cbd3366a37052e to your computer and use it in GitHub Desktop.
[HttpPost("list-grid/{externalSystemId}/{area:int}/{externalId:int}")]
[ProducesResponseType(200, Type = typeof(DataSourceResult))]
[ProducesResponseType(400)]
[ProducesResponseType(403)]
[Authorize(Policies.ViewAllComplaint)]
public DataSourceResult GetComplaintByAreaDataSource([FromBody] DataSourceRequest request, string externalSystemId, int area = -1, int externalId = -1)
{
var citizens = _unitOfWork.Citizens;
var areas = _unitOfWork.AreaOfInterests;
var occurrences = _unitOfWork.Occurrences;
var mainStates = _unitOfWork.ComplaintMainStates;
var secondaryStates = _unitOfWork.ComplaintSecondaryStates;
var registers = _unitOfWork.Registers;
var organization = _context.Organizations
.Where(o => o.ExternalId == externalId)
.Where(o => o.ExternalSystemId.ToString() == externalSystemId)
.FirstOrDefault();
if (organization == null)
{
throw new Exception("Organização não encontrada. Por favor, revise o parametro de organização");
}
var user = new ApplicationUser
{
Id = _context.CurrentUserId
};
IEnumerable<Complaint> complaints;
var complaintsResult = _context.Complaints;
IEnumerable<Complaint> queryResults;
if (area != -1)
{
// complaints = _unitOfWork.Complaints.Find(x => x.AreaOfInterestId == area);
queryResults = _context.Complaints
.Where(c => c.OrganizationId == organization.Id && c.AreaOfInterestId == area).ToList();
}
else
{
// complaints = _unitOfWork.Complaints.GetAll();
queryResults = _context.Complaints.Where(c => c.OrganizationId == organization.Id).ToList();
}
var results = queryResults.Select(x =>
{
x.Citizen = citizens.GetById(x.CitizenId);
x.AreaOfInterest = areas.GetById(x.AreaOfInterestId);
x.Occurrence = occurrences.GetById(x.OccurrenceId);
x.MainState = mainStates.GetById((int)x.MainStateId);
x.SecondaryState = secondaryStates.GetById((int)x.SecondaryStateId);
x.LastRegister = registers.GetById((int)x.LastRegisterId);
x.NextActions = user != null ? _actionController.NextActionsFor(_context.CurrentUserId, (int)x.LastRegisterId).ToArray() : null;
return Mapper.Map<ComplaintViewModel>(x);
})
.AsQueryable()
.ToDataSourceResult(request);
// return Ok(Mapper.Map<IEnumerable<ComplaintViewModel>>(result));
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment