From 1b30dd94406fadc83857db7c63c07afba409e91e Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Fri, 22 Nov 2024 15:01:20 +0200 Subject: [PATCH] return role in personal info --- .../Models/ShortUserDto.cs | 1 - .../Communication/CommunicationConstants.cs | 2 -- .../Communication/CommunicationService.cs | 14 +++++--------- .../Controllers/V1/PersonalInfoController.cs | 8 ++++++-- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/OutOfSchool/OutOfSchool.BusinessLogic/Models/ShortUserDto.cs b/OutOfSchool/OutOfSchool.BusinessLogic/Models/ShortUserDto.cs index 72bd194733..60662cab20 100644 --- a/OutOfSchool/OutOfSchool.BusinessLogic/Models/ShortUserDto.cs +++ b/OutOfSchool/OutOfSchool.BusinessLogic/Models/ShortUserDto.cs @@ -7,7 +7,6 @@ namespace OutOfSchool.BusinessLogic.Models; public class ShortUserDto : BaseUserDto { - [DataType(DataType.EmailAddress)] public string UserName { get; set; } public string Role { get; set; } diff --git a/OutOfSchool/OutOfSchool.Common/Communication/CommunicationConstants.cs b/OutOfSchool/OutOfSchool.Common/Communication/CommunicationConstants.cs index f871b7cadb..b50ce289ec 100644 --- a/OutOfSchool/OutOfSchool.Common/Communication/CommunicationConstants.cs +++ b/OutOfSchool/OutOfSchool.Common/Communication/CommunicationConstants.cs @@ -43,6 +43,4 @@ public static class CommunicationConstants public const string BlockRegionAdmin = "regionadmin/block/"; public const string ReinviteRegionAdmin = "regionadmin/reinvite/"; - - public const int BufferSize = 1024; } \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.Common/Communication/CommunicationService.cs b/OutOfSchool/OutOfSchool.Common/Communication/CommunicationService.cs index e7efeafaa4..8c4279186f 100644 --- a/OutOfSchool/OutOfSchool.Common/Communication/CommunicationService.cs +++ b/OutOfSchool/OutOfSchool.Common/Communication/CommunicationService.cs @@ -8,7 +8,6 @@ using System.Net.Mime; using System.Text; using System.Threading.Tasks; -using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using OutOfSchool.Common.Communication.ICommunication; @@ -94,22 +93,19 @@ public virtual async Task> SendRequest( requestMessage.Method = HttpMethodService.GetHttpMethodType(request); - logger.LogDebug("Sending request to {RequestMessage}", requestMessage); - - var response = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead) + using var response = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead) .ConfigureAwait(false); if (!response.IsSuccessStatusCode) { // Error response should be small, but still no need to waste time as default handler is ignoring it - var errorBody = errorHandler == null ? null : await response.Content.ReadAsStringAsync(); + var errorBody = errorHandler == null ? null : await response.Content.ReadAsStringAsync().ConfigureAwait(false); var error = new CommunicationError(response.StatusCode) { Body = errorBody, }; - logger.LogDebug("Error body: {Body}", errorBody); logger.LogError("Remote service error: {StatusCode}", response.StatusCode); - return await HandleErrorAsync(error, "Remote service error", errorHandler); + return await HandleErrorAsync(error, "Remote service error", errorHandler).ConfigureAwait(false); } await using var stream = await response.EnsureSuccessStatusCode().Content.ReadAsStreamAsync() @@ -120,12 +116,12 @@ public virtual async Task> SendRequest( catch (HttpRequestException ex) { logger.LogError(ex, "Networking error"); - return await HandleExceptionAsync(ex, errorHandler); + return await HandleExceptionAsync(ex, errorHandler).ConfigureAwait(false); } catch (Exception ex) { logger.LogError(ex, "Unknown error"); - return await HandleExceptionAsync(ex, errorHandler); + return await HandleExceptionAsync(ex, errorHandler).ConfigureAwait(false); } } diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/PersonalInfoController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/PersonalInfoController.cs index bbd1d21d35..9e580e78f6 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/PersonalInfoController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/PersonalInfoController.cs @@ -44,9 +44,13 @@ public async Task GetPersonalInfo() { var info = currentUserService.IsInRole(Role.Parent) ? await parentService.GetPersonalInfoByUserId(currentUserService.UserId) - : null; + : await userService.GetById(currentUserService.UserId); - return Ok(info ?? (await userService.GetById(currentUserService.UserId))); + if (string.IsNullOrWhiteSpace(info.Role)) + { + info.Role = currentUserService.UserRole; + } + return Ok(info); }