47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using ContactService.Domain;
|
|
using ContactService.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ContactService.Infrastructure
|
|
{
|
|
public class FriendReposity : IFriendReposity
|
|
{
|
|
private readonly ContactDbContext db;
|
|
|
|
public FriendReposity(ContactDbContext db)
|
|
{
|
|
this.db = db;
|
|
}
|
|
|
|
public async Task<bool> CheckOwnerIdAndTargetIdAsync(Guid ownerId, Guid targetId)
|
|
{
|
|
var exist = await db.Friends.AnyAsync(x => x.Owner.Id == ownerId && x.Target.Id == targetId);
|
|
return exist;
|
|
}
|
|
|
|
public async Task<Friend> CreateAsync(Friend friend)
|
|
{
|
|
db.Add(friend);
|
|
return friend;
|
|
}
|
|
|
|
public Task<Friend?> FindByIdAsync(Guid id)
|
|
{
|
|
return db.Friends.FirstOrDefaultAsync(x => x.Id == id);
|
|
}
|
|
|
|
public Task<Friend?> FindByOwnerAndTargetAsync(Guid ownerId, Guid targetId)
|
|
{
|
|
return db.Friends.FirstOrDefaultAsync(x => x.Owner.Id == ownerId && x.Target.Id == targetId);
|
|
}
|
|
public async Task<IEnumerable<Friend>> FindByTargetAsync(Guid targetId)
|
|
{
|
|
return await db.Friends.Where(x => x.Target.Id == targetId).ToListAsync();
|
|
}
|
|
public async Task<IEnumerable<Friend>> FindByOwnerAsync(Guid ownerId)
|
|
{
|
|
return await db.Friends.Where(x => x.Owner.Id == ownerId).ToListAsync();
|
|
}
|
|
}
|
|
}
|