Merged new dev changes

This commit is contained in:
uppuv
2023-09-13 13:16:42 -04:00
137 changed files with 2247 additions and 1254 deletions

View File

@ -1,87 +0,0 @@
using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Db;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
using System.Data.Common;
using System.Security.Cryptography;
using System.Text.Json.Nodes;
using System.Text;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class AnswerServiceProvider : ServiceProviderBase, IAnswerServiceProvider
{
public AnswerServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<AnswerServiceProvider> logger, IRegionServiceProvider regionServiceProvider, ILocationServiceProvider locationServiceProvider) : base(configuration, httpClient, logger, "/api/Answers", configuration.GetValue<string>("EndPointSettings:AnswerUrlBase"))
{
}
public async Task<List<Answer>> getAnswersAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseString);
if (answers == null || !answers.Any())
return null;
else return answers;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.getAnswersAsync()");
return null;
}
}
public async Task<List<Answer>> GetAnswersByResponseIdAsync(int responseId)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync("/api/AnswersByResponse/"+ responseId);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseString);
if (answers == null || !answers.Any())
return null;
else return answers;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.GetAnswersByResponseId()");
return null;
}
}
public async Task<Answer> PostAnswersAsync(Answer answer)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var jsonObject = JsonConvert.SerializeObject(answer);
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(ressource,content);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var answers = JsonConvert.DeserializeObject<Answer>(responseString);
if (answers == null) {
logger?.LogError($"Answer cannot be added - Ref: AnswerServiceProvider.PostAnswersAsync()");
return null;
}
else return answers;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.PostAnswersAsync()");
return null;
}
}
}
}

View File

@ -1,67 +0,0 @@
using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Runtime.Intrinsics.Arm;
using System.Text;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class AttachmentServiceProvider : ServiceProviderBase, IAttachmentServiceProvider
{
public AttachmentServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<AttachmentServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Attachments", configuration.GetValue<string>("EndPointSettings:AttachmentUrlBase"))
{
}
public async Task<List<Attachment>> getAttachmentsAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var attachments = JsonConvert.DeserializeObject<List<Attachment>>(responseString);
if (attachments == null || !attachments.Any())
return null;
else return attachments;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: AttachmentServiceProvider.getAttachmentsAsync()");
return null;
}
}
public async Task<IEnumerable<Attachment>> PostAttachmentsAsync(AttachmentInfo attachmentInfo)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var jsonObject = JsonConvert.SerializeObject(attachmentInfo);
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(ressource, content);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var attachments = JsonConvert.DeserializeObject<IEnumerable<Attachment>>(responseString);
if (attachments == null)
{
logger?.LogError($"Attachments cannot be added - Ref: AttachmentServiceProvider.PostAttachmentsAsync()");
return null;
}
else return attachments;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: AttachmentServiceProvider.PostAttachmentsAsync()");
return null;
}
}
}
}

View File

@ -1,58 +0,0 @@
using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
using System.Reflection;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class EmployeeServiceProvider :ServiceProviderBase, IEmployeeServiceProvider
{
public EmployeeServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Employees", configuration.GetValue<string>("EndPointSettings:EmployeeUrlBase"))
{
}
public async Task<List<Employee>> getEmployeesAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var employees = JsonConvert.DeserializeObject<List<Employee>>(responseString);
if (employees == null || !employees.Any())
return null;
else return employees;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeesAsync()");
return null;
}
}
public async Task<Employee> getEmployeeAsync(string employeeID)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
//ressource = ressource + "/" + employeeID;
var response = await httpClient.GetAsync("/api/Employees/"+ employeeID);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var employee = JsonConvert.DeserializeObject<Employee>(responseString);
if (employee == null )
return null;
else return employee;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeeAsync()");
return null;
}
}
}
}

View File

