Skip to content

Commit

Permalink
fix(user) : upload image fix with public read (#97)
Browse files Browse the repository at this point in the history
* fix(user) : upload image fix with public read

* fix(search graph ) : search with pagination

* fix(User) : email reset now is with email dto
  • Loading branch information
mahdijafariii authored Sep 7, 2024
1 parent a92331e commit ea611cb
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 37 deletions.
4 changes: 2 additions & 2 deletions AnalysisData/AnalysisData.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=4036527e_002D1256_002D400e_002D9592_002Dd58dfbe85769/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;TestProject&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="C:\Users\amir\Desktop\New folder (4)\Summer1403-Project-Group03-Backend\AnalysisData\TestProject" Presentation="&amp;lt;TestProject&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=57d88556_002Dabe9_002D4ba5_002Da041_002D358ccf783382/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="AddFileToDb_ShouldReturnCategoryId_WhenCategoryIdAdded" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=57d88556_002Dabe9_002D4ba5_002Da041_002D358ccf783382/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="AddFileToDb_ShouldReturnCategoryId_WhenCategoryIdAdded" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="C:\Users\Mahdi\Desktop\New folder (2)\Summer1403-Project-Group03-Backend\AnalysisData\TestProject" Presentation="&amp;lt;TestProject&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=580d1d66_002D6a9d_002D41c5_002D9bbc_002D4f090dd2983a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="GetAllNodesAsync_ShouldReturnsPaginatedNodes_WhenNodesExist #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
Expand Down Expand Up @@ -135,7 +135,7 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=c8b452a4_002D3e52_002D45a2_002D959c_002Dbbbf101ed826/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;TestProject&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="C:\Users\Mahdi\Desktop\New folder (2)\Summer1403-Project-Group03-Backend\AnalysisData\TestProject" Presentation="&amp;lt;TestProject&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=ce35ecc6_002D100e_002D4f4e_002Daba0_002D7678c76dbbbe/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="DeleteRole_ShouldRemovesRoleAndReturnsTrue_WhenRoleExists" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=ce35ecc6_002D100e_002D4f4e_002Daba0_002D7678c76dbbbe/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="DeleteRole_ShouldRemovesRoleAndReturnsTrue_WhenRoleExists" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="C:\Users\Mahdi\Desktop\New folder (2)\Summer1403-Project-Group03-Backend\AnalysisData\TestProject" Presentation="&amp;lt;TestProject&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=fe4fe44b_002D7953_002D4423_002Dac04_002Da7f1b464e5c0/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="ResetPasswordAsync_ShouldCallPasswordCheck_WhenCalled" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public async Task<IActionResult> GetRelationalEdgeByNodeId([FromQuery] int nodeI

[Authorize(Policy = "bronze")]
[HttpGet("Search")]
public async Task<IActionResult> SearchEntityNode([FromQuery] string searchInput, string searchType = "contain")
public async Task<IActionResult> SearchEntityNode([FromQuery] string searchInput, string searchType = "contain",[FromQuery] int pageIndex = 0, [FromQuery] int pageSize = 10)
{
var user = User;
var result = await _graphSearchService.SearchInEntityNodeNameAsync(user, searchInput, searchType);
var result = await _graphSearchService.SearchInEntityNodeNameAsync(user, searchInput, searchType,pageIndex,pageSize);
return Ok(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace AnalysisData.Controllers.UserController;

[ApiController]
//[Authorize(Roles = "admin")]
[Route("api/[controller]")]
public class AdminController : ControllerBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ await _userService.ResetPasswordAsync(userClaim, resetPasswordDto.NewPassword,

[Authorize(Policy = "bronze")]
[HttpPost("request-reset-password")]
public async Task<IActionResult> RequestResetPassword()
public async Task<IActionResult> RequestResetPassword(EmailForResetPasswordDto resetPassword)
{
var userClaim = User;
await _resetPasswordRequestService.SendRequestToResetPassword(userClaim);
await _resetPasswordRequestService.SendRequestToResetPassword(resetPassword.Email);
return Ok(new { massage = "success" });
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using AnalysisData.Models.GraphModel.Node;

namespace AnalysisData.Dtos.GraphDto.NodeDto;

public class PaginationSearchDto
{
public List<PaginationNodeDto> Items { get; set; }
public int PageIndex { get; set; }
public int TotalItems { get; set; }

public PaginationSearchDto(List<PaginationNodeDto> items, int pageIndex, int totalItems)
{
Items = items;
PageIndex = pageIndex;
TotalItems = totalItems;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AnalysisData.Dtos.UserDto.PasswordDto;

public class EmailForResetPasswordDto
{
public string Email { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Security.Claims;
using AnalysisData.Dtos.GraphDto.NodeDto;
using AnalysisData.Models.GraphModel.Node;

namespace AnalysisData.Services.GraphService.GraphServices.Search.Abstraction;

public interface IGraphSearchService
{
Task<IEnumerable<EntityNode>> SearchInEntityNodeNameAsync(ClaimsPrincipal claimsPrincipal, string inputSearch,
string type);
Task<PaginationSearchDto> SearchInEntityNodeNameAsync(ClaimsPrincipal claimsPrincipal, string inputSearch,
string type,int pageIndex, int pageSize );
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Claims;
using AnalysisData.Dtos.GraphDto.NodeDto;
using AnalysisData.Exception.GraphException.NodeException;
using AnalysisData.Models.GraphModel.Node;
using AnalysisData.Repositories.GraphRepositories.GraphRepository.GraphNodeRepository.Abstraction;
Expand All @@ -15,8 +16,8 @@ public GraphSearchService(IGraphNodeRepository graphNodeRepository)
_graphNodeRepository = graphNodeRepository;
}

public async Task<IEnumerable<EntityNode>> SearchInEntityNodeNameAsync(ClaimsPrincipal claimsPrincipal,
string inputSearch, string type)
public async Task<PaginationSearchDto> SearchInEntityNodeNameAsync(ClaimsPrincipal claimsPrincipal,
string inputSearch, string type,int pageIndex , int pageSize)
{
var role = claimsPrincipal.FindFirstValue(ClaimTypes.Role);
var username = claimsPrincipal.FindFirstValue("id");
Expand All @@ -34,8 +35,19 @@ public async Task<IEnumerable<EntityNode>> SearchInEntityNodeNameAsync(ClaimsPri
{
throw new NodeNotFoundException();
}
var groupedNodes = entityNodes.Select(g => new PaginationNodeDto
{
Id = g.Id,
EntityName = g.Name,
})
.ToList();

return entityNodes;
var count = groupedNodes.Count;
var items = groupedNodes
.Skip(pageIndex * pageSize)
.Take(pageSize)
.ToList();
return new PaginationSearchDto(items,pageIndex,count);
}

private async Task<IEnumerable<EntityNode>> SearchEntityInNodeNameForAdminAsync(string inputSearch, string type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,30 @@ public S3FileStorageService(IConfiguration configuration)
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials(configuration["AWS:AccessKey"], configuration["AWS:SecretKey"]);
var config = new AmazonS3Config
{
ServiceURL = configuration["AWS:ServiceURL"]
ServiceURL = configuration["AWS:ServiceURL"],
ForcePathStyle = true
};
_s3Client = new AmazonS3Client(awsCredentials, config);
_bucketName = configuration["AWS:BucketName"];
}

public async Task<string> UploadFileAsync(IFormFile file, string folderName)
{
var fileKey = Path.Combine(folderName, file.FileName).Replace("\\", "/");
var fileKey = Path.Combine(folderName, Guid.NewGuid().ToString()).Replace("\\", "/");

var putRequest = new PutObjectRequest
{
BucketName = _bucketName,
Key = fileKey,
InputStream = file.OpenReadStream(),
ContentType = file.ContentType
ContentType = file.ContentType,
CannedACL = S3CannedACL.PublicRead
};

var response = await _s3Client.PutObjectAsync(putRequest);
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
var fileUrl = $"https://{_bucketName}.s3.{RegionEndpoint.USEast1.SystemName}.amazonaws.com/{fileKey}";
var fileUrl = $"https://{_bucketName}.s3.ir-thr-at1.arvanstorage.ir/{fileKey}";
return fileUrl;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ private async Task SetUpdatedInformation(User user, UpdateAdminDto updateAdminDt
user.Email = updateAdminDto.Email;
user.PhoneNumber = updateAdminDto.PhoneNumber;
user.Username = updateAdminDto.UserName;
user.Role.RoleName = updateAdminDto.RoleName;
var role = await _roleRepository.GetRoleByNameAsync(updateAdminDto.RoleName);
if (role is null)
{
throw new RoleNotFoundException();
}
user.Role = role;
await _userRepository.UpdateUserAsync(user.Id, user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace AnalysisData.Services.UserService.UserService.Abstraction;

public interface IResetPasswordRequestService
{
Task SendRequestToResetPassword(ClaimsPrincipal userClaim);
Task SendRequestToResetPassword(string email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using AnalysisData.Services.EmailService.Abstraction;
using AnalysisData.Services.JwtService.Abstraction;
using AnalysisData.Services.UserService.UserService.Abstraction;
using AnalysisData.Services.ValidationService.Abstraction;

namespace AnalysisData.Services.UserService.UserService;

Expand All @@ -12,23 +13,27 @@ public class ResetPasswordRequestService : IResetPasswordRequestService
private readonly IJwtService _jwtService;
private readonly IUserRepository _userRepository;
private readonly IEmailService _emailService;
private readonly IValidationService _validationService;

public ResetPasswordRequestService(IJwtService jwtService, IUserRepository userRepository, IEmailService emailService)
public ResetPasswordRequestService(IJwtService jwtService, IUserRepository userRepository,
IEmailService emailService, IValidationService validationService)
{
_jwtService = jwtService;
_userRepository = userRepository;
_emailService = emailService;
_validationService = validationService;
}


public async Task SendRequestToResetPassword(ClaimsPrincipal userClaim)
public async Task SendRequestToResetPassword(string email)
{
var userName = userClaim.FindFirstValue("username");
var user = await _userRepository.GetUserByUsernameAsync(userName);
_validationService.EmailCheck(email);
var user = await _userRepository.GetUserByEmailAsync(email);
if (user is null)
{
throw new UserNotFoundException();
}

await _jwtService.RequestResetPassword(user);
await _emailService.SendPasswordResetEmail(user.Email, "www.digikala.com");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public async Task SearchInEntityNodeNameAsync_ShouldReturnResults_WhenRoleIsAdmi
_graphNodeRepository.GetNodeStartsWithSearchInputForAdminAsync(searchInput).Returns(entityNodes);

// Act
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType);
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType,0,10);

// Assert
Assert.Equal(entityNodes, result);
Assert.Equal(entityNodes.Count, result.Items.Count);
await _graphNodeRepository.Received(1).GetNodeStartsWithSearchInputForAdminAsync(searchInput);
}

Expand All @@ -70,10 +70,10 @@ public async Task SearchInEntityNodeNameAsync_ShouldReturnResults_WhenRoleIsAdmi
_graphNodeRepository.GetNodeContainSearchInputForAdminAsync(searchInput).Returns(entityNodes);

// Act
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType);
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType,0,10);

// Assert
Assert.Equal(entityNodes, result);
Assert.Equal(entityNodes.Count, result.Items.Count);
await _graphNodeRepository.Received(1).GetNodeContainSearchInputForAdminAsync(searchInput);
}

Expand All @@ -94,10 +94,10 @@ public async Task SearchInEntityNodeNameAsync_ShouldReturnResults_WhenRoleIsAdmi
_graphNodeRepository.GetNodeEndsWithSearchInputForAdminAsync(searchInput).Returns(entityNodes);

// Act
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType);
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType,0,10);

// Assert
Assert.Equal(entityNodes, result);
Assert.Equal(entityNodes.Count, result.Items.Count);
await _graphNodeRepository.Received(1).GetNodeEndsWithSearchInputForAdminAsync(searchInput);
}

Expand All @@ -119,10 +119,10 @@ public async Task SearchInEntityNodeNameAsync_ShouldReturnResults_WhenRoleIsUser
_graphNodeRepository.GetNodeStartsWithSearchInputForUserAsync(Arg.Any<Guid>(),searchInput).Returns(entityNodes);

// Act
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType);
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType,0,10);

// Assert
Assert.Equal(entityNodes, result);
Assert.Equal(entityNodes.Count, result.Items.Count);
await _graphNodeRepository.Received(1).GetNodeStartsWithSearchInputForUserAsync(Arg.Any<Guid>(),searchInput);
}

Expand All @@ -143,10 +143,10 @@ public async Task SearchInEntityNodeNameAsync_ShouldReturnResults_WhenRoleIsUser
_graphNodeRepository.GetNodeContainSearchInputForUserAsync(Arg.Any<Guid>(), searchInput).Returns(entityNodes);

// Act
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType);
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType,0,10);

// Assert
Assert.Equal(entityNodes, result);
Assert.Equal(entityNodes.Count, result.Items.Count);
await _graphNodeRepository.Received(1).GetNodeContainSearchInputForUserAsync(Arg.Any<Guid>(), searchInput);
}

Expand All @@ -167,10 +167,10 @@ public async Task SearchInEntityNodeNameAsync_ShouldReturnResults_WhenRoleIsUser
_graphNodeRepository.GetNodeEndsWithSearchInputForUserAsync(Arg.Any<Guid>(), searchInput).Returns(entityNodes);

// Act
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType);
var result = await _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType,0,10);

// Assert
Assert.Equal(entityNodes, result);
Assert.Equal(entityNodes.Count, result.Items.Count);
await _graphNodeRepository.Received(1).GetNodeEndsWithSearchInputForUserAsync(Arg.Any<Guid>(), searchInput);
}

Expand All @@ -187,7 +187,7 @@ public async Task SearchInEntityNodeNameAsync_ShouldThrowNodeNotFoundException_W
.Returns(Enumerable.Empty<EntityNode>());

// Act
var action = () => _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType);
var action = () => _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType,0,10);

// Assert
await Assert.ThrowsAsync<NodeNotFoundException>(action);
Expand All @@ -207,7 +207,7 @@ public async Task SearchInEntityNodeNameAsync_ShouldThrowNodeNotFoundException_W
.Returns(Enumerable.Empty<EntityNode>());

// Act
var action =()=> _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType);
var action =()=> _sut.SearchInEntityNodeNameAsync(claimsPrincipal, searchInput, searchType,1,10);

// Assert
await Assert.ThrowsAsync<NodeNotFoundException>(action);
Expand Down

0 comments on commit ea611cb

Please sign in to comment.