From 52869afc3fa09904d2196284e728a20cf114f31c Mon Sep 17 00:00:00 2001 From: Vijay Uppu <913468@dadeschools.net> Date: Thu, 7 Dec 2023 09:54:56 -0500 Subject: [PATCH 1/2] adding multiple questions for survey id --- .../Controllers/QuestionsController.cs | 21 ++++++++++++ .../Interfaces/IQuestionsProvider.cs | 1 + .../Providers/QuestionsProvider.cs | 33 +++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs index 14df48f..5adc91a 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs @@ -1,4 +1,5 @@ using DamageAssesment.Api.Questions.Interfaces; +using DamageAssesment.Api.Questions.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -111,6 +112,26 @@ namespace DamageAssesment.Api.Questions.Controllers return CreatedAtRoute("DefaultApi", questions); } /// + /// PUT request for update a multiple question (multilingual) for survey. + /// + [HttpPut("questions/multiple/{surveyid}")] + public async Task CreateQuestions(int surveyid, List questions) + { + if (questions != null) + { + var result = await this.questionsProvider.PutQuestionsAsync(surveyid,questions); + if (result.IsSuccess) + { + return Ok(result.Question); + } + if (result.ErrorMessage == "Not Found") + return NotFound(result.ErrorMessage); + + return BadRequest(result.ErrorMessage); + } + return CreatedAtRoute("DefaultApi", questions); + } + /// /// POST request for creating a new question (multilingual). /// diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs index 4fcea32..086de87 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs @@ -9,6 +9,7 @@ namespace DamageAssesment.Api.Questions.Interfaces Task<(bool IsSuccess, List SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int surveyId,string language); Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question); Task<(bool IsSuccess, IEnumerable Question, string ErrorMessage)> PostQuestionsAsync(List Questions); + Task<(bool IsSuccess, IEnumerable Question, string ErrorMessage)> PutQuestionsAsync(int surveyId,List Questions); Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question); Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> DeleteQuestionAsync(int id); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs index de179ba..bf3fdb0 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs @@ -376,6 +376,36 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } + + public async Task<(bool IsSuccess, IEnumerable Question, string ErrorMessage)> PutQuestionsAsync(int surveyId, List Questions) + { + try + { + var questions=await questionDbContext.Questions.AsNoTracking().Where(a=>a.SurveyId == surveyId).ToListAsync(); + if (questions != null) + { + List questionids=questions.Select(a=>a.Id).ToList(); + var questiontrans = await questionDbContext.QuestionsTranslations.AsNoTracking().Where(x => questionids.Contains(x.QuestionId)).ToListAsync(); + if (questiontrans != null) + questionDbContext.QuestionsTranslations.RemoveRange(questiontrans); + questionDbContext.Questions.RemoveRange(questions); + questionDbContext.SaveChanges(); + } + List results = new List(); + logger?.LogInformation("Query Question"); + foreach (Models.Question Question in Questions) + { + Question.SurveyId = surveyId; + results.Add(InsertQuestion(Question)); + } + return (true, results, null); + } + catch (Exception ex) + { + logger?.LogError(ex.ToString()); + return (false, null, ex.Message); + } + } public async Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question) { try @@ -409,8 +439,11 @@ namespace DamageAssesment.Api.Questions.Providers if (question != null) { + var questiontrans=await questionDbContext.QuestionsTranslations.AsNoTracking().Where(x=>x.QuestionId== id).ToListAsync(); var result = mapper.Map(question); result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, "")); + if(questiontrans!=null) + questionDbContext.QuestionsTranslations.RemoveRange(questiontrans); questionDbContext.Questions.Remove(question); questionDbContext.SaveChanges(); return (true, result, $"QuestionID {id} deleted Successfuly"); From bb87f1c8e04ee83784df6fd514c68f287c6f96c5 Mon Sep 17 00:00:00 2001 From: Vijay Uppu <913468@dadeschools.net> Date: Tue, 12 Dec 2023 17:18:18 -0500 Subject: [PATCH 2/2] user access module changes for populating employee Information --- .../Interfaces/IEmployeeServiceProvider.cs | 4 +- .../Interfaces/IUsersAccessProvider.cs | 4 +- .../Interfaces/Interface.cs | 2 +- .../Program.cs | 1 + .../Providers/UserAccessProvider.cs | 67 ++++++++++++++++--- .../Services/EmployeeServiceProvider.cs | 8 +-- .../Services/HttpUtil.cs | 4 +- .../appsettings.json | 5 +- 8 files changed, 72 insertions(+), 23 deletions(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IEmployeeServiceProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IEmployeeServiceProvider.cs index 255a954..4b6622b 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IEmployeeServiceProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IEmployeeServiceProvider.cs @@ -4,7 +4,7 @@ namespace DamageAssesment.Api.UsersAccess.Interfaces { public interface IEmployeeServiceProvider { - Task> getEmployeesAsync(); - Task getEmployeeAsync(int employeeId); + Task> getEmployeesAsync(string token); + Task getEmployeeAsync(int employeeId, string token); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IUsersAccessProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IUsersAccessProvider.cs index 6a70a5b..92d531c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IUsersAccessProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IUsersAccessProvider.cs @@ -4,8 +4,8 @@ namespace DamageAssesment.Api.UsersAccess.Interfaces { public interface IUsersAccessProvider { - public Task<(bool IsSuccess, IEnumerable< Models.User> Users, string ErrorMessage)> GetUsersAsync(); - public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> GetUsersAsync(int Id); + public Task<(bool IsSuccess, IEnumerable Users, string ErrorMessage)> GetUsersAsync(); + public Task<(bool IsSuccess, object User, string ErrorMessage)> GetUsersAsync(int Id); public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> PostUserAsync(Models.User User); public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> PutUserAsync(int Id,Models.User User); public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> DeleteUserAsync(int Id); diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/Interface.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/Interface.cs index 3d0a8ed..aa35187 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/Interface.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/Interface.cs @@ -2,6 +2,6 @@ { public interface IHttpUtil { - Task SendAsync(HttpMethod method, string url, string JsonInput); + Task SendAsync(HttpMethod method, string url, string JsonInput, string token); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Program.cs index c628445..3c9d869 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Program.cs @@ -68,6 +68,7 @@ builder.Services.AddAuthorization(options => var _jwtsettings = builder.Configuration.GetSection("JwtSettings"); builder.Services.Configure(_jwtsettings); +builder.Services.AddHttpContextAccessor(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/UserAccessProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/UserAccessProvider.cs index 939ca4e..5fbfe75 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/UserAccessProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/UserAccessProvider.cs @@ -2,9 +2,12 @@ using DamageAssesment.Api.UsersAccess.Db; using DamageAssesment.Api.UsersAccess.Interfaces; using DamageAssesment.Api.UsersAccess.Models; +using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System.Data; using Newtonsoft.Json; using System.IdentityModel.Tokens.Jwt; @@ -18,21 +21,23 @@ namespace DamageAssesment.Api.UsersAccess.Providers private readonly UsersAccessDbContext userAccessDbContext; private readonly ILogger logger; private readonly IMapper mapper; - //private readonly IEmployeeServiceProvider employeeServiceProvider; + private readonly IEmployeeServiceProvider employeeServiceProvider; private readonly JwtSettings jwtSettings; private readonly ITokenServiceProvider tokenServiceProvider; private readonly IConfiguration configuration; + private readonly IHttpContextAccessor httpContextAccessor; - public UsersAccessProvider(IConfiguration configuration,IOptions options, ITokenServiceProvider tokenServiceProvider, UsersAccessDbContext userAccessDbContext, IEmployeeServiceProvider employeeServiceProvider, ILogger logger, IMapper mapper) + public UsersAccessProvider(IConfiguration configuration,IOptions options, ITokenServiceProvider tokenServiceProvider, IHttpContextAccessor httpContextAccessor, UsersAccessDbContext userAccessDbContext, IEmployeeServiceProvider employeeServiceProvider, ILogger logger, IMapper mapper) { this.userAccessDbContext = userAccessDbContext; - //this.employeeServiceProvider = employeeServiceProvider; + this.employeeServiceProvider = employeeServiceProvider; this.logger = logger; this.mapper = mapper; jwtSettings = options.Value; this.tokenServiceProvider = tokenServiceProvider; + this.httpContextAccessor = httpContextAccessor; this.configuration = configuration; - // seedData(); + seedData(); } public void seedData() @@ -55,18 +60,47 @@ namespace DamageAssesment.Api.UsersAccess.Providers userAccessDbContext.SaveChanges(); } } - - public async Task<(bool IsSuccess, IEnumerable Users, string ErrorMessage)> GetUsersAsync() + private string GetToken() + { + string token = httpContextAccessor.HttpContext.Request.Headers.Authorization; + if (token != null) + { + token = token.Replace("Bearer ", string.Empty); + } + else + { + token = ""; + } + return token; + } + public async Task<(bool IsSuccess, IEnumerable Users, string ErrorMessage)> GetUsersAsync() { try { logger?.LogInformation("Gell all Users from DB"); var users = await userAccessDbContext.Users.ToListAsync(); + List userslist= new List(); if (users != null) { + var employees = await employeeServiceProvider.getEmployeesAsync( GetToken()); + var roles = await userAccessDbContext.Roles.ToListAsync(); + foreach (Db.User user in users) + { + var employee = employees.SingleOrDefault(a=>a.Id==user.EmployeeId); + var role = roles.SingleOrDefault(s => s.Id == user.RoleId); + userslist.Add(new + { + Id = user.Id, + EmployeeId = user.EmployeeId, + EmployeeCode = user.EmployeeCode, + EmployeeName = (employee != null) ? employee.Name : null, + RoleId = user.RoleId, + RoleName = (role != null) ? role.Name : null + }); + } logger?.LogInformation($"{users.Count} Items(s) found"); - var result = mapper.Map, IEnumerable>(users); - return (true, result, null); + // var result = mapper.Map, IEnumerable>(users); + return (true, userslist, null); } return (false, null, "Not found"); } @@ -76,18 +110,29 @@ namespace DamageAssesment.Api.UsersAccess.Providers return (false, null, ex.Message); } } - - public async Task<(bool IsSuccess, Models.User User, string ErrorMessage)> GetUsersAsync(int Id) + public async Task<(bool IsSuccess, object User, string ErrorMessage)> GetUsersAsync(int Id) { try { logger?.LogInformation("Querying Users table"); + var user = await userAccessDbContext.Users.SingleOrDefaultAsync(s => s.Id == Id); if (user != null) { + var employee = await employeeServiceProvider.getEmployeeAsync(user.EmployeeId,GetToken()); + var role = await userAccessDbContext.Roles.SingleOrDefaultAsync(s => s.Id == user.RoleId); + var data = new + { + Id = user.Id, + EmployeeId = user.EmployeeId, + EmployeeCode=user.EmployeeCode, + EmployeeName = (employee != null) ? employee.Name : null, + RoleId = user.RoleId, + RoleName = (role!=null)?role.Name:null + }; logger?.LogInformation($"User Id: {Id} found"); var result = mapper.Map(user); - return (true, result, null); + return (true, data, null); } return (false, null, "Not found"); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Services/EmployeeServiceProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Services/EmployeeServiceProvider.cs index a0364a0..497b9c7 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Services/EmployeeServiceProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Services/EmployeeServiceProvider.cs @@ -10,11 +10,11 @@ namespace DamageAssesment.Api.UsersAccess.Services { } - public async Task> getEmployeesAsync() + public async Task> getEmployeesAsync(string token) { try { - var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null); + var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null,token); var employees = JsonConvert.DeserializeObject>(responseJsonString); if (employees == null || !employees.Any()) @@ -28,12 +28,12 @@ namespace DamageAssesment.Api.UsersAccess.Services } } - public async Task getEmployeeAsync(int employeeId) + public async Task getEmployeeAsync(int employeeId, string token) { try { url = urlBase + string.Format(configuration.GetValue("RessourceSettings:EmployeeById"), employeeId); - var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null); + var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null,token); var employee = JsonConvert.DeserializeObject(responseJsonString); if (employee == null) diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Services/HttpUtil.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Services/HttpUtil.cs index a8b5c9f..071a048 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Services/HttpUtil.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Services/HttpUtil.cs @@ -14,7 +14,7 @@ namespace DamageAssesment.Api.UsersAccess.Services this.httpClient = httpClient; this.logger = logger; } - public async Task SendAsync(HttpMethod method, string url, string JsonInput) + public async Task SendAsync(HttpMethod method, string url, string JsonInput,string token) { try { @@ -22,7 +22,7 @@ namespace DamageAssesment.Api.UsersAccess.Services request.Headers.Accept.Clear(); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - //request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); if (method == HttpMethod.Post) { request.Content = new StringContent(JsonInput, Encoding.UTF8, "application/json"); diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/appsettings.json index 2924cb1..ca3c7b8 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/appsettings.json @@ -8,8 +8,11 @@ "Microsoft.AspNetCore": "Warning" } }, + //"EndPointSettings": { + // "EmployeeUrlBase": "http://localhost:5135" + //}, "EndPointSettings": { - "EmployeeUrlBase": "http://localhost:5135" + "EmployeeUrlBase": "http://damageassesment.api.employees:80" }, "RessourceSettings": { "Employee": "/Employees",