Skip to content

Commit

Permalink
Merge pull request #69 from abubakrmirgiyasov/main
Browse files Browse the repository at this point in the history
Main
  • Loading branch information
abubakrmirgiyasov committed Feb 27, 2024
2 parents 0cc4b83 + 90d227e commit 6868362
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
1 change: 0 additions & 1 deletion src/Mint.Infrastructure/Helpers/MinioClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public IMinioClient Client
client
.WithEndpoint("127.0.0.1:9000")
.WithCredentials("minioadmin", "minioadmin")
.WithSSL()
.Build();
}
else
Expand Down
30 changes: 15 additions & 15 deletions src/Mint.Infrastructure/Repositories/Identity/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public async Task<IEnumerable<UserFullViewModel>> GetUsersAsync(CancellationToke
throw new Exception(ex.Message, ex);
}
}

/// <summary>
/// Gets all addresses single user by id
/// </summary>
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="UserNotFoundException"></exception>
/// <exception cref="Exception"></exception>
public async Task<IEnumerable<UserAddressFullViewModel>> GetUserAddressesAsync(Guid id, CancellationToken cancellationToken = default)
{
Expand All @@ -66,13 +66,13 @@ public async Task<IEnumerable<UserAddressFullViewModel>> GetUserAddressesAsync(G
.ToListAsync(cancellationToken);

var user = users.FirstOrDefault(x => x.Id == id)
?? throw new ArgumentNullException(nameof(User), "Пользователь не существует");
?? throw new UserNotFoundException("Пользователь не существует");

return UserAddressDTOConverter.FormingMultiViewModels(user.UserAddresses);
}
catch (ArgumentNullException ex)
catch (UserNotFoundException ex)
{
throw new ArgumentNullException(ex.Message, ex);
throw new UserNotFoundException(ex.Message);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public async Task<UserJwtAuthorize> GetUserByIdAsync(Guid id, CancellationToken
.ToListAsync(cancellationToken);

var user = users.FirstOrDefault(x => x.Id == id)
?? throw new UserNotFoundException("User doesn't exists");
?? throw new UserNotFoundException("Пользователь не существует");

return _mapper.Map<UserJwtAuthorize>(user);
}
Expand Down Expand Up @@ -133,17 +133,17 @@ public async Task<string[]> GetUserRoleForAuthAsync(Guid id, CancellationToken c
.ToListAsync(cancellationToken);

var user = users.FirstOrDefault(x => x.Id == id)
?? throw new ArgumentNullException(nameof(User), "User doesn't exists");
?? throw new UserNotFoundException("Пользователь не существует");

var roles = user.UserRoles
.Select(x => x.Role.TranslateEn)
.ToArray();

return roles;
}
catch (ArgumentNullException ex)
catch (UserNotFoundException ex)
{
throw new ArgumentNullException(ex.Message, ex);
throw new UserNotFoundException(ex.Message);
}
catch (Exception ex)
{
Expand All @@ -170,13 +170,13 @@ public async Task<UserFullViewModel> GetUserByEmailAsync(string email, Cancellat
.ToListAsync(cancellationToken);

var user = users.FirstOrDefault() // x => x.Email == email
?? throw new ArgumentNullException(nameof(User), "User doesn't exists");
?? throw new UserNotFoundException("Пользователь не существует");

return UserDTOConverter.FormingSingleViewModel(user);
}
catch (ArgumentNullException ex)
catch (UserNotFoundException ex)
{
throw new ArgumentNullException(ex.Message, ex);
throw new UserNotFoundException(ex.Message);
}
catch (Exception ex)
{
Expand All @@ -203,13 +203,13 @@ public async Task<UserFullViewModel> GetUserByPhoneAsync(long phone, Cancellatio
.ToListAsync(cancellationToken);

var user = users.FirstOrDefault() // x => x.Phone == phone
?? throw new ArgumentNullException(nameof(User), "User doesn't exists");
?? throw new UserNotFoundException("Пользователь не существует");

return UserDTOConverter.FormingSingleViewModel(user);
}
catch (ArgumentNullException ex)
catch (UserNotFoundException ex)
{
throw new ArgumentNullException(ex.Message, ex);
throw new UserNotFoundException(ex.Message);
}
catch (Exception ex)
{
Expand Down
52 changes: 38 additions & 14 deletions src/Mint.Infrastructure/Services/StorageCloudService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,53 @@ public async Task<string> GetFileLinkAsync(string name, string bucket, Cancellat
}
}

public Task<bool> CreateBucketAsync(string bucket, CancellationToken cancellationToken = default)
public async Task<bool> CreateBucketAsync(string bucket, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
var makeBucketArgs = new MakeBucketArgs()
.WithBucket(bucket);

await _minio.Client.MakeBucketAsync(makeBucketArgs, cancellationToken);

_logger.LogInformation("Bucket created successfully: {Bucket}", bucket);

return true;
}
catch (Exception ex)
{
_logger.LogError("Exception Message: {Message}. \n Inner Exception: {Inner}", ex.Message, ex);
throw new Exception(ex.Message, ex);
}
}

public async Task<string> UploadFileAsync(IFormFile file, string bucket, CancellationToken cancellationToken = default)
{
try
{
using var stream = file.OpenReadStream();
if (await IsBucketExist(bucket, cancellationToken))
{
using var stream = file.OpenReadStream();

var putObjectArgs = new PutObjectArgs()
.WithBucket(bucket)
.WithObject(file.FileName)
.WithObjectSize(file.Length)
.WithContentType(file.ContentType)
.WithStreamData(stream);
var putObjectArgs = new PutObjectArgs()
.WithBucket(bucket)
.WithObject(file.FileName)
.WithObjectSize(file.Length)
.WithContentType(file.ContentType)
.WithStreamData(stream);

var res = await _minio.Client.PutObjectAsync(putObjectArgs, cancellationToken);
var res = await _minio.Client.PutObjectAsync(putObjectArgs, cancellationToken);

return res.ToString()!;
return res.ToString()!;
}
else
{
await CreateBucketAsync(bucket, cancellationToken);

await UploadFileAsync(file, bucket, cancellationToken);

return default!;
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -93,9 +119,7 @@ public async Task<bool> IsBucketExist(string bucket, CancellationToken cancellat
try
{
var args = new BucketExistsArgs().WithBucket(bucket);

await _minio.Client.BucketExistsAsync(args, cancellationToken);
return true;
return await _minio.Client.BucketExistsAsync(args, cancellationToken);
}
catch (Exception ex)
{
Expand Down

0 comments on commit 6868362

Please sign in to comment.