mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
test to make server
This commit is contained in:
@@ -9,7 +9,11 @@ public class UserConfiguration : IEntityTypeConfiguration<User>
|
||||
public void Configure(EntityTypeBuilder<User> builder)
|
||||
{
|
||||
builder.HasKey(x => x.Id);
|
||||
|
||||
|
||||
builder.HasIndex(u => u.Username).IsUnique();
|
||||
builder.HasIndex(u => u.CreatedOn);
|
||||
builder.HasIndex(u => u.WasOnline);
|
||||
|
||||
builder.HasOne(u => u.Invite)
|
||||
.WithMany(i => i.Users)
|
||||
.HasForeignKey(u => u.InviteId);
|
||||
|
||||
@@ -40,6 +40,14 @@ public class FriendshipsRepository : IFriendshipsRepository
|
||||
?? throw new NotFoundByKeyException<Guid>(id, "Friendship with given id was not found");
|
||||
}
|
||||
|
||||
public async Task<Friendship> GetFriendshipAsync(Guid fromUserId, Guid toUserId)
|
||||
{
|
||||
return await _context.Friendships
|
||||
.FirstOrDefaultAsync(x =>
|
||||
(x.RequesterId == fromUserId && x.AddresseeId == toUserId) ||
|
||||
(x.RequesterId == toUserId && x.AddresseeId == fromUserId));
|
||||
}
|
||||
|
||||
public async Task<List<Friendship>> FindByUserIdAsync(Guid userId)
|
||||
{
|
||||
return await _context.Friendships
|
||||
@@ -120,4 +128,23 @@ public class FriendshipsRepository : IFriendshipsRepository
|
||||
return _context.Friendships.AsNoTracking().Any(x => (x.RequesterId == requesterId && x.AddresseeId == addresseeId) ||
|
||||
x.RequesterId == addresseeId && x.AddresseeId == requesterId);
|
||||
}
|
||||
|
||||
public bool IsPossibilityOfCreatingFriendship(Guid requesterId, Guid addresseeId)
|
||||
{
|
||||
// Ищем любую существующую связь между пользователями
|
||||
var existingFriendship = _context.Friendships
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(x =>
|
||||
(x.RequesterId == requesterId && x.AddresseeId == addresseeId) ||
|
||||
(x.RequesterId == addresseeId && x.AddresseeId == requesterId));
|
||||
|
||||
// Если записи нет — создавать можно
|
||||
if (existingFriendship == null) return true;
|
||||
|
||||
// Если запись есть, создавать новую НЕЛЬЗЯ (нужно обновлять старую),
|
||||
// за исключением случаев, когда статус "Rejected" или "Blocked" (смотря какая логика)
|
||||
// Но обычно метод "IsPossibility" подразумевает именно "можно ли инициировать процесс"
|
||||
|
||||
return existingFriendship.Status == FriendshipStatus.Rejected;
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,14 @@ public class PrivateChatsRepository : IPrivateChatsRepository
|
||||
?? throw new NotFoundByKeyException<(Guid, Guid)>((memberAId, memberBId), "Private Chat with given members Id's does not exist");
|
||||
}
|
||||
|
||||
public async Task<List<PrivateChat>> GetAllOfUser(Guid userId)
|
||||
{
|
||||
return await _context.PrivateChats
|
||||
.AsNoTracking()
|
||||
.Where(f => f.UserAId == userId || f.UserBId == userId)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(userId, "Private Chat with given member Id's does not exist"));
|
||||
}
|
||||
|
||||
public async Task AddAsync(PrivateChat chat)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -102,8 +102,6 @@ public class UserSessionsRepository : IUserSessionsRepository
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new UpdateException($"Not found user session by given id {userSession.Id}");
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Infrastructure.Validators;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Models.Users;
|
||||
using Govor.Core.Repositories.Users;
|
||||
using Govor.Data.Repositories.Exceptions;
|
||||
@@ -77,16 +78,13 @@ public class UsersRepository : IUsersRepository
|
||||
{
|
||||
return await _context.Users
|
||||
.AsNoTracking()
|
||||
.Include(u => u.Invite)
|
||||
.Include(u => u.ReceivedFriendRequests)
|
||||
.Include(u => u.SentFriendRequests)
|
||||
.AsSplitQuery()
|
||||
.Where(u => u.Id != currentUserId &&
|
||||
u.Username.ToLower().Contains(query.ToLower()) &&
|
||||
!_context.Friendships.Any(f =>
|
||||
(f.RequesterId == currentUserId && f.AddresseeId == u.Id) ||
|
||||
(f.RequesterId == u.Id && f.AddresseeId == currentUserId)))
|
||||
.Take(20)
|
||||
((f.RequesterId == currentUserId && f.AddresseeId == u.Id) ||
|
||||
(f.RequesterId == u.Id && f.AddresseeId == currentUserId)) && f.Status != FriendshipStatus.Rejected))
|
||||
.Take(10)
|
||||
.OrderBy(u => u.Username)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<(string, Guid)>((query, currentUserId), $"Users with given query for user {currentUserId} not found"));
|
||||
}
|
||||
@@ -162,8 +160,6 @@ public class UsersRepository : IUsersRepository
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new UpdateException($"Not found user by given id {user.Id}");
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user