Put function with profile picture works on API layer

This commit is contained in:
LilleBRG 2024-09-05 11:38:47 +02:00
parent 067a3e8d3c
commit 781e3dd87b
5 changed files with 33 additions and 26 deletions

View File

@ -23,22 +23,32 @@ namespace API.Application.Users.Commands
public async Task<IActionResult> Handle(UpdateUserDTO updateUserDTO)
{
List<User> 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<User> 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);

View File

@ -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; }
}

View File

@ -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;
}
}

View File

@ -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<AppDBContext>(options => options.UseSqlite(connectionString));

View File

@ -12,5 +12,7 @@
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=database.sqlite3"
}
},
"AccessKey": "783f66e09776e12d0f49fd5d6be0eedf",
"SecretKey": "4485259e3dd9f00fa6d9eedb3c5f55a9f256d4ea951d5a4fc0acd3e9d261da12"
}