🐛 Fix relationship get status wrongly

This commit is contained in:
LittleSheep 2025-05-17 00:52:35 +08:00
parent b489a79df2
commit 6fe0b9b50a
2 changed files with 39 additions and 3 deletions

View File

@ -29,6 +29,13 @@ public class RelationshipController(AppDatabase db, RelationshipService rels) :
.Take(take) .Take(take)
.ToListAsync(); .ToListAsync();
var statuses = await db.AccountRelationships
.Where(r => r.AccountId == userId)
.ToDictionaryAsync(r => r.AccountId);
foreach (var relationship in relationships)
if (statuses.TryGetValue(relationship.RelatedId, out var status))
relationship.Status = status.Status;
Response.Headers["X-Total"] = totalCount.ToString(); Response.Headers["X-Total"] = totalCount.ToString();
return relationships; return relationships;
@ -123,6 +130,23 @@ public class RelationshipController(AppDatabase db, RelationshipService rels) :
return BadRequest(err.Message); return BadRequest(err.Message);
} }
} }
[HttpDelete("{userId:guid}/friends")]
[Authorize]
public async Task<ActionResult> DeleteFriendRequest(Guid userId)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
try
{
await rels.DeleteFriendRequest(currentUser.Id, userId);
return NoContent();
}
catch (ArgumentException err)
{
return NotFound(err.Message);
}
}
[HttpPost("{userId:guid}/friends/accept")] [HttpPost("{userId:guid}/friends/accept")]
[Authorize] [Authorize]

View File

@ -25,7 +25,7 @@ public class RelationshipService(AppDatabase db, IMemoryCache cache)
var now = Instant.FromDateTimeUtc(DateTime.UtcNow); var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
var queries = db.AccountRelationships.AsQueryable() var queries = db.AccountRelationships.AsQueryable()
.Where(r => r.AccountId == accountId && r.RelatedId == relatedId); .Where(r => r.AccountId == accountId && r.RelatedId == relatedId);
if (!ignoreExpired) queries = queries.Where(r => r.ExpiredAt > now); if (!ignoreExpired) queries = queries.Where(r => r.ExpiredAt == null || r.ExpiredAt > now);
if (status is not null) queries = queries.Where(r => r.Status == status); if (status is not null) queries = queries.Where(r => r.Status == status);
var relationship = await queries.FirstOrDefaultAsync(); var relationship = await queries.FirstOrDefaultAsync();
return relationship; return relationship;
@ -80,7 +80,19 @@ public class RelationshipService(AppDatabase db, IMemoryCache cache)
return relationship; return relationship;
} }
public async Task DeleteFriendRequest(Guid accountId, Guid relatedId)
{
var relationship = await GetRelationship(accountId, relatedId, RelationshipStatus.Pending);
if (relationship is null) throw new ArgumentException("Friend request was not found.");
db.AccountRelationships.Remove(relationship);
await db.SaveChangesAsync();
cache.Remove($"UserFriends_{accountId}");
cache.Remove($"UserFriends_{relatedId}");
}
public async Task<Relationship> AcceptFriendRelationship( public async Task<Relationship> AcceptFriendRelationship(
Relationship relationship, Relationship relationship,
RelationshipStatus status = RelationshipStatus.Friends RelationshipStatus status = RelationshipStatus.Friends
@ -115,7 +127,7 @@ public class RelationshipService(AppDatabase db, IMemoryCache cache)
public async Task<Relationship> UpdateRelationship(Guid accountId, Guid relatedId, RelationshipStatus status) public async Task<Relationship> UpdateRelationship(Guid accountId, Guid relatedId, RelationshipStatus status)
{ {
var relationship = await GetRelationship(accountId, relatedId, status); var relationship = await GetRelationship(accountId, relatedId);
if (relationship is null) throw new ArgumentException("There is no relationship between you and the user."); if (relationship is null) throw new ArgumentException("There is no relationship between you and the user.");
if (relationship.Status == status) return relationship; if (relationship.Status == status) return relationship;
relationship.Status = status; relationship.Status = status;