Add EmployeeId as filter in the endpoints, add endpoints to get all active and historical surveys for a particular employee

This commit is contained in:
Reginald Cherenfant Jasmin 2023-09-21 00:58:29 -04:00
parent 8d386af40a
commit a0033ca045
10 changed files with 347 additions and 143 deletions

View File

@ -1,8 +1,6 @@
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace DamageAssesment.Api.SurveyResponses.Controllers
{
@ -19,10 +17,12 @@ namespace DamageAssesment.Api.SurveyResponses.Controllers
/// GET request for retrieving survey responses.
/// </summary>
[HttpGet("Responses")]
public async Task<ActionResult> GetSurveyResponsesAsync()
[Route("Responses/{employeeid:int}")]
[Route("Responses")]
[HttpGet]
public async Task<ActionResult> GetSurveyResponsesAsync(int? employeeid)
{
var result = await this.surveyResponseProvider.GetSurveyResponsesAsync();
var result = await this.surveyResponseProvider.GetSurveyResponsesAsync(employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.surveyResponses);
@ -36,11 +36,12 @@ namespace DamageAssesment.Api.SurveyResponses.Controllers
/// <summary>
/// GET request for retrieving survey responses by survey ID.
/// </summary>
[HttpGet("Responses/BySurvey/{surveyid}")]
public async Task<ActionResult> GetSurveyResponsesAsync(int surveyid)
[Route("Responses/BySurvey/{surveyid:int}/{employeeid:int}")]
[Route("Responses/BySurvey/{surveyid:int}")]
[HttpGet]
public async Task<ActionResult> GetSurveyResponsesAsync(int surveyid, int? employeeid)
{
var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAsync(surveyid);
var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAsync(surveyid, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.SurveyResponses);
@ -53,15 +54,16 @@ namespace DamageAssesment.Api.SurveyResponses.Controllers
/// <param name="surveyid">The ID of the survey for which responses are to be retrieved.</param>
/// <param name="locationid">The ID of the location for which responses are to be retrieved.</param>
[HttpGet("Responses/{surveyid}/{locationid}")]
public async Task<ActionResult> GetSurveyResponsesBySurveyAndLocationAsync(int surveyid, int locationid)
[Route("Responses/{surveyid:int}/{locationid:int}/{employeeid:int}")]
[Route("Responses/{surveyid:int}/{locationid:int}")]
[HttpGet]
public async Task<ActionResult> GetSurveyResponsesBySurveyAndLocationAsync(int surveyid, int locationid,int? employeeid)
{
var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(surveyid, locationid);
var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(surveyid, locationid,employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.SurveyResponses);
}
return NoContent();
}
/// <summary>
@ -71,10 +73,12 @@ namespace DamageAssesment.Api.SurveyResponses.Controllers
/// <param name="questionId">The ID of the question for which responses are to be retrieved.</param>
/// <param name="answer">The answer for which responses are to be retrieved.</param>
[HttpGet("Responses/ByAnswer/{surveyid}/{questionid}/{answer}")]
public async Task<ActionResult> GetSurveyResponsesByAnswerAsyncAsync(int surveyid, int questionid, string answer)
[Route("Responses/ByAnswer/{surveyid:int}/{questionid:int}/{answer:alpha}/{employeeid:int}")]
[Route("Responses/ByAnswer/{surveyid:int}/{questionid:int}/{answer:alpha}")]
[HttpGet]
public async Task<ActionResult> GetSurveyResponsesByAnswerAsyncAsync(int surveyid, int questionid, string answer, int? employeeid)
{
var result = await surveyResponseProvider.GetResponsesByAnswerAsync(surveyid, questionid, answer);
var result = await surveyResponseProvider.GetResponsesByAnswerAsync(surveyid, questionid, answer, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.SurveyResponses);
@ -87,10 +91,12 @@ namespace DamageAssesment.Api.SurveyResponses.Controllers
/// </summary>
/// <param name="surveyId">The ID of the survey for which answers are to be retrieved.</param>
[HttpGet("Responses/ByRegion/{surveyid}")]
public async Task<ActionResult> GetAnswersByRegionAsync(int surveyid)
[Route("Responses/ByRegion/{surveyid:int}")]
[Route("Responses/ByRegion/{surveyid:int}/{employeeid}")]
[HttpGet]
public async Task<ActionResult> GetAnswersByRegionAsync(int surveyid, int? employeeid)
{
var result = await this.surveyResponseProvider.GetAnswersByRegionAsync(surveyid);
var result = await this.surveyResponseProvider.GetAnswersByRegionAsync(surveyid, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.Answers);
@ -101,11 +107,12 @@ namespace DamageAssesment.Api.SurveyResponses.Controllers
/// GET request for retrieving survey responses by survey ID and maintenance center.
/// </summary>
/// <param name="surveyId">The ID of the survey for which responses are to be retrieved.</param>
[HttpGet("Responses/ByMaintenanceCenter/{surveyid}")]
public async Task<ActionResult> GetAnswersByMaintenaceCentersync(int surveyid)
[Route("Responses/ByMaintenanceCenter/{surveyid:int}/{employeeid:int}")]
[Route("Responses/ByMaintenanceCenter/{surveyid:int}")]
[HttpGet]
public async Task<ActionResult> GetAnswersByMaintenaceCentersync(int surveyid, int? employeeid)
{
var result = await this.surveyResponseProvider.GetSurveyResponsesByMaintenanceCenterAsync(surveyid);
var result = await this.surveyResponseProvider.GetSurveyResponsesByMaintenanceCenterAsync(surveyid, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.SurveyResponses);
@ -191,5 +198,31 @@ namespace DamageAssesment.Api.SurveyResponses.Controllers
else
return BadRequest(result.ErrorMessage);
}
[Route("Responses/Surveys/active/{employeeid:int}")]
[Route("Responses/Surveys/active/{employeeid:int}/{language:alpha}")]
[HttpGet]
public async Task<ActionResult> GetActiveSurveysAsync(int employeeid, string? language)
{
var result = await this.surveyResponseProvider.GetActiveSurveysAsync(employeeid, language);
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NoContent();
}
[Route("Responses/Surveys/historic/{employeeid:int}")]
[Route("Responses/Surveys/historic/{employeeid:int}/{language:alpha}")]
[HttpGet]
public async Task<ActionResult> GetHistoricSurveysAsync(int employeeid, string? language)
{
var result = await this.surveyResponseProvider.GetHistoricSurveysAsync(employeeid, language);
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NoContent();
}
}
}

View File

@ -4,7 +4,7 @@ namespace DamageAssesment.Api.SurveyResponses.Interfaces
{
public interface ISurveyServiceProvider
{
Task<List<Survey>> getSurveysAsync();
Task<List<Survey>> getSurveysAsync(string language);
Task<Survey> getSurveyAsync(int surveyId);
}
}

View File

@ -5,18 +5,19 @@ namespace DamageAssesment.Api.SurveyResponses.Interfaces
{
public interface ISurveysResponse
{
Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId);
Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId, int employeeid);
Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyResponseAsync(Models.SurveyResponse surveyResponse);
// Task<(bool IsSuccess,dynamic surveyResponses, string ErrorMessage)> GetSurveyResponseAsync(int responseId);
Task<(bool IsSuccess, dynamic surveyResponses, string ErrorMessage)> GetSurveyResponsesAsync();
Task<(bool IsSuccess, dynamic surveyResponses, string ErrorMessage)> GetSurveyResponsesAsync(int employeeid);
Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetActiveSurveysAsync(int employeeid, string language);
Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetHistoricSurveysAsync(int employeeid, string language);
Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PutSurveyResponseAsync(int Id, Models.SurveyResponse surveyResponse);
Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> DeleteSurveyResponseAsync(int Id);
Task<(bool IsSuccess, dynamic SurveyResponse, string ErrorMessage)> GetSurveyResponseByIdAsync(int responseId);
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAsync(int surveyId);
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, int locationId);
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesByMaintenanceCenterAsync(int surveyId);
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetResponsesByAnswerAsync(int surveyId, int questionId, string answer);
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAsync(int surveyId, int employeeid);
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, int locationId, int employeeid);
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesByMaintenanceCenterAsync(int surveyId, int employeeid);
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetResponsesByAnswerAsync(int surveyId, int questionId, string answer, int employeeid);
Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Request request);
}