@ -1,35 +0,0 @@
using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class LocationServiceProvider :ServiceProviderBase, ILocationServiceProvider
{
public LocationServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<LocationServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Locations", configuration.GetValue<string>("EndPointSettings:LocationUrlBase"))
{
}
public async Task<List<Location>> getLocationsAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var locations = JsonConvert.DeserializeObject<List<Location>>(responseString);
if (locations == null || !locations.Any())
return null;
else return locations;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: LocationServiceProvider.getLocationsAsync()");
return null;
}
}
}
}

View File

@ -1,80 +0,0 @@
using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class QuestionServiceProvider : ServiceProviderBase, IQuestionServiceProvider
{
public QuestionServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<QuestionServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Questions", configuration.GetValue<string>("EndPointSettings:QuestionUrlBase"))
{
}
public async Task<List<Question>> getQuestionsAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var questions = JsonConvert.DeserializeObject<List<Question>>(responseString);
if (questions == null || !questions.Any())
return null;
else return questions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync()");
return null;
}
}
public async Task<List<SurveyQuestions>> getSurveyQuestionsAsync(int surveyId)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync("/api/GetSurveyQuestions/" + surveyId);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var questions = JsonConvert.DeserializeObject<List<SurveyQuestions>>(responseString);
if (questions == null || !questions.Any())
return null;
else return questions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getSurveyQuestionsAsync()");
return null;
}
}
public async Task<Question> getQuestionsAsync(int questionId)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync("/api/Questions/" + questionId);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var question = JsonConvert.DeserializeObject<Question>(responseString);
if (question == null)
return null;
else return question;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync(questionId)");
return null;
}
}
}
}

View File

@ -1,34 +0,0 @@
using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class RegionServiceProvider : ServiceProviderBase, IRegionServiceProvider
{
public RegionServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<RegionServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Regions", configuration.GetValue<string>("EndPointSettings:LocationUrlBase"))
{
}
public async Task<List<Region>> getRegionsAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var regions = JsonConvert.DeserializeObject<List<Region>>(responseString);
if (regions == null || !regions.Any())
return null;
else return regions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: RegionServiceProvider.getRegionsAsync()");
return null;
}
}
}
}

View File

