🐛 Fix relationship get status wrongly
This commit is contained in:
		| @@ -29,6 +29,13 @@ public class RelationshipController(AppDatabase db, RelationshipService rels) : | ||||
|             .Take(take) | ||||
|             .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(); | ||||
|  | ||||
|         return relationships; | ||||
| @@ -123,6 +130,23 @@ public class RelationshipController(AppDatabase db, RelationshipService rels) : | ||||
|             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")] | ||||
|     [Authorize] | ||||
|   | ||||
| @@ -25,7 +25,7 @@ public class RelationshipService(AppDatabase db, IMemoryCache cache) | ||||
|         var now = Instant.FromDateTimeUtc(DateTime.UtcNow); | ||||
|         var queries = db.AccountRelationships.AsQueryable() | ||||
|             .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); | ||||
|         var relationship = await queries.FirstOrDefaultAsync(); | ||||
|         return relationship; | ||||
| @@ -80,7 +80,19 @@ public class RelationshipService(AppDatabase db, IMemoryCache cache) | ||||
|  | ||||
|         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( | ||||
|         Relationship relationship, | ||||
|         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) | ||||
|     { | ||||
|         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.Status == status) return relationship; | ||||
|         relationship.Status = status; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user