View File

@ -31,7 +31,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
this.questionServiceProvider = questionServiceProvider;
this.surveyServiceProvider = surveyServiceProvider;
this.mapper = mapper;
// seedData();
// seedData();
}
private void seedData()
@ -48,12 +48,20 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
}
public async Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId)
public async Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId, int employeeid)
{
try
{
logger?.LogInformation("Querying to get SurveyResponse object from DB");
var listSurveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.SurveyId == surveyId);
IQueryable<Db.SurveyResponse> listSurveyResponse = null;
if (employeeid == 0)
{
listSurveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.SurveyId == surveyId);
}
else
{
listSurveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.SurveyId == surveyId && s.EmployeeId == employeeid);
}
if (listSurveyResponse.Any())
{
@ -103,9 +111,57 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
}
public async Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetActiveSurveysAsync(int employeeid, string language)
{
try
{
logger?.LogInformation("Querying to get SurveyResponse object from DB");
//get all the survey that already taken by the employee
var listOfsurveysId = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).Select(y => y.SurveyId).ToListAsync();
var surveys = await surveyServiceProvider.getSurveysAsync(language);
if (surveys != null)
{
surveys = surveys.Where(s => s.IsEnabled == true && s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now).ToList();
}
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAsync(int surveyId)
if (listOfsurveysId==null || listOfsurveysId.Count == 0)
return (true, surveys, null);
var activeSurveys = surveys.Where(s => s.IsEnabled == true && s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now && !listOfsurveysId.Contains(s.Id));
return (true, activeSurveys, null);
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetHistoricSurveysAsync(int employeeid, string language)
{
try
{
logger?.LogInformation("Querying to get SurveyResponse object from DB");
//get all the survey that already taken by the employee
var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync();
var surveys = await surveyServiceProvider.getSurveysAsync(language);
var historicSurveys = from s in surveys
from r in surveyResponses
where s.Id == r.SurveyId
select s;
return (true, historicSurveys, null);
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAsync(int surveyId, int employeeid)
{
try
{
@ -114,7 +170,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
if (survey != null)
{
var answers = await getSurveyResponsesBySurveyIdAsync(surveyId);
var answers = await getSurveyResponsesBySurveyIdAsync(surveyId, employeeid);
if (answers != null)
return (true, answers, "Request Successful.");
@ -136,7 +192,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
}
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, int locationId)
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, int locationId, int employeeid)
{
try
{
@ -145,7 +201,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
if (survey != null)
{
var answers = await getSurveyResponsesBySurveyIdLocationIdAsync(surveyId, locationId);
var answers = await getSurveyResponsesBySurveyIdLocationIdAsync(surveyId, locationId, employeeid);
if (answers != null)
return (true, answers, "Request Successful.");
@ -167,7 +223,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
}
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesByMaintenanceCenterAsync(int surveyId)
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesByMaintenanceCenterAsync(int surveyId, int employeeid)
{
try
{
@ -176,7 +232,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
if (survey != null)
{
var answers = await getResultsByMaintenanceCenterAsync(surveyId);
var answers = await getResultsByMaintenanceCenterAsync(surveyId, employeeid);
if (answers != null)
return (true, answers, "Request Successful.");
@ -198,7 +254,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
}
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetResponsesByAnswerAsync(int surveyId, int questionId, string answer)
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetResponsesByAnswerAsync(int surveyId, int questionId, string answer, int employeeid)
{
try
{
@ -210,7 +266,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
if (survey != null && question != null && IsCorrectAnswer)
{
var answers = await getSurveyResponsesByAnswerAsync(survey, question, answer);
var answers = await getSurveyResponsesByAnswerAsync(survey, question, answer, employeeid);
if (answers != null)
return (true, answers, "Request Successful.");
@ -233,18 +289,18 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
public async Task<(bool IsSuccess, dynamic surveyResponses, string ErrorMessage)> GetSurveyResponsesAsync()
public async Task<(bool IsSuccess, dynamic surveyResponses, string ErrorMessage)> GetSurveyResponsesAsync(int employeeid)
{
try
{
var answers = await getAllSurveyResponsesAsync();
var responses = await getAllSurveyResponsesAsync(employeeid);
if (answers != null)
return (true, answers, "Request Successful.");
if (responses != null)
return (true, responses, "Request Successful.");
else
{
answers = new List<Models.SurveyResponse>();
return (true, answers, "Empty object returned");
responses = new List<Models.SurveyResponse>();
return (true, responses, "Empty object returned");
}
}
catch (Exception ex)
@ -381,13 +437,13 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
where answer.LocationId.Equals(location.Id)
select new
{
answer.Id,
answer.QuestionId,
answer.AnswerText,
answer.Comment,
location.RegionId,
LocationId = location.Id,
answer.SurveyResponseId
answer.Id,
answer.QuestionId,
answer.AnswerText,
answer.Comment,
location.RegionId,
LocationId = location.Id,
answer.SurveyResponseId
};
@ -414,7 +470,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
where u.RegionId.Equals(region.Id)
select u.Answers;
resultList.Add(new { RegionId = region.Id, region.Name, region.Abbreviation, Answers = answers});
resultList.Add(new { RegionId = region.Id, region.Name, region.Abbreviation, Answers = answers });
}
//return the object result
return new { Regions = resultList };
@ -470,13 +526,24 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
//Method to get Survey Responses by surveyId
private async Task<dynamic> getSurveyResponsesBySurveyIdAsync(int surveyId)
private async Task<dynamic> getSurveyResponsesBySurveyIdAsync(int surveyId, int employeeid)
{
try
{
var surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync();
List<Db.SurveyResponse> surveyResonses = null;
Employee employee = null;
List<Employee> employees = null;
if (employeeid == 0)
{
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync();
employees = await employeeServiceProvider.getEmployeesAsync();
}
else
{
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync();
employee = await employeeServiceProvider.getEmployeeAsync(employeeid);
}
var employees = await employeeServiceProvider.getEmployeesAsync();
var answers = await answerServiceProvider.getAnswersAsync();
var questions = await questionServiceProvider.getQuestionsAsync();
var surveyQuestions = from q in questions where q.SurveyId == surveyId select q;
@ -484,32 +551,70 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
//var surveyQuestions = await questionServiceProvider.getSurveyQuestionsAsync(surveyId);
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
var result = from r in surveyResonses
select new
{
r.Id,
r.SurveyId,
r.LocationId,
r.EmployeeId,
r.ClientDevice,
r.KeyAnswerResult,
r.Longitute,
r.Latitude,
Employee = (from e in employees where e.Id == r.EmployeeId select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
answers = from ans in answers
where ans.SurveyResponseId == r.Id
select new
{
ans.Id,
ans.QuestionId,
ans.AnswerText,
ans.Comment,
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
};
return result;
if (employeeid == 0)
{
var result = from r in surveyResonses
select new
{
r.Id,
r.SurveyId,
r.LocationId,
r.EmployeeId,
r.ClientDevice,
r.KeyAnswerResult,
r.Longitute,
r.Latitude,
Employee = (from e in employees where e.Id == r.EmployeeId select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
answers = from ans in answers
where ans.SurveyResponseId == r.Id
select new
{
ans.Id,
ans.QuestionId,
ans.AnswerText,
ans.Comment,
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
};
return result;
}
else
{
object _employee = new { };
if (employee != null)
{
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber };
}
var result = from r in surveyResonses
select new
{
r.Id,
r.SurveyId,
r.LocationId,
r.EmployeeId,
r.ClientDevice,
r.KeyAnswerResult,
r.Longitute,
r.Latitude,
Employee = _employee,
answers = from ans in answers
where ans.SurveyResponseId == r.Id
select new
{
ans.Id,
ans.QuestionId,
ans.AnswerText,
ans.Comment,
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
};
return result;
}
}
catch (Exception ex)
{
@ -520,13 +625,31 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
//Method to get All Survey Responses
private async Task<dynamic> getAllSurveyResponsesAsync()
private async Task<dynamic> getAllSurveyResponsesAsync(int employeeid)
{
try
{
var surveyResonses = await surveyResponseDbContext.SurveyResponses.ToListAsync();
List<Db.SurveyResponse> surveyResonses = null;
Employee employee = null;
List<Employee> employees = null;
object _employee = new { };
if (employeeid == 0)
{
surveyResonses = await surveyResponseDbContext.SurveyResponses.ToListAsync();
employees = await employeeServiceProvider.getEmployeesAsync();
}
else
{
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync();
employee = await employeeServiceProvider.getEmployeeAsync(employeeid);
if (employee != null)
{
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber };
}
}
var employees = await employeeServiceProvider.getEmployeesAsync();
var answers = await answerServiceProvider.getAnswersAsync();
var questions = await questionServiceProvider.getQuestionsAsync();
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
@ -542,7 +665,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
r.KeyAnswerResult,
r.Longitute,
r.Latitude,
Employee = (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
answers = from ans in answers
where ans.SurveyResponseId == r.Id
select new
@ -556,6 +679,8 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
};
return result;
}
catch (Exception ex)
{
@ -566,11 +691,19 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
//Method to get Answers By Maintenance Center by surveyId
private async Task<dynamic> getResultsByMaintenanceCenterAsync(int surveyId)
private async Task<dynamic> getResultsByMaintenanceCenterAsync(int surveyId, int employeeid)
{
try
{
var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync();
List<Db.SurveyResponse> surveyResponses = null;
if (employeeid == 0)
{
surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync();
}
else
{
surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync();
}
var answers = await answerServiceProvider.getAnswersAsync();
var locations = await locationServiceProvider.getLocationsAsync();
var maintenanceCenters = locations.DistinctBy(m => m.MaintenanceCenter);
@ -626,13 +759,31 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
//Method to get Survey Responses by surveyId and LocationId
private async Task<dynamic> getSurveyResponsesBySurveyIdLocationIdAsync(int surveyId, int locationId)
private async Task<dynamic> getSurveyResponsesBySurveyIdLocationIdAsync(int surveyId, int locationId, int employeeid)
{
try
{
var surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.LocationId.Equals(locationId)).ToListAsync();
List<Db.SurveyResponse> surveyResonses = null;
Employee employee = null;
List<Employee> employees = null;
object _employee = new { };
if (employeeid == 0)
{
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.LocationId == locationId).ToListAsync();
employees = await employeeServiceProvider.getEmployeesAsync();
}
else
{
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid && x.LocationId == locationId).ToListAsync();
employee = await employeeServiceProvider.getEmployeeAsync(employeeid);
if (employee != null)
{
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber };
}
}
var employees = await employeeServiceProvider.getEmployeesAsync();
var answers = await answerServiceProvider.getAnswersAsync();
var questions = await questionServiceProvider.getQuestionsAsync();
var surveyQuestions = from q in questions where q.SurveyId == surveyId select q;
@ -649,7 +800,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
r.KeyAnswerResult,
r.Longitute,
r.Latitude,
Employee = (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
answers = from ans in answers
where ans.SurveyResponseId == r.Id
@ -664,6 +815,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
};
return result;
}
catch (Exception ex)
{
@ -674,13 +826,34 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
//Method to get Survey Responses by surveyId questionId and answer
private async Task<dynamic> getSurveyResponsesByAnswerAsync(Survey survey, Question question, string answer)
private async Task<dynamic> getSurveyResponsesByAnswerAsync(Survey survey, Question question, string answer, int employeeid)
{
try
{
var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id).ToListAsync();
List<Db.SurveyResponse> surveyResponses = null;
Employee employee = null;
List<Employee> employees = null;
object _employee = new { };
if (employeeid == 0)
{
surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id).ToListAsync();
employees = await employeeServiceProvider.getEmployeesAsync();
}
else
{
surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id && x.EmployeeId == employeeid).ToListAsync();
employee = await employeeServiceProvider.getEmployeeAsync(employeeid);
if (employee != null)
{
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber };
}
}
//var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id).ToListAsync();
// var employees = await employeeServiceProvider.getEmployeesAsync();
var answers = await answerServiceProvider.getAnswersAsync();
var employees = await employeeServiceProvider.getEmployeesAsync();
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
var result = from r in surveyResponses
@ -694,7 +867,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
r.KeyAnswerResult,
r.Longitute,
r.Latitude,
Employee = (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
answers = from ans in answers
where ans.SurveyResponseId == r.Id
&& ans.QuestionId == question.Id
@ -757,7 +930,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
{
if (request != null)
{
var response = await PostSurveyResponseAsync(new Models.SurveyResponse { SurveyId = request.SurveyId, EmployeeId = request.EmployeeId, LocationId = request.LocationId, ClientDevice = request.ClientDevice, KeyAnswerResult = request.KeyAnswerResult, Latitude = Convert.ToDouble(request.Latitude), Longitute = Convert.ToDouble(request.Longitute), CreatedDate=DateTime.Now });
var response = await PostSurveyResponseAsync(new Models.SurveyResponse { SurveyId = request.SurveyId, EmployeeId = request.EmployeeId, LocationId = request.LocationId, ClientDevice = request.ClientDevice, KeyAnswerResult = request.KeyAnswerResult, Latitude = Convert.ToDouble(request.Latitude), Longitute = Convert.ToDouble(request.Longitute), CreatedDate = DateTime.Now });
if (response.IsSuccess)
{
var surveyResponse = response.SurveyResponse;

View File

@ -4,16 +4,18 @@ using Newtonsoft.Json;
namespace DamageAssesment.Api.SurveyResponses.Services
{
public class SurveyServiceProvider :ServiceProviderBase, ISurveyServiceProvider
public class SurveyServiceProvider : ServiceProviderBase, ISurveyServiceProvider
{
public SurveyServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Survey"), configuration.GetValue<string>("EndPointSettings:SurveyUrlBase"))
{
}
public async Task<List<Survey>> getSurveysAsync()
public async Task<List<Survey>> getSurveysAsync(string language)
{
try
{
if (!string.IsNullOrEmpty(language))
url = url + "/" + language;
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
var surveys = JsonConvert.DeserializeObject<List<Survey>>(responseJsonString);
@ -36,7 +38,7 @@ namespace DamageAssesment.Api.SurveyResponses.Services
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
var survey = JsonConvert.DeserializeObject<Survey>(responseJsonString);
if (survey == null )
if (survey == null)
return null;
else return survey;
}

View File

@ -15,7 +15,6 @@ namespace DamageAssesment.Api.Surveys.Controllers
/// <summary>
/// GET request for retrieving surveys.
/// </summary>
[Route("Surveys")]
[Route("Surveys/{language:alpha}")]
[HttpGet]
@ -47,7 +46,6 @@ namespace DamageAssesment.Api.Surveys.Controllers
/// <summary>
/// POST request for creating a new survey.
/// </summary>
[HttpPost("Surveys")]
public async Task<ActionResult> PostSurveysAsync(Models.Survey survey)
{
@ -61,8 +59,6 @@ namespace DamageAssesment.Api.Surveys.Controllers
/// <summary>
/// PUT request for updating an existing survey (surveyId,Updated Survey data).
/// </summary>
[HttpPut("Surveys/{id}")]
public async Task<ActionResult> PutSurveysAsync(int id, Models.Survey survey)
{

View File

@ -24,7 +24,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DamageAssesment.Api.SurveyResponses\DamageAssesment.Api.SurveyResponses.csproj" />
<ProjectReference Include="..\DamageAssesment.Api.SurveyResponses\DamageAssesment.Api.Responses.csproj" />
</ItemGroup>
</Project>

View File

@ -3,10 +3,9 @@ using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using DamageAssesment.Api.SurveyResponses.Test;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Moq;
using Xunit;
using Xunit.Sdk;
namespace DamageAssesment.SurveyResponses.Test
{
@ -23,9 +22,9 @@ namespace DamageAssesment.SurveyResponses.Test
{
SurveyResponse mockRequestObject = await MockData.getSurveyResponseObject();
var mockResponse = await MockData.getOkResponse(mockRequestObject);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesAsync()).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesAsync(1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (OkObjectResult)await surveyResponseProvider.GetSurveyResponsesAsync();
var result = (OkObjectResult)await surveyResponseProvider.GetSurveyResponsesAsync(1);
Assert.Equal(200, result.StatusCode);
}
@ -33,9 +32,9 @@ namespace DamageAssesment.SurveyResponses.Test
public async Task GetSurveyResponsesAsync_ShouldReturnStatusCode204()
{
var mockResponse = await MockData.getResponse();
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesAsync()).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesAsync(1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (BadRequestObjectResult)await surveyResponseProvider.GetSurveyResponsesAsync();
var result = (BadRequestObjectResult)await surveyResponseProvider.GetSurveyResponsesAsync(1);
Assert.Equal(400, result.StatusCode);
}
@ -44,7 +43,7 @@ namespace DamageAssesment.SurveyResponses.Test
{
SurveyResponse mockRequestObject = await MockData.getSurveyResponseObject();
var mockResponse = await MockData.getOkResponse();
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesBySurveyAsync(1)).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesBySurveyAsync(1,1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (OkObjectResult)await surveyResponseProvider.GetSurveyResponsesAsync(1);
Assert.Equal(200, result.StatusCode);
@ -54,7 +53,7 @@ namespace DamageAssesment.SurveyResponses.Test
public async Task GetSurveyResponsesBySurveyAsync_ShouldReturnStatusCode204()
{
var mockResponse = await MockData.getResponse();
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesBySurveyAsync(1)).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesBySurveyAsync(1,1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (NoContentResult)await surveyResponseProvider.GetSurveyResponsesAsync(1);
Assert.Equal(204, result.StatusCode);
@ -68,9 +67,9 @@ namespace DamageAssesment.SurveyResponses.Test
{
SurveyResponse mockRequestObject = await MockData.getSurveyResponseObject();
var mockResponse = await MockData.getOkResponse();
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesBySurveyAndLocationAsync(1, 1)).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesBySurveyAndLocationAsync(1, 1,1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (OkObjectResult)await surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(1, 1);
var result = (OkObjectResult)await surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(1, 1,1);
Assert.Equal(200, result.StatusCode);
}
@ -78,9 +77,9 @@ namespace DamageAssesment.SurveyResponses.Test
public async Task GetSurveyResponsesBySurveyLocationAsync_ShouldReturnStatusCode204()
{
var mockResponse = await MockData.getResponse();
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesBySurveyAndLocationAsync(1, 1)).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesBySurveyAndLocationAsync(1, 1, 1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (NoContentResult)await surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(1, 1);
var result = (NoContentResult)await surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(1, 1, 1);
Assert.Equal(204, result.StatusCode);
}
@ -89,9 +88,9 @@ namespace DamageAssesment.SurveyResponses.Test
{
SurveyResponse mockRequestObject = await MockData.getSurveyResponseObject();
var mockResponse = await MockData.getOkResponse();
mockSurveyResponseService.Setup(service => service.GetResponsesByAnswerAsync(1, 1, "Yes")).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetResponsesByAnswerAsync(1, 1, "Yes",1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (OkObjectResult)await surveyResponseProvider.GetSurveyResponsesByAnswerAsyncAsync(1, 1, "Yes");
var result = (OkObjectResult)await surveyResponseProvider.GetSurveyResponsesByAnswerAsyncAsync(1, 1, "Yes",1);
Assert.Equal(200, result.StatusCode);
}
@ -99,9 +98,9 @@ namespace DamageAssesment.SurveyResponses.Test
public async Task GetSurveyResponsesBySurveyQuestionAnswerAsync_ShouldReturnStatusCode204()
{
var mockResponse = await MockData.getResponse();
mockSurveyResponseService.Setup(service => service.GetResponsesByAnswerAsync(1, 1, "Yes")).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetResponsesByAnswerAsync(1, 1, "Yes", 1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (NoContentResult)await surveyResponseProvider.GetSurveyResponsesByAnswerAsyncAsync(1, 1, "Yes");
var result = (NoContentResult)await surveyResponseProvider.GetSurveyResponsesByAnswerAsyncAsync(1, 1, "Yes",1);
Assert.Equal(204, result.StatusCode);
}
@ -111,9 +110,9 @@ namespace DamageAssesment.SurveyResponses.Test
{
SurveyResponse mockRequestObject = await MockData.getSurveyResponseObject();
var mockResponse = await MockData.getOkResponse();
mockSurveyResponseService.Setup(service => service.GetAnswersByRegionAsync(1)).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetAnswersByRegionAsync(1,1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (OkObjectResult)await surveyResponseProvider.GetAnswersByRegionAsync(1);
var result = (OkObjectResult)await surveyResponseProvider.GetAnswersByRegionAsync(1, 1);
Assert.Equal(200, result.StatusCode);
}
@ -121,9 +120,9 @@ namespace DamageAssesment.SurveyResponses.Test
public async Task GetSurveyResponsesByRegionSurveyAsync_ShouldReturnStatusCode204()
{
var mockResponse = await MockData.getResponse();
mockSurveyResponseService.Setup(service => service.GetAnswersByRegionAsync(1)).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetAnswersByRegionAsync(1, 1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (NoContentResult)await surveyResponseProvider.GetAnswersByRegionAsync(1);
var result = (NoContentResult)await surveyResponseProvider.GetAnswersByRegionAsync(1, 1);
Assert.Equal(204, result.StatusCode);
}
@ -132,9 +131,9 @@ namespace DamageAssesment.SurveyResponses.Test
{
SurveyResponse mockRequestObject = await MockData.getSurveyResponseObject();
var mockResponse = await MockData.getOkResponse();
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesByMaintenanceCenterAsync(1)).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesByMaintenanceCenterAsync(1, 1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (OkObjectResult)await surveyResponseProvider.GetAnswersByMaintenaceCentersync(1);
var result = (OkObjectResult)await surveyResponseProvider.GetAnswersByMaintenaceCentersync(1, 1);
Assert.Equal(200, result.StatusCode);
}
@ -142,9 +141,9 @@ namespace DamageAssesment.SurveyResponses.Test
public async Task GetSurveyResponsesMaintenanceCenterSurveyAsync_ShouldReturnStatusCode204()
{
var mockResponse = await MockData.getResponse();
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesByMaintenanceCenterAsync(1)).ReturnsAsync(mockResponse);
mockSurveyResponseService.Setup(service => service.GetSurveyResponsesByMaintenanceCenterAsync(1, 1)).ReturnsAsync(mockResponse);
var surveyResponseProvider = new SurveyResponsesController(mockSurveyResponseService.Object);
var result = (NoContentResult)await surveyResponseProvider.GetAnswersByMaintenaceCentersync(1);
var result = (NoContentResult)await surveyResponseProvider.GetAnswersByMaintenaceCentersync(1, 1);
Assert.Equal(204, result.StatusCode);
}

View File

@ -17,13 +17,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Locations", "DamageAssesment.Api.Locations\DamageAssesment.Api.Locations.csproj", "{746C67BF-9949-4361-B5D2-358C7607750E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.SurveyResponses", "DamageAssesment.Api.SurveyResponses\DamageAssesment.Api.SurveyResponses.csproj", "{D11808FE-AD1C-4BA6-87FD-9D18B2DC81F2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Responses", "DamageAssesment.Api.SurveyResponses\DamageAssesment.Api.Responses.csproj", "{D11808FE-AD1C-4BA6-87FD-9D18B2DC81F2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Questions.Test", "DamageAssesment.Api.QuestionsTest\DamageAssesment.Api.Questions.Test.csproj", "{35CD9231-034D-4999-BCFC-1786DD007ED2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Surveys.Test", "DamageAssesment.Api.Surveys.Test\DamageAssesment.Api.Surveys.Test.csproj", "{ADFB79E3-83C9-454F-A070-49D167BD28CC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.SurveyResponses.Test", "DamageAssesment.SurveyResponses.Test\DamageAssesment.Api.SurveyResponses.Test.csproj", "{6F4B9C9D-CE5D-421A-876F-57D0FEDF8049}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Responses.Test", "DamageAssesment.SurveyResponses.Test\DamageAssesment.Api.Responses.Test.csproj", "{6F4B9C9D-CE5D-421A-876F-57D0FEDF8049}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Attachments.Test", "DamageAssesment.Api.Attachments.Test\DamageAssesment.Api.Attachments.Test.csproj", "{730E5718-FCE1-42C0-AB76-EA020896A788}"
EndProject