From 781e3dd87b05039d7cab1cdbae6bdc606f89e3b6 Mon Sep 17 00:00:00 2001 From: LilleBRG Date: Thu, 5 Sep 2024 11:38:47 +0200 Subject: [PATCH] Put function with profile picture works on API layer --- API/Application/Users/Commands/UpdateUser.cs | 37 +++++++++++--------- API/Models/User.cs | 8 ++--- API/Persistence/Services/R2Service.cs | 6 ++-- API/Program.cs | 4 +-- API/appsettings.Development.json | 4 ++- 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/API/Application/Users/Commands/UpdateUser.cs b/API/Application/Users/Commands/UpdateUser.cs index 0c1871c..e51e0fb 100644 --- a/API/Application/Users/Commands/UpdateUser.cs +++ b/API/Application/Users/Commands/UpdateUser.cs @@ -23,22 +23,32 @@ namespace API.Application.Users.Commands public async Task Handle(UpdateUserDTO updateUserDTO) { - List existingUsers = await _repository.QueryAllUsersAsync(); User currentUser = await _repository.QueryUserByIdAsync(updateUserDTO.Id); - foreach (User existingUser in existingUsers) + if (updateUserDTO.Username != null || updateUserDTO.Email != null) { - if (existingUser.Username == updateUserDTO.Username && existingUser.Username != currentUser.Username) + List existingUsers = await _repository.QueryAllUsersAsync(); + + foreach (User existingUser in existingUsers) { - return new ConflictObjectResult(new { message = "Username is already in use." }); + if (existingUser.Username == updateUserDTO.Username && existingUser.Username != currentUser.Username) + { + return new ConflictObjectResult(new { message = "Username is already in use." }); + } + + if (existingUser.Email == updateUserDTO.Email && existingUser.Email != currentUser.Email) + { + return new ConflictObjectResult(new { message = "Email is already in use." }); + } } - if (existingUser.Email == updateUserDTO.Email && existingUser.Email != currentUser.Email) - { - return new ConflictObjectResult(new { message = "Email is already in use." }); - } + if (updateUserDTO.Username != null) + currentUser.Username = updateUserDTO.Username; + if (updateUserDTO.Email != null) + currentUser.Email = updateUserDTO.Email; } - if (updateUserDTO.Password != "½") + + if (updateUserDTO.Password != null) { if (IsPasswordSecure(updateUserDTO.Password)) { @@ -50,20 +60,16 @@ namespace API.Application.Users.Commands 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; if (updateUserDTO.ProfilePicture != null && updateUserDTO.ProfilePicture.Length > 0) { - try { 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; } } @@ -71,7 +77,6 @@ namespace API.Application.Users.Commands { return new StatusCodeResult(StatusCodes.Status500InternalServerError); } - } bool success = await _repository.UpdateUserAsync(currentUser); diff --git a/API/Models/User.cs b/API/Models/User.cs index 5eb7937..620ee57 100644 --- a/API/Models/User.cs +++ b/API/Models/User.cs @@ -37,10 +37,10 @@ public class SignUpDTO public class UpdateUserDTO { public string Id { get; set; } - public string Email { get; set; } - public string Username { get; set; } - public string Password { get; set; } - public IFormFile ProfilePicture { get; set; } + public string? Email { get; set; } + public string? Username { get; set; } + public string? Password { get; set; } + public IFormFile? ProfilePicture { get; set; } } diff --git a/API/Persistence/Services/R2Service.cs b/API/Persistence/Services/R2Service.cs index c99f844..c8717b4 100644 --- a/API/Persistence/Services/R2Service.cs +++ b/API/Persistence/Services/R2Service.cs @@ -18,7 +18,7 @@ namespace API.Persistence.Services var credentials = new BasicAWSCredentials(accessKey, secretKey); var config = new AmazonS3Config { - ServiceURL = "https://a6051dbbe0af70488aff47b9f4d9fc1c.r2.cloudflarestorage.com", + ServiceURL = "https://a6051dbbe0af70488aff47b9f4d9fc1c.r2.cloudflarestorage.com/h4picturebucket", ForcePathStyle = true }; _s3Client = new AmazonS3Client(credentials, config); @@ -29,7 +29,7 @@ namespace API.Persistence.Services var request = new PutObjectRequest { InputStream = fileStream, - BucketName = "h4fil", + BucketName = "h4picturebucket", Key = fileName, DisablePayloadSigning = true }; @@ -41,7 +41,7 @@ namespace API.Persistence.Services 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; } } diff --git a/API/Program.cs b/API/Program.cs index 7566934..522abfe 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -70,7 +70,7 @@ namespace API }; }); - + // Swagger does not by default allow to use Bearer tokens // The method AddSwaggerGen with the following options grants access to address a Bearer token - @@ -101,7 +101,7 @@ namespace API new string[] { } } }); - }); + }); var connectionString = Configuration.GetConnectionString("DefaultConnection") ?? Environment.GetEnvironmentVariable("DEFAULT_CONNECTION"); builder.Services.AddDbContext(options => options.UseSqlite(connectionString)); diff --git a/API/appsettings.Development.json b/API/appsettings.Development.json index 802dbd0..7068c11 100644 --- a/API/appsettings.Development.json +++ b/API/appsettings.Development.json @@ -12,5 +12,7 @@ }, "ConnectionStrings": { "DefaultConnection": "Data Source=database.sqlite3" - } + }, + "AccessKey": "783f66e09776e12d0f49fd5d6be0eedf", + "SecretKey": "4485259e3dd9f00fa6d9eedb3c5f55a9f256d4ea951d5a4fc0acd3e9d261da12" }