-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10e57f6
commit 1edd2a1
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using FocusAPI.Data; | ||
using FocusAPI.Models; | ||
using FocusCore.Commands.Social; | ||
using FocusCore.Commands.User; | ||
using FocusCore.Models; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FocusApi.Methods.Social; | ||
public class DeleteFriend | ||
{ | ||
public class Handler : IRequestHandler<DeleteFriendCommand, Unit> | ||
{ | ||
FocusAPIContext _context; | ||
ILogger<Handler> _logger; | ||
public Handler(FocusAPIContext context, ILogger<Handler> logger) | ||
{ | ||
_context = context; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<Unit> Handle(DeleteFriendCommand command, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
// Fetch friendship | ||
var friendship = await _context.Friends | ||
.Include(f => f.User) | ||
.Include(f => f.Friend) | ||
.FirstOrDefaultAsync(f => f.UserId == command.UserId && f.FriendId == command.FriendId); | ||
|
||
// Remove friend entry | ||
friendship.User.Inviters?.Remove(friendship); | ||
|
||
friendship.Friend.Invitees?.Remove(friendship); | ||
|
||
_context.Friends.Remove(friendship); | ||
|
||
await _context.SaveChangesAsync(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error removing friendship from database"); | ||
} | ||
|
||
return Unit.Value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using MediatR; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace FocusCore.Commands.Social; | ||
public class DeleteFriendCommand : IRequest<Unit> | ||
{ | ||
public Guid UserId { get; set; } | ||
public Guid FriendId { get; set; } | ||
} |