Update survey response, adding EmployeeId , Location Id as int, adjust end point for ansers submission in batch

This commit is contained in:
Reginald Cherenfant Jasmin
2023-09-13 01:28:24 -04:00
parent 4cf7d9f891
commit 9109d0d793
68 changed files with 525 additions and 540 deletions

View File

@ -31,19 +31,19 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
this.questionServiceProvider = questionServiceProvider;
this.surveyServiceProvider = surveyServiceProvider;
this.mapper = mapper;
//seedData();
// seedData();
}
private void seedData()
{
if (!surveyResponseDbContext.SurveyResponses.Any())
{
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 1, SurveyId = 1, EmployeeId = "Emp1", LocationId = "Loc1", ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 2, SurveyId = 1, EmployeeId = "Emp2", LocationId = "Loc2", ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 3, SurveyId = 3, EmployeeId = "Emp4", LocationId = "Loc1", ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 4, SurveyId = 4, EmployeeId = "Emp1", LocationId = "Loc2", ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 6, SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc2", ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 7, SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc3", ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 1, SurveyId = 1, EmployeeId = 1, LocationId = 1, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 2, SurveyId = 1, EmployeeId = 2, LocationId = 2, ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 3, SurveyId = 3, EmployeeId = 4, LocationId = 1, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 4, SurveyId = 4, EmployeeId = 1, LocationId = 2, ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "false", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 6, SurveyId = 1, EmployeeId = 4, LocationId = 2, ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 7, SurveyId = 1, EmployeeId = 4, LocationId = 3, ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "false", CreatedDate = DateTime.Now });
surveyResponseDbContext.SaveChanges();
}
}
@ -136,7 +136,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
}
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, string locationId)
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, int locationId)
{
try
{
@ -286,19 +286,14 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
{
if (SurveyResponse != null)
{
var _SurveyResponse = await surveyResponseDbContext.SurveyResponses.Where(s => s.Id == Id).FirstOrDefaultAsync();
var _SurveyResponse = await surveyResponseDbContext.SurveyResponses.AsNoTracking().Where(s => s.Id == Id).FirstOrDefaultAsync();
if (_SurveyResponse != null)
{
_SurveyResponse.SurveyId = SurveyResponse.SurveyId;
_SurveyResponse.EmployeeId = SurveyResponse.EmployeeId;
_SurveyResponse.LocationId = SurveyResponse.LocationId;
_SurveyResponse.ClientDevice = SurveyResponse.ClientDevice;
_SurveyResponse.KeyAnswerResult = SurveyResponse.KeyAnswerResult;
_SurveyResponse.Longitute = SurveyResponse.Longitute;
_SurveyResponse.Latitude = SurveyResponse.Latitude;
var response = mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(SurveyResponse);
surveyResponseDbContext.Update(response);
await surveyResponseDbContext.SaveChangesAsync();
return (true, mapper.Map<Db.SurveyResponse, Models.SurveyResponse>(_SurveyResponse), "Successful");
return (true, SurveyResponse, "Successful");
}
else
{
@ -360,13 +355,13 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
answer => answer.SurveyResponseId,
surveyResponse => surveyResponse.Id,
(answer, surveyResponse) => new
Answer
{
Id = answer.Id,
QuestionId = answer.QuestionId,
AnswerText = answer.AnswerText,
Comment = answer.Comment,
LocationId = surveyResponse.LocationId,
answer.Id,
answer.QuestionId,
answer.AnswerText,
answer.Comment,
surveyResponse.LocationId,
SurveyResponseId = surveyResponse.Id
});
@ -383,41 +378,46 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
//get all the answers based on the locations
var result = from answer in surveyAnswers
from location in locations
where answer.LocationId == location.Id
select new Answer
where answer.LocationId.Equals(location.Id)
select new
{
Id = answer.Id,
QuestionId = answer.QuestionId,
AnswerText = answer.AnswerText,
Comment = answer.Comment,
RegionId = location.RegionId,
LocationId = location.Id,
SurveyResponseId = answer.SurveyResponseId
answer.Id,
answer.QuestionId,
answer.AnswerText,
answer.Comment,
location.RegionId,
LocationId = location.Id,
answer.SurveyResponseId
};
//group records by answer and region
var q = from e in result
group e by (e.RegionId, e.AnswerText) into g
select new AggregateResult
select new
{
RegionId = g.Key.RegionId,
Answers = new AggregateAnswer
g.Key.RegionId,
Answers = new
{
Answer = g.Key.AnswerText,
g.Key.AnswerText,
Counter = g.Count()
}
};
//build the result
List<AnswerData> resultList = new List<AnswerData>();
List<dynamic> resultList = new List<dynamic>();
foreach (Region region in regions)
{
var answers = q.Where(x => x.RegionId.Equals(region.Id)).Select(x => x.Answers).ToList();
resultList.Add(new AnswerData { RegionId = region.Id, Name = region.Name, Abbreviation = region.Abbreviation, Answers = answers });
var answers = from u in q.ToList()
where u.RegionId.Equals(region.Id)
select u.Answers;
resultList.Add(new { RegionId = region.Id, region.Name, region.Abbreviation, Answers = answers});
}
//return the object result
return new ResultData { Regions = resultList };
return new { Regions = resultList };
}
catch (Exception ex)
{
@ -455,7 +455,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
ans.Id,
ans.AnswerText,
ans.Comment,
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Questions }).SingleOrDefault(),
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 }
}
};
@ -504,7 +504,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
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.Questions }).SingleOrDefault(),
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 }
}
@ -531,8 +531,6 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
var questions = await questionServiceProvider.getQuestionsAsync();
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
var result = from r in surveyResonses
select new
{
@ -553,7 +551,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
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.Questions }).SingleOrDefault(),
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 }
}
};
@ -594,7 +592,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
//get all the answers with location
var surveyAnswersLocations = from surveyAns in surveyAnswers
from location in locations
where surveyAns.LocationId == location.Id
where surveyAns.LocationId.Equals(location.Id)
select new { surveyAns, location.MaintenanceCenter };
//aggreting the answers
@ -603,9 +601,9 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
select new
{
g.Key.MaintenanceCenter,
Answers = new AggregateAnswer
Answers = new
{
Answer = g.Key.AnswerText,
g.Key.AnswerText,
Counter = g.Count()
}
};
@ -628,7 +626,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
//Method to get Survey Responses by surveyId and LocationId
private async Task<dynamic> getSurveyResponsesBySurveyIdLocationIdAsync(int surveyId, string locationId)
private async Task<dynamic> getSurveyResponsesBySurveyIdLocationIdAsync(int surveyId, int locationId)
{
try
{
@ -661,7 +659,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
ans.Id,
ans.AnswerText,
ans.Comment,
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Questions }).SingleOrDefault(),
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 }
}
};
@ -759,8 +757,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 });
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;