From fc0c02a71d698b4ba98def30f5f2d3bc7ed35222 Mon Sep 17 00:00:00 2001 From: AmirReza Date: Thu, 29 Aug 2024 13:12:51 +0330 Subject: [PATCH 1/3] fix(Data Structure): Fixed crashes due to missing record IDs in DB and improved JSON contract between front and back --- .../EAV/Controllers/FileAccessController.cs | 36 ++++++++++++++++--- .../EAV/Dto/AccessFileToUserDto.cs | 7 ++++ .../AnalysisData/EAV/Dto/PaginatedListDto.cs | 22 ++++++------ .../AnalysisData/EAV/Dto/PaginationNodeDto.cs | 1 + .../AnalysisData/EAV/Dto/UploadDataDto.cs | 2 +- .../EAV/Repository/UserFileRepository.cs | 7 ---- .../EAV/Service/FilePermissionService.cs | 33 +++++++++++++---- .../EAV/Service/GraphServiceEav.cs | 6 ++-- .../AnalysisData/Resources.Designer.cs | 9 +++++ AnalysisData/AnalysisData/Resources.resx | 3 ++ .../User/Exception/GuidNotCorrectFormat.cs | 8 +++++ 11 files changed, 101 insertions(+), 33 deletions(-) create mode 100644 AnalysisData/AnalysisData/EAV/Dto/AccessFileToUserDto.cs create mode 100644 AnalysisData/AnalysisData/User/Exception/GuidNotCorrectFormat.cs diff --git a/AnalysisData/AnalysisData/EAV/Controllers/FileAccessController.cs b/AnalysisData/AnalysisData/EAV/Controllers/FileAccessController.cs index ff60e9f..32af98c 100644 --- a/AnalysisData/AnalysisData/EAV/Controllers/FileAccessController.cs +++ b/AnalysisData/AnalysisData/EAV/Controllers/FileAccessController.cs @@ -1,5 +1,7 @@ +using AnalysisData.EAV.Dto; using AnalysisData.EAV.Repository.Abstraction; using AnalysisData.EAV.Service; +using AnalysisData.Exception; using Microsoft.AspNetCore.Mvc; namespace AnalysisData.EAV.Controllers; @@ -33,13 +35,37 @@ public async Task GetUsersAsync([FromQuery] string username) } [HttpPost("AccessFileToUser")] - public async Task AccessFileToUser([FromBody] List userGuidIdes,[FromQuery] int fileId) + public async Task AccessFileToUser([FromBody] AccessFileToUserDto request) { - await _filePermissionService.AccessFileToUser(userGuidIdes, fileId); - return Ok(new + try { - massage = "success" - }); + await _filePermissionService.AccessFileToUser(request.UserGuidIds.ToList(), request.FileId); + return Ok(new + { + message = "success" + }); + } + catch (GuidNotCorrectFormat ex) + { + return BadRequest(new + { + message = ex.Message + }); + } + catch (UserNotFoundException ex) + { + return NotFound(new + { + message = ex.Message + }); + } + catch (FileNotFoundException ex) + { + return NotFound(new + { + message = ex.Message + }); + } } [HttpGet("WhoAccessToThisFile")] diff --git a/AnalysisData/AnalysisData/EAV/Dto/AccessFileToUserDto.cs b/AnalysisData/AnalysisData/EAV/Dto/AccessFileToUserDto.cs new file mode 100644 index 0000000..da6a10b --- /dev/null +++ b/AnalysisData/AnalysisData/EAV/Dto/AccessFileToUserDto.cs @@ -0,0 +1,7 @@ +namespace AnalysisData.EAV.Dto; + +public class AccessFileToUserDto +{ + public IEnumerable UserGuidIds { get; set; } + public int FileId { get; set; } +} \ No newline at end of file diff --git a/AnalysisData/AnalysisData/EAV/Dto/PaginatedListDto.cs b/AnalysisData/AnalysisData/EAV/Dto/PaginatedListDto.cs index 6216647..b4aa84a 100644 --- a/AnalysisData/AnalysisData/EAV/Dto/PaginatedListDto.cs +++ b/AnalysisData/AnalysisData/EAV/Dto/PaginatedListDto.cs @@ -1,17 +1,17 @@ namespace AnalysisData.EAV.Dto; -public class PaginatedListDto +public class PaginatedListDto { - public List PaginateList { get; } - public int PageIndex { get; } - public int TotalCount { get; } - public string TypeCategory { get; } - - public PaginatedListDto(List items, int pageIndex, int totalCount, string typeCategory) + public PaginatedListDto(List items, int pageIndex, int totalItems, string categoryName) { - PaginateList = items; + Items = items; PageIndex = pageIndex; - TotalCount = totalCount; - TypeCategory = typeCategory; - } + TotalItems = totalItems; + CategoryName = categoryName; + } + + public List Items { get; set; } + public int PageIndex { get; set; } + public int TotalItems { get; set; } + public string CategoryName { get; set; } } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/EAV/Dto/PaginationNodeDto.cs b/AnalysisData/AnalysisData/EAV/Dto/PaginationNodeDto.cs index 0778406..b6e45c4 100644 --- a/AnalysisData/AnalysisData/EAV/Dto/PaginationNodeDto.cs +++ b/AnalysisData/AnalysisData/EAV/Dto/PaginationNodeDto.cs @@ -2,5 +2,6 @@ namespace AnalysisData.EAV.Dto; public class PaginationNodeDto { + public int Id { get; set; } public string EntityName { get; set; } } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/EAV/Dto/UploadDataDto.cs b/AnalysisData/AnalysisData/EAV/Dto/UploadDataDto.cs index 852d270..84556e1 100644 --- a/AnalysisData/AnalysisData/EAV/Dto/UploadDataDto.cs +++ b/AnalysisData/AnalysisData/EAV/Dto/UploadDataDto.cs @@ -2,7 +2,7 @@ namespace AnalysisData.EAV.Dto; public class UploadDataDto { - public string Id { get; set; } + public int Id { get; set; } public string Category { get; set; } public string FileName { get; set; } diff --git a/AnalysisData/AnalysisData/EAV/Repository/UserFileRepository.cs b/AnalysisData/AnalysisData/EAV/Repository/UserFileRepository.cs index 32ea0f3..5c7f964 100644 --- a/AnalysisData/AnalysisData/EAV/Repository/UserFileRepository.cs +++ b/AnalysisData/AnalysisData/EAV/Repository/UserFileRepository.cs @@ -58,11 +58,6 @@ public async Task DeleteByUserIdAsync(string userId) public async Task GrantUserAccess(List userIds,int fileId) { - var file = await GetByFileIdAsync(fileId); - if (file is null) - { - throw new FileNotFoundException(); - } foreach (var userId in userIds) { var userFile = new UserFile() { UserId = Guid.Parse(userId), FileId = fileId }; @@ -76,6 +71,4 @@ public async Task RevokeUserAccess(List userIds) await DeleteByUserIdAsync(userId); } } - - } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs b/AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs index 3171568..6a13154 100644 --- a/AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs +++ b/AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs @@ -13,12 +13,14 @@ public class FilePermissionService : IFilePermissionService private readonly IFileUploadedRepository _fileUploadedRepository; private readonly IUserRepository _userRepository; private readonly IUserFileRepository _userFileRepository; + private readonly IUploadDataRepository _uploadDataRepository; - public FilePermissionService(IFileUploadedRepository fileUploadedRepository,IUserRepository userRepository, IUserFileRepository userFileRepository) + public FilePermissionService(IFileUploadedRepository fileUploadedRepository,IUserRepository userRepository, IUserFileRepository userFileRepository, IUploadDataRepository uploadDataRepository) { _fileUploadedRepository = fileUploadedRepository; _userRepository = userRepository; _userFileRepository = userFileRepository; + _uploadDataRepository = uploadDataRepository; } @@ -29,7 +31,7 @@ public async Task GetFilesPagination(int page, int limit) var paginatedFiles = files.Select(x => new UploadDataDto() { - Id = x.Id.ToString(), + Id = x.Id, FileName = x.FileName, Category = x.Category.Name, UploadDate = x.UploadDate, @@ -68,19 +70,38 @@ public async Task> WhoAccessThisFile(int f return usersDtos; } - public async Task AccessFileToUser(List inputUserGuidIds,int fileId) + public async Task AccessFileToUser(List inputUserGuidIds, int fileId) { + var validUserGuids = new List(); + foreach (var userId in inputUserGuidIds) { - var user = await _userRepository.GetUserById(Guid.Parse(userId)); + if (Guid.TryParse(userId, out Guid parsedGuid)) + { + validUserGuids.Add(parsedGuid); + } + else + { + throw new GuidNotCorrectFormat(); + } + } + + foreach (var userGuid in validUserGuids) + { + var user = await _userRepository.GetUserById(userGuid); if (user is null) { throw new UserNotFoundException(); } } + if (await _uploadDataRepository.GetByIdAsync(fileId) is null) + { + throw new FileNotFoundException(); + } var currentAccessor = await _userFileRepository.GetUsersIdAccessToInputFile(fileId.ToString()); - var newUsers = inputUserGuidIds.Except(currentAccessor).ToList(); - var blockAccessToFile = currentAccessor.Except(currentAccessor.Intersect(inputUserGuidIds)).ToList(); + var newUsers = validUserGuids.Select(g => g.ToString()).Except(currentAccessor).ToList(); + var blockAccessToFile = currentAccessor.Except(currentAccessor.Intersect(validUserGuids.Select(g => g.ToString()))).ToList(); + await _userFileRepository.RevokeUserAccess(blockAccessToFile); await _userFileRepository.GrantUserAccess(newUsers, fileId); } diff --git a/AnalysisData/AnalysisData/EAV/Service/GraphServiceEav.cs b/AnalysisData/AnalysisData/EAV/Service/GraphServiceEav.cs index 6aa36fc..f76874f 100644 --- a/AnalysisData/AnalysisData/EAV/Service/GraphServiceEav.cs +++ b/AnalysisData/AnalysisData/EAV/Service/GraphServiceEav.cs @@ -30,9 +30,9 @@ public GraphServiceEav(IGraphNodeRepository graphNodeRepository, IGraphEdgeRepos _graphEdgeRepository = graphEdgeRepository; } - public async Task GetNodesPaginationAsync(ClaimsPrincipal claimsPrincipal,int pageIndex, int pageSize, int? categoryId = null) + public async Task GetNodesPaginationAsync(ClaimsPrincipal claimsPrincipal, int pageIndex, int pageSize, int? categoryId = null) { - var valueNodes = await GetEntityNodesForPaginationAsync(claimsPrincipal,categoryId); + var valueNodes = await GetEntityNodesForPaginationAsync(claimsPrincipal, categoryId); string categoryName = null; if (categoryId.HasValue) @@ -43,6 +43,7 @@ public async Task GetNodesPaginationAsync(ClaimsPrincipal clai var groupedNodes = valueNodes.Select(g => new PaginationNodeDto { + Id = g.Id, EntityName = g.Name, }) .ToList(); @@ -51,7 +52,6 @@ public async Task GetNodesPaginationAsync(ClaimsPrincipal clai var items = groupedNodes .Skip(pageIndex * pageSize) .Take(pageSize) - .Select(x => x.EntityName) // Extract EntityName to match List .ToList(); return new PaginatedListDto(items, pageIndex, count, categoryName); diff --git a/AnalysisData/AnalysisData/Resources.Designer.cs b/AnalysisData/AnalysisData/Resources.Designer.cs index c664627..51ae4b0 100644 --- a/AnalysisData/AnalysisData/Resources.Designer.cs +++ b/AnalysisData/AnalysisData/Resources.Designer.cs @@ -113,6 +113,15 @@ internal static string FileExistenceException { } } + /// + /// Looks up a localized string similar to id not in correct format!. + /// + internal static string GuidNotCorrectFormat { + get { + return ResourceManager.GetString("GuidNotCorrectFormat", resourceCulture); + } + } + /// /// Looks up a localized string similar to Invalid Email format !. /// diff --git a/AnalysisData/AnalysisData/Resources.resx b/AnalysisData/AnalysisData/Resources.resx index ea5c094..64a5ff0 100644 --- a/AnalysisData/AnalysisData/Resources.resx +++ b/AnalysisData/AnalysisData/Resources.resx @@ -69,4 +69,7 @@ We have this role in db ! + + id not in correct format! + \ No newline at end of file diff --git a/AnalysisData/AnalysisData/User/Exception/GuidNotCorrectFormat.cs b/AnalysisData/AnalysisData/User/Exception/GuidNotCorrectFormat.cs new file mode 100644 index 0000000..ac18c7f --- /dev/null +++ b/AnalysisData/AnalysisData/User/Exception/GuidNotCorrectFormat.cs @@ -0,0 +1,8 @@ +namespace AnalysisData.Exception; + +public class GuidNotCorrectFormat : ServiceException +{ + public GuidNotCorrectFormat() : base(Resources.GuidNotCorrectFormat,StatusCodes.Status401Unauthorized) + { + } +} \ No newline at end of file From 2978bcfa8d426918ece532160f8ec8647823c2fa Mon Sep 17 00:00:00 2001 From: AmirReza Date: Thu, 29 Aug 2024 13:42:59 +0330 Subject: [PATCH 2/3] fix(try-catch):delete extra trycatch --- .../EAV/Controllers/FileAccessController.cs | 32 +++---------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/AnalysisData/AnalysisData/EAV/Controllers/FileAccessController.cs b/AnalysisData/AnalysisData/EAV/Controllers/FileAccessController.cs index 32af98c..e640154 100644 --- a/AnalysisData/AnalysisData/EAV/Controllers/FileAccessController.cs +++ b/AnalysisData/AnalysisData/EAV/Controllers/FileAccessController.cs @@ -37,35 +37,11 @@ public async Task GetUsersAsync([FromQuery] string username) [HttpPost("AccessFileToUser")] public async Task AccessFileToUser([FromBody] AccessFileToUserDto request) { - try + await _filePermissionService.AccessFileToUser(request.UserGuidIds.ToList(), request.FileId); + return Ok(new { - await _filePermissionService.AccessFileToUser(request.UserGuidIds.ToList(), request.FileId); - return Ok(new - { - message = "success" - }); - } - catch (GuidNotCorrectFormat ex) - { - return BadRequest(new - { - message = ex.Message - }); - } - catch (UserNotFoundException ex) - { - return NotFound(new - { - message = ex.Message - }); - } - catch (FileNotFoundException ex) - { - return NotFound(new - { - message = ex.Message - }); - } + message = "success" + }); } [HttpGet("WhoAccessToThisFile")] From ff60f0c8c44fedae534859b2d78398fbec152319 Mon Sep 17 00:00:00 2001 From: AmirReza Date: Thu, 29 Aug 2024 13:48:55 +0330 Subject: [PATCH 3/3] fix(try-catch):Use specific try-catch --- AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs | 2 +- AnalysisData/AnalysisData/Resources.Designer.cs | 2 +- AnalysisData/AnalysisData/Resources.resx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs b/AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs index 6a13154..4df87e8 100644 --- a/AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs +++ b/AnalysisData/AnalysisData/EAV/Service/FilePermissionService.cs @@ -96,7 +96,7 @@ public async Task AccessFileToUser(List inputUserGuidIds, int fileId) } if (await _uploadDataRepository.GetByIdAsync(fileId) is null) { - throw new FileNotFoundException(); + throw new NoFileUploadedException(); } var currentAccessor = await _userFileRepository.GetUsersIdAccessToInputFile(fileId.ToString()); var newUsers = validUserGuids.Select(g => g.ToString()).Except(currentAccessor).ToList(); diff --git a/AnalysisData/AnalysisData/Resources.Designer.cs b/AnalysisData/AnalysisData/Resources.Designer.cs index 51ae4b0..d16886e 100644 --- a/AnalysisData/AnalysisData/Resources.Designer.cs +++ b/AnalysisData/AnalysisData/Resources.Designer.cs @@ -168,7 +168,7 @@ internal static string NodeNotFoundException { } /// - /// Looks up a localized string similar to No file uploaded exception !. + /// Looks up a localized string similar to No file uploaded !. /// internal static string NoFileUploadedException { get { diff --git a/AnalysisData/AnalysisData/Resources.resx b/AnalysisData/AnalysisData/Resources.resx index 64a5ff0..7cbed1b 100644 --- a/AnalysisData/AnalysisData/Resources.resx +++ b/AnalysisData/AnalysisData/Resources.resx @@ -61,7 +61,7 @@ Node not found Exception ! - No file uploaded exception ! + No file uploaded ! Edge not found exception !