Put function with profile picture works on API layer
This commit is contained in:
parent
067a3e8d3c
commit
781e3dd87b
@ -23,9 +23,12 @@ namespace API.Application.Users.Commands
|
|||||||
|
|
||||||
public async Task<IActionResult> Handle(UpdateUserDTO updateUserDTO)
|
public async Task<IActionResult> Handle(UpdateUserDTO updateUserDTO)
|
||||||
{
|
{
|
||||||
List<User> existingUsers = await _repository.QueryAllUsersAsync();
|
|
||||||
User currentUser = await _repository.QueryUserByIdAsync(updateUserDTO.Id);
|
User currentUser = await _repository.QueryUserByIdAsync(updateUserDTO.Id);
|
||||||
|
|
||||||
|
if (updateUserDTO.Username != null || updateUserDTO.Email != null)
|
||||||
|
{
|
||||||
|
List<User> existingUsers = await _repository.QueryAllUsersAsync();
|
||||||
|
|
||||||
foreach (User existingUser in existingUsers)
|
foreach (User existingUser in existingUsers)
|
||||||
{
|
{
|
||||||
if (existingUser.Username == updateUserDTO.Username && existingUser.Username != currentUser.Username)
|
if (existingUser.Username == updateUserDTO.Username && existingUser.Username != currentUser.Username)
|
||||||
@ -38,7 +41,14 @@ namespace API.Application.Users.Commands
|
|||||||
return new ConflictObjectResult(new { message = "Email is already in use." });
|
return new ConflictObjectResult(new { message = "Email is already in use." });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (updateUserDTO.Password != "½")
|
|
||||||
|
if (updateUserDTO.Username != null)
|
||||||
|
currentUser.Username = updateUserDTO.Username;
|
||||||
|
if (updateUserDTO.Email != null)
|
||||||
|
currentUser.Email = updateUserDTO.Email;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updateUserDTO.Password != null)
|
||||||
{
|
{
|
||||||
if (IsPasswordSecure(updateUserDTO.Password))
|
if (IsPasswordSecure(updateUserDTO.Password))
|
||||||
{
|
{
|
||||||
@ -50,20 +60,16 @@ namespace API.Application.Users.Commands
|
|||||||
return new ConflictObjectResult(new { message = "Password is not secure." });
|
return new ConflictObjectResult(new { message = "Password is not secure." });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (updateUserDTO.Username != "½")
|
|
||||||
currentUser.Username = updateUserDTO.Username;
|
|
||||||
if (updateUserDTO.Email != "½")
|
|
||||||
currentUser.Email = updateUserDTO.Email;
|
|
||||||
|
|
||||||
string imageUrl = null;
|
string imageUrl = null;
|
||||||
if (updateUserDTO.ProfilePicture != null && updateUserDTO.ProfilePicture.Length > 0)
|
if (updateUserDTO.ProfilePicture != null && updateUserDTO.ProfilePicture.Length > 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var fileStream = updateUserDTO.ProfilePicture.OpenReadStream())
|
using (var fileStream = updateUserDTO.ProfilePicture.OpenReadStream())
|
||||||
{
|
{
|
||||||
imageUrl = await _r2Service.UploadToR2(fileStream, "PP" + updateUserDTO.Id);
|
imageUrl = await _r2Service.UploadToR2(fileStream, "PP" + updateUserDTO.Id+".png");
|
||||||
currentUser.ProfilePicture = imageUrl;
|
currentUser.ProfilePicture = imageUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,7 +77,6 @@ namespace API.Application.Users.Commands
|
|||||||
{
|
{
|
||||||
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
|
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool success = await _repository.UpdateUserAsync(currentUser);
|
bool success = await _repository.UpdateUserAsync(currentUser);
|
||||||
|
@ -37,10 +37,10 @@ public class SignUpDTO
|
|||||||
public class UpdateUserDTO
|
public class UpdateUserDTO
|
||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public string Email { get; set; }
|
public string? Email { get; set; }
|
||||||
public string Username { get; set; }
|
public string? Username { get; set; }
|
||||||
public string Password { get; set; }
|
public string? Password { get; set; }
|
||||||
public IFormFile ProfilePicture { get; set; }
|
public IFormFile? ProfilePicture { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ namespace API.Persistence.Services
|
|||||||
var credentials = new BasicAWSCredentials(accessKey, secretKey);
|
var credentials = new BasicAWSCredentials(accessKey, secretKey);
|
||||||
var config = new AmazonS3Config
|
var config = new AmazonS3Config
|
||||||
{
|
{
|
||||||
ServiceURL = "https://a6051dbbe0af70488aff47b9f4d9fc1c.r2.cloudflarestorage.com",
|
ServiceURL = "https://a6051dbbe0af70488aff47b9f4d9fc1c.r2.cloudflarestorage.com/h4picturebucket",
|
||||||
ForcePathStyle = true
|
ForcePathStyle = true
|
||||||
};
|
};
|
||||||
_s3Client = new AmazonS3Client(credentials, config);
|
_s3Client = new AmazonS3Client(credentials, config);
|
||||||
@ -29,7 +29,7 @@ namespace API.Persistence.Services
|
|||||||
var request = new PutObjectRequest
|
var request = new PutObjectRequest
|
||||||
{
|
{
|
||||||
InputStream = fileStream,
|
InputStream = fileStream,
|
||||||
BucketName = "h4fil",
|
BucketName = "h4picturebucket",
|
||||||
Key = fileName,
|
Key = fileName,
|
||||||
DisablePayloadSigning = true
|
DisablePayloadSigning = true
|
||||||
};
|
};
|
||||||
@ -41,7 +41,7 @@ namespace API.Persistence.Services
|
|||||||
throw new AmazonS3Exception($"Error uploading file to S3. HTTP Status Code: {response.HttpStatusCode}");
|
throw new AmazonS3Exception($"Error uploading file to S3. HTTP Status Code: {response.HttpStatusCode}");
|
||||||
}
|
}
|
||||||
|
|
||||||
var imageUrl = $"https://h4file.magsapi.com/{fileName}";
|
var imageUrl = $"https://pub-bf709b641048489ca70f693673e3e04c.r2.dev/{fileName}";
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,5 +12,7 @@
|
|||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Data Source=database.sqlite3"
|
"DefaultConnection": "Data Source=database.sqlite3"
|
||||||
}
|
},
|
||||||
|
"AccessKey": "783f66e09776e12d0f49fd5d6be0eedf",
|
||||||
|
"SecretKey": "4485259e3dd9f00fa6d9eedb3c5f55a9f256d4ea951d5a4fc0acd3e9d261da12"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user