Connect profile settings to user data
This commit is contained in:
1 parent
4f7e9bac68
commit
d3b1d01058
13 files changed
+258
-32
No files matched your search
@@ -22,11 +22,41 @@ namespace OnlyPrompt.Backend.Controllers
|
||||
{
|
||||
{ nameof(UserProfileModel.Slug), new[] { "Slug already exists." } }
|
||||
});
|
||||
private static ValidationProblem UserNameExistsProblem = TypedResults.ValidationProblem(new Dictionary<string, string[]>
|
||||
{
|
||||
{ nameof(UserModel.UserName), new[] { "Username is already taken." } }
|
||||
});
|
||||
|
||||
public ProfileController(OnlyPromptContext db, IMapper mapper) : base(db, mapper)
|
||||
{
|
||||
}
|
||||
|
||||
[HttpGet("self")]
|
||||
public async Task<Results<NotFound<string>, Ok<ApiUserProfile>>> GetSelfProfileAsync()
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
if (userId is null)
|
||||
return TypedResults.NotFound("Profile not found.");
|
||||
|
||||
var profile = await _db.UserProfiles
|
||||
.Where(up => up.Id == userId.Value)
|
||||
.Select(up => new ApiUserProfile(
|
||||
up.DisplayName,
|
||||
up.Slug,
|
||||
up.Bio,
|
||||
up.AvatarUrl,
|
||||
up.Specialities,
|
||||
_db.Reviews.Where(r => r.Prompt.CreatorId == up.Id).Average(r => (double?)r.Rating) ?? 0.0,
|
||||
_db.Subscriptions.Count(s => s.SubscribedToId == up.Id)
|
||||
))
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (profile is null)
|
||||
return TypedResults.NotFound("Profile not found.");
|
||||
|
||||
return TypedResults.Ok(profile);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<Results<NotFound<string>, Ok<ApiUserProfile>>> GetProfileAsync(Identifier id)
|
||||
{
|
||||
@@ -81,6 +111,18 @@ namespace OnlyPrompt.Backend.Controllers
|
||||
if (self is null)
|
||||
return TypedResults.NotFound("Profile not found.");
|
||||
|
||||
var user = await GetUserAsync();
|
||||
if (user is null)
|
||||
return TypedResults.NotFound("User not found.");
|
||||
|
||||
if (string.IsNullOrEmpty(request.UserName) == false)
|
||||
{
|
||||
if (await _db.Users.AnyAsync(u => u.UserName == request.UserName && u.Id != user.Id))
|
||||
return UserNameExistsProblem;
|
||||
|
||||
user.UserName = request.UserName;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(request.Slug) == false)
|
||||
{
|
||||
if (await _db.UserProfiles.AnyAsync(up => up.Slug == request.Slug && up.Id != self.Id))
|
||||
@@ -89,13 +131,13 @@ namespace OnlyPrompt.Backend.Controllers
|
||||
self.Slug = request.Slug;
|
||||
}
|
||||
|
||||
if(string.IsNullOrEmpty(request.AvatarUrl) == false)
|
||||
if(request.AvatarUrl is not null)
|
||||
self.AvatarUrl = request.AvatarUrl;
|
||||
|
||||
if(string.IsNullOrEmpty(request.Bio) == false)
|
||||
if(request.Bio is not null)
|
||||
self.Bio = request.Bio;
|
||||
|
||||
if(string.IsNullOrEmpty(request.Specialities) == false)
|
||||
if(request.Specialities is not null)
|
||||
self.Specialities = request.Specialities;
|
||||
|
||||
if (string.IsNullOrEmpty(request.DisplayName) == false)
|
||||
@@ -103,7 +145,15 @@ namespace OnlyPrompt.Backend.Controllers
|
||||
|
||||
self.IsPublic = request.IsPublic;
|
||||
await _db.SaveChangesAsync();
|
||||
var result = _mapper.Map<ApiUserProfile>(self);
|
||||
var result = new ApiUserProfile(
|
||||
self.DisplayName,
|
||||
self.Slug,
|
||||
self.Bio,
|
||||
self.AvatarUrl,
|
||||
self.Specialities,
|
||||
await _db.Reviews.Where(r => r.Prompt.CreatorId == self.Id).AverageAsync(r => (double?)r.Rating) ?? 0.0,
|
||||
await _db.Subscriptions.CountAsync(s => s.SubscribedToId == self.Id)
|
||||
);
|
||||
return TypedResults.Ok(result);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user