Skip to content

Commit

Permalink
fix (User) : remove jwt update after update user information
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdijafariii committed Sep 8, 2024
2 parents 8a13a89 + e899001 commit cddcb1c
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 28 deletions.
7 changes: 5 additions & 2 deletions AnalysisData/AnalysisData.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5a6db5c4_002D6b10_002D4bf8_002D8325_002D71b5cea4c333/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="UpdateCategory_ShouldReturnOk_WhenCategoryIsUpdatedSuccessfully" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=219940aa_002Df21c_002D44cf_002D9307_002Dab482de92a14/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="RoleRepositoryTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Graph.Controllers.CategoryControllersTest.UpdateCategory_ShouldReturnOk_WhenCategoryIsUpdatedSuccessfully&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.User.Repository.RoleRepository.RoleRepositoryTests&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5a6db5c4_002D6b10_002D4bf8_002D8325_002D71b5cea4c333/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="UpdateCategory_ShouldReturnOk_WhenCategoryIsUpdatedSuccessfully" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="C:\Users\Mahdi\Desktop\code star proje\Summer1403-Project-Group03-Backend\AnalysisData\TestProject" Presentation="&amp;lt;TestProject&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String>



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<IActionResult> DeleteCategory(int id)

[Authorize(Policy = "silver")]
[HttpPut]
public async Task<IActionResult> UpdateCategory([FromBody] NewCategoryDto newCategory)
public async Task<IActionResult> UpdateCategory([FromBody] UpdateCategoryDto newCategory)
{
await _categoryService.UpdateAsync(newCategory);
return Ok(new { massage = "updated successfully" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ namespace AnalysisData.Dtos.GraphDto.CategoryDto;

public class NewCategoryDto
{
[Required] public int Id { get; set; }
[Required] public string Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace AnalysisData.Dtos.GraphDto.CategoryDto;

public class UpdateCategoryDto
{
[Required] public int Id { get; set; }
[Required] public string Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AnalysisData.Exception.UserException;

public class AdminProtectedException : ServiceException
{
public AdminProtectedException() : base(Resources.AdminProtectedException, StatusCodes.Status400BadRequest)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ public async Task AddAsync(AttributeEdge entity)
}
public async Task AddRangeAsync(IEnumerable<AttributeEdge> attributeEdges)
{
await _context.AttributeEdges.AddRangeAsync(attributeEdges);
await _context.SaveChangesAsync();
var existingIds = await _context.AttributeEdges
.Where(ae => attributeEdges.Select(e => e.Id).Contains(ae.Id))
.Select(ae => ae.Id)
.ToListAsync();
var newAttributeEdges = attributeEdges.Where(ae => !existingIds.Contains(ae.Id)).ToList();
if (newAttributeEdges.Any())
{
await _context.AttributeEdges.AddRangeAsync(newAttributeEdges);
await _context.SaveChangesAsync();
}
}
public async Task<IEnumerable<AttributeEdge>> GetAllAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ public async Task<IEnumerable<EntityEdge>> GetAllAsync()
}
public async Task AddRangeAsync(IEnumerable<EntityEdge> entityEdges)
{
await _context.EntityEdges.AddRangeAsync(entityEdges);
await _context.SaveChangesAsync();
var existingEntityIds = await _context.EntityNodes.Select(en => en.Id).ToListAsync();

var validEntityEdges = entityEdges
.Where(ee => existingEntityIds.Contains(ee.EntityIDSource))
.ToList();

if (validEntityEdges.Any())
{
await _context.EntityEdges.AddRangeAsync(validEntityEdges);
await _context.SaveChangesAsync();
}

}
public async Task<EntityEdge> GetByIdAsync(Guid id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ public async Task<IEnumerable<ValueEdge>> GetAllAsync()
}
public async Task AddRangeAsync(IEnumerable<ValueEdge> valueEdges)
{
await _context.ValueEdges.AddRangeAsync(valueEdges);
await _context.SaveChangesAsync();
var existingEntityIds = await _context.EntityEdges.Select(ee => ee.Id).ToListAsync();

var validValueEdges = valueEdges
.Where(ve => existingEntityIds.Contains(ve.EntityId))
.ToList();

if (validValueEdges.Any())
{
await _context.ValueEdges.AddRangeAsync(validValueEdges);
await _context.SaveChangesAsync();
}
}
public async Task<ValueEdge> GetByIdAsync(Guid id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,25 @@ public async Task<AttributeNode> GetByNameAsync(string name)
{
return await _context.AttributeNodes.FirstOrDefaultAsync(x => x.Name == name);
}

public async Task AddRangeAsync(IEnumerable<AttributeNode> attributeNodes)
{
await _context.AttributeNodes.AddRangeAsync(attributeNodes);
await _context.SaveChangesAsync();
{
var existingIds = await _context.AttributeNodes
.Where(an => attributeNodes.Select(n => n.Id).Contains(an.Id))
.Select(an => an.Id)
.ToListAsync();

var newAttributeNodes = attributeNodes.Where(an => !existingIds.Contains(an.Id)).ToList();

if (newAttributeNodes.Any())
{
await _context.AttributeNodes.AddRangeAsync(newAttributeNodes);
await _context.SaveChangesAsync();
}
}
}

public async Task DeleteAsync(Guid id)
{
var entity = await _context.AttributeNodes.FindAsync(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ public async Task AddAsync(ValueNode entity)

public async Task AddRangeAsync(IEnumerable<ValueNode> valueNodes)
{
await _context.ValueNodes.AddRangeAsync(valueNodes);
await _context.SaveChangesAsync();
var existingAttributeNodeIds = await _context.AttributeNodes.Select(an => an.Id).ToListAsync();
var validValueNodes = valueNodes
.Where(vn => existingAttributeNodeIds.Contains(vn.AttributeId))
.ToList();
if (validValueNodes.Any())
{
await _context.ValueNodes.AddRangeAsync(validValueNodes);
await _context.SaveChangesAsync();
}
}
public async Task<IEnumerable<ValueNode>> GetAllAsync()
{
Expand Down
9 changes: 9 additions & 0 deletions AnalysisData/AnalysisData/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions AnalysisData/AnalysisData/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,7 @@
<data name="FailedOpetaionException" xml:space="preserve">
<value>Failed Operation ! </value>
</data>
<data name="AdminProtectedException" xml:space="preserve">
<value>You can not delete or modify admin information !</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public async Task ProcessEdgesAsync(ICsvReaderProcessor csv, IEnumerable<Attribu
while (csv.Read())
{
var entityEdge = await CreateEntityEdgeAsync(csv, from, to);
if (entityEdge.Id == null)
{
continue;
}
entityEdges.Add(entityEdge);

foreach (var header in attributeEdges)
Expand Down Expand Up @@ -76,7 +80,10 @@ private async Task<EntityEdge> CreateEntityEdgeAsync(ICsvReaderProcessor csv, st
var fromNode = await _entityNodeRepository.GetByNameAsync(entityFrom);
var toNode = await _entityNodeRepository.GetByNameAsync(entityTo);

ValidateNodesExistence(fromNode, toNode, entityFrom, entityTo);
if (!ValidateNodesExistence(fromNode, toNode, entityFrom, entityTo))
{
return new EntityEdge();
}

return new EntityEdge
{
Expand All @@ -92,7 +99,7 @@ private async Task BatchInsertAsync(List<EntityEdge> entityEdges, List<ValueEdge
await _valueEdgeRepository.AddRangeAsync(valueEdges);
}

private static void ValidateNodesExistence(EntityNode fromNode, EntityNode toNode, string entityFrom, string entityTo)
private static bool ValidateNodesExistence(EntityNode fromNode, EntityNode toNode, string entityFrom, string entityTo)
{
var missingNodeIds = new List<string>();

Expand All @@ -101,7 +108,8 @@ private static void ValidateNodesExistence(EntityNode fromNode, EntityNode toNod

if (missingNodeIds.Any())
{
throw new NodeNotFoundInEntityEdgeException(missingNodeIds);
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface ICategoryService
{
Task<PaginationCategoryDto> GetAllCategoriesAsync(int pageNumber, int pageSize);
Task AddAsync(NewCategoryDto categoryDto);
Task UpdateAsync(NewCategoryDto newCategoryDto);
Task UpdateAsync(UpdateCategoryDto newCategoryDto);
Task DeleteAsync(int id);
Task<Category> GetByIdAsync(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ public async Task AddAsync(NewCategoryDto categoryDto)
await _categoryRepository.AddAsync(category);
}

public async Task UpdateAsync(NewCategoryDto newCategoryDto)
public async Task UpdateAsync(UpdateCategoryDto updateCategoryDto)
{
var currentCategory = await _categoryRepository.GetByIdAsync(newCategoryDto.Id);
var existingCategory = await _categoryRepository.GetByNameAsync(newCategoryDto.Name);
if (existingCategory != null && newCategoryDto.Name != currentCategory.Name)
var currentCategory = await _categoryRepository.GetByIdAsync(updateCategoryDto.Id);
var existingCategory = await _categoryRepository.GetByNameAsync(updateCategoryDto.Name);
if (existingCategory != null && updateCategoryDto.Name != currentCategory.Name)
{
throw new CategoryAlreadyExist();
}

currentCategory.Name = newCategoryDto.Name;
currentCategory.Name = updateCategoryDto.Name;
await _categoryRepository.UpdateAsync(currentCategory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public AdminService(IUserRepository userRepository, IValidationService validatio
public async Task UpdateUserInformationByAdminAsync(Guid id, UpdateAdminDto updateAdminDto)
{
var user = await _userRepository.GetUserByIdAsync(id);
if (user !=null && user.Username == "admin")
{
throw new AdminProtectedException();
}

await ValidateUserInformation(user, updateAdminDto);
_validationService.EmailCheck(updateAdminDto.Email);
Expand Down Expand Up @@ -68,14 +72,18 @@ private async Task SetUpdatedInformation(User user, UpdateAdminDto updateAdminDt
{
throw new RoleNotFoundException();
}

user.Role = role;
user.RoleId = role.Id;
await _userRepository.UpdateUserAsync(user.Id, user);
}


public async Task<bool> DeleteUserAsync(Guid id)
{
var user = await _userRepository.GetUserByIdAsync(id);
if (user != null && user.Username == "admin")
{
throw new AdminProtectedException();
}
var isDelete = await _userRepository.DeleteUserAsync(id);
if (!isDelete)
throw new UserNotFoundException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public async Task UpdateCategory_ShouldReturnOk_WhenCategoryIsUpdatedSuccessfull
{
// Arrange
int categoryId = 1;
var newCategoryDto = new NewCategoryDto { Id = 1, Name = "UpdatedCategory" };
var newCategoryDto = new UpdateCategoryDto() { Id = 1, Name = "UpdatedCategory" };

// Act
var result = await _controller.UpdateCategory(newCategoryDto);
Expand Down

0 comments on commit cddcb1c

Please sign in to comment.