@ -3,9 +3,6 @@ using DamageAssesment.Api.SurveyResponses.Db;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using System.Diagnostics;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
@ -34,21 +31,20 @@ 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 { SurveyId = 1, EmployeeId = "Emp1", LocationId = "Loc1" });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp2", LocationId = "Loc2" });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 3, EmployeeId = "Emp4", LocationId = "Loc1" });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 4, EmployeeId = "Emp1", LocationId = "Loc2" });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp3", LocationId = "Loc3" });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc2" });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc3" });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 1, LocationId = 1 });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 2, LocationId = 1 });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 3, EmployeeId = 3, LocationId = 2 });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 4, EmployeeId = 4, LocationId = 2 });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 5, LocationId = 3});
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 4, LocationId = 2 });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 3, LocationId = 1 });
surveyResponseDbContext.SaveChanges();
}
}
@ -141,7 +137,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
{
@ -212,7 +208,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
var question = await questionServiceProvider.getQuestionsAsync(questionId);
bool IsCorrectAnswer = answer.ToLower().Equals("yes") || answer.ToLower().Equals("no") ? true : false;
if (survey != null && question != null && IsCorrectAnswer)
{
var answers = await getSurveyResponsesByAnswerAsync(survey, question, answer);
@ -228,7 +224,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
else
{
return (false, null, "Not found");
}
}
}
catch (Exception ex)
{
@ -251,7 +247,6 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
answers = new List<Models.SurveyResponse>();
return (true, answers, "Empty object returned");
}
}
catch (Exception ex)
{
@ -266,16 +261,16 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
{
if (surveyResponse != null)
{
var surveyResponses = await surveyResponseDbContext.SurveyResponses.ToListAsync();
surveyResponse.Id = surveyResponses.Count + 1;
surveyResponseDbContext.SurveyResponses.Add(mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(surveyResponse));
surveyResponseDbContext.SaveChanges();
var _surveyResponse = mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(surveyResponse);
surveyResponseDbContext.SurveyResponses.Add(_surveyResponse);
await surveyResponseDbContext.SaveChangesAsync();
surveyResponse.Id = _surveyResponse.Id;
return (true, surveyResponse, "Request Successful");
}
else
{
logger?.LogInformation($"SurveyResponseID={surveyResponse.Id} cannot be added");
return (false, null, "Survey cannot be added");
logger?.LogInformation($"SurveyResponse cannot be added");
return (false, null, "SurveyResponse cannot be added");
}
}
catch (Exception ex)
@ -292,15 +287,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;
surveyResponseDbContext.SaveChanges();
return (true, mapper.Map<Db.SurveyResponse, Models.SurveyResponse>(_SurveyResponse), "Successful");
var response = mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(SurveyResponse);
surveyResponseDbContext.Update(response);
await surveyResponseDbContext.SaveChangesAsync();
return (true, SurveyResponse, "Successful");
}
else
{
@ -331,7 +325,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
if (_SurveyResponse != null)
{
surveyResponseDbContext.Remove(_SurveyResponse);
surveyResponseDbContext.SaveChanges();
await surveyResponseDbContext.SaveChangesAsync();
return (true, mapper.Map<Db.SurveyResponse, Models.SurveyResponse>(_SurveyResponse), "Successful");
}
else
@ -339,7 +333,6 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
logger?.LogInformation($"SurveyReponseId = {Id} Not found");
return (false, null, "Not Found");
}
}
catch (Exception ex)
{
@ -363,13 +356,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
});
@ -386,41 +379,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)
{
@ -434,35 +432,35 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
{
try
{
var surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.Id == surveyResponse.Id).ToListAsync();
//var surveyResponse = surveyResonses.SingleOrDefault();
var employee = await employeeServiceProvider.getEmployeeAsync(surveyResponse.EmployeeId);
var answers = await answerServiceProvider.GetAnswersByResponseIdAsync(surveyResponse.Id);
var allQuestions = await questionServiceProvider.getQuestionsAsync();
var questions = allQuestions.Where(s=> s.SurveyId == surveyResponse.Id);
var questions = allQuestions.Where(s => s.SurveyId == surveyResponse.SurveyId);
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
var result = from r in surveyResonses
select new
{
r.Id,
r.SurveyId,
r.LocationId,
r.EmployeeId,
Employee = employee,
answers = from ans in answers
select new
{
ans.QuestionId,
ans.Id,
ans.AnswerText,
ans.Comment,
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
};
return result.SingleOrDefault();
var result = new
{
surveyResponse.Id,
surveyResponse.SurveyId,
surveyResponse.LocationId,
surveyResponse.EmployeeId,
surveyResponse.ClientDevice,
surveyResponse.KeyAnswerResult,
surveyResponse.Longitute,
surveyResponse.Latitude,
Employee = employee,
answers = from ans in answers
select new
{
ans.QuestionId,
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.Text }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
};
return result;
}
catch (Exception ex)
{
@ -494,6 +492,10 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
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
@ -503,7 +505,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.QuestionGroup, 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 }
}
@ -537,6 +539,10 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
r.SurveyId,
r.LocationId,
r.EmployeeId,
r.ClientDevice,
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(),
answers = from ans in answers
where ans.SurveyResponseId == r.Id
@ -546,7 +552,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.QuestionGroup, 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 }
}
};
@ -587,7 +593,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
@ -596,9 +602,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()
}
};
@ -621,7 +627,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
{
@ -640,6 +646,10 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
r.SurveyId,
r.LocationId,
r.EmployeeId,
r.ClientDevice,
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(),
answers = from ans in answers
where ans.SurveyResponseId == r.Id
@ -650,7 +660,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.QuestionGroup, 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 }
}
};
@ -670,8 +680,6 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
try
{
var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id).ToListAsync();
//var questions = await questionServiceProvider.getQuestionsAsync();
var answers = await answerServiceProvider.getAnswersAsync();
var employees = await employeeServiceProvider.getEmployeesAsync();
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
@ -683,6 +691,10 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
r.SurveyId,
r.LocationId,
r.EmployeeId,
r.ClientDevice,
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(),
answers = from ans in answers
where ans.SurveyResponseId == r.Id
@ -709,66 +721,49 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
async Task<bool> ProcessAnswers(QuestionRequest questionRequest, int surveyResponseId)
async Task<bool> ProcessAnswers(AnswerRequest answerRequest, int surveyResponseId)
{
if (questionRequest != null)
if (answerRequest != null)
{
var answer = await answerServiceProvider.PostAnswersAsync(new Answer { Id = 0, QuestionId = questionRequest.QuestionId, AnswerText = questionRequest.AnswerText, Comment = questionRequest.Comment, SurveyResponseId = surveyResponseId });
var answer = await answerServiceProvider.PostAnswersAsync(new Models.Answer { QuestionId = answerRequest.QuestionId, AnswerText = answerRequest.AnswerText, Comment = answerRequest.Comment, SurveyResponseId = surveyResponseId });
if (answer != null)
{
List<AnswerInfo> listAnswerInfo = new List<AnswerInfo>();
listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = questionRequest.PostedFiles });
var attachments = await attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo });
listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = answerRequest.PostedFiles });
var attachments = attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo });
string message = $"Answer for question {questionRequest.QuestionId} saved to the database";
string message = $"Answer for question {answerRequest.QuestionId} saved to the database";
logger?.LogInformation(message);
return (true);
}
else
{
string message = $"Answer for question {questionRequest.QuestionId} cannot be saved to the database";
string message = $"Answer for question {answerRequest.QuestionId} cannot be saved to the database";
logger?.LogInformation(message);
return (false);
}
}
else
{
var message = $"Answer for question {questionRequest.QuestionId} cannot be saved to the database - questionRequest object is null";
var message = $"Answer for question {answerRequest.QuestionId} cannot be saved to the database - answerRequest object is null";
logger?.LogInformation(message);
return (false);
}
}
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Models.AnswerRequest answers)
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Models.Request request)
{
try
{
if (answers != null)
if (request != null)
{
var response = await PostSurveyResponseAsync(new Models.SurveyResponse { Id = 0, SurveyId = answers.SurveyId, EmployeeId = answers.EmployeeId, LocationId = answers.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;
var answerTasks = new List<Task>(); //new List<string>();
//var tasks = answers.Answers.Select(x => ProcessAnswers(x,surveyResponse.SurveyResponseID));
foreach (QuestionRequest ans in answers.Answers)
{
//var stopwatch = new Stopwatch();
//stopwatch.Start();
var task = Task.Run(() => ProcessAnswers(ans, surveyResponse.Id));
//var task = await ProcessAnswers(ans, surveyResponse.Id);
answerTasks.Add(task);
//stopwatch.Stop();
//answerTasks.Add(ProcessAnswers(ans, surveyResponse.Id));
}
await Task.WhenAll(answerTasks);
var tasks = request.Answers.Select(x => ProcessAnswers(x, surveyResponse.Id));
await Task.WhenAll(tasks);
return (true, surveyResponse, null);
}
else

View File

@ -1,59 +0,0 @@
using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
using System.Reflection;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class SurveyServiceProvider :ServiceProviderBase, ISurveyServiceProvider
{
public SurveyServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Surveys", configuration.GetValue<string>("EndPointSettings:SurveyUrlBase"))
{
}
public async Task<List<Survey>> getSurveysAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var surveys = JsonConvert.DeserializeObject<List<Survey>>(responseString);
if (surveys == null || !surveys.Any())
return null;
else return surveys;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveysAsync()");
return null;
}
}
public async Task<Survey> getSurveyAsync(int surveyId)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource+"/"+ surveyId);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var survey = JsonConvert.DeserializeObject<Survey>(responseString);
if (survey == null )
return null;
else return survey;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveyAsync()");
return null;
}
}
}
}