mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
optimization sql
This commit is contained in:
@@ -16,6 +16,7 @@ public class AdminsRepository(GovorDbContext context, IObjectValidator<Admin> va
|
||||
return await _context.Admins
|
||||
.AsNoTracking()
|
||||
.Include(a => a.User)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("Database is empty"));
|
||||
}
|
||||
|
||||
@@ -24,6 +25,7 @@ public class AdminsRepository(GovorDbContext context, IObjectValidator<Admin> va
|
||||
return await _context.Admins
|
||||
.AsNoTracking()
|
||||
.Include(a => a.User)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(u => u.UserId == id)
|
||||
?? throw new NotFoundByKeyException<Guid>(id, "User with given id does not exist");
|
||||
}
|
||||
@@ -60,11 +62,7 @@ public class AdminsRepository(GovorDbContext context, IObjectValidator<Admin> va
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new NotFoundByKeyException<Guid>(admin.UserId);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UpdateException($"Not found admin by given id {admin.UserId}", ex);
|
||||
throw new UpdateException($"Not found admin by given id {admin.UserId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
using Govor.Core;
|
||||
|
||||
namespace Govor.Data.Repositories.Exceptions;
|
||||
|
||||
public class UpdateException(string s, Exception ex)
|
||||
: Exception(s, ex);
|
||||
public class UpdateException : GovorCoreException
|
||||
{
|
||||
public UpdateException(string s)
|
||||
: base(s) { }
|
||||
|
||||
public UpdateException(string s, Exception ex)
|
||||
: base(s, ex) { }
|
||||
}
|
||||
@@ -24,6 +24,7 @@ public class FriendshipsRepository : IFriendshipsRepository
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Requester)
|
||||
.Include(x => x.Addressee)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("Database is empty"));
|
||||
}
|
||||
|
||||
@@ -33,6 +34,7 @@ public class FriendshipsRepository : IFriendshipsRepository
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Requester)
|
||||
.Include(x => x.Addressee)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(x => x.Id == id)
|
||||
?? throw new NotFoundByKeyException<Guid>(id, "Friendship with given id was not found");
|
||||
}
|
||||
@@ -43,6 +45,7 @@ public class FriendshipsRepository : IFriendshipsRepository
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Requester)
|
||||
.Include(x => x.Addressee)
|
||||
.AsSplitQuery()
|
||||
.Where(x => x.RequesterId == userId)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(userId, "Friendship with given user id was not found"));
|
||||
}
|
||||
@@ -81,11 +84,7 @@ public class FriendshipsRepository : IFriendshipsRepository
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new NotFoundByKeyException<Guid>(friendship.Id);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UpdateException($"Not found friendship by given id {friendship.Id}", ex);
|
||||
throw new UpdateException($"Not found friendship by given id {friendship.Id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -23,6 +23,7 @@ public class InvitesRepository : IInvitesRepository
|
||||
return await _context.Invitations
|
||||
.AsNoTracking()
|
||||
.Include(i => i.Users)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("Database is empty"));
|
||||
}
|
||||
|
||||
@@ -31,6 +32,7 @@ public class InvitesRepository : IInvitesRepository
|
||||
return await _context.Invitations
|
||||
.AsNoTracking()
|
||||
.Include(i => i.Users)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(i => i.Id == id)
|
||||
?? throw new NotFoundByKeyException<Guid>(id, "Invitation with given id does not exist");
|
||||
}
|
||||
@@ -40,6 +42,7 @@ public class InvitesRepository : IInvitesRepository
|
||||
return await _context.Invitations
|
||||
.AsNoTracking()
|
||||
.Include(i => i.Users)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(i => i.Code == code)
|
||||
?? throw new NotFoundByKeyException<string>(code, "Invitation with given code does not exist");
|
||||
}
|
||||
@@ -49,6 +52,7 @@ public class InvitesRepository : IInvitesRepository
|
||||
return await _context.Invitations
|
||||
.AsNoTracking()
|
||||
.Include(i => i.Users)
|
||||
.AsSplitQuery()
|
||||
.Where(i => i.IsAdmin)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<bool>(true, "Admins invites do not exist"));
|
||||
}
|
||||
@@ -94,11 +98,7 @@ public class InvitesRepository : IInvitesRepository
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new NotFoundByKeyException<Guid>(invitation.Id, "Invitation with given data invalid");
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UpdateException($"Not found invitation by given id {invitation.Id}", ex);
|
||||
throw new UpdateException($"Not found invitation by given id {invitation.Id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -23,6 +23,7 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository
|
||||
return await _context.MediaAttachments
|
||||
.AsNoTracking()
|
||||
.Include(ma => ma.Message)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("No media attachments found."));
|
||||
}
|
||||
|
||||
@@ -31,6 +32,7 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository
|
||||
return await _context.MediaAttachments
|
||||
.AsNoTracking()
|
||||
.Include(ma => ma.Message)
|
||||
.AsSplitQuery()
|
||||
.Where(m => m.MessageId == messageId)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(messageId, "No media attachments found by given message Id"));
|
||||
}
|
||||
@@ -40,6 +42,7 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository
|
||||
return await _context.MediaAttachments
|
||||
.AsNoTracking()
|
||||
.Include(ma => ma.Message)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(m => m.Id == id)
|
||||
?? throw new NotFoundByKeyException<Guid>(id, "No media attachments found by given Id");
|
||||
}
|
||||
@@ -80,11 +83,7 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new NotFoundByKeyException<Guid>(attachments.Id);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UpdateException($"Not found attachments by given id {attachments.Id}", ex);
|
||||
throw new UpdateException($"Not found attachments by given id {attachments.Id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -22,6 +22,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database"));
|
||||
}
|
||||
|
||||
@@ -30,6 +31,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(m => m.Id == messageId)
|
||||
?? throw new NotFoundByKeyException<Guid>(messageId, "Message with given id does not exist");
|
||||
}
|
||||
@@ -39,6 +41,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.AsSplitQuery()
|
||||
.Where(m => m.SenderId == senderId)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(senderId, "Messages with given sender id do not exist"));
|
||||
}
|
||||
@@ -48,6 +51,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.AsSplitQuery()
|
||||
.Where(m => m.RecipientId == receiverId)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(receiverId, "Messages with given recipient id do not exist"));
|
||||
}
|
||||
@@ -57,6 +61,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.AsSplitQuery()
|
||||
.Where(m => m.SenderId == senderId
|
||||
&& m.RecipientId == receiverId
|
||||
&& m.RecipientType == recipientType)
|
||||
@@ -68,6 +73,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.AsSplitQuery()
|
||||
.Where(m => m.SentAt == date)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<DateTime>(date, "Messages sent at date do not exist"));
|
||||
}
|
||||
@@ -116,11 +122,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new NotFoundByKeyException<Guid>(message.Id);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UpdateException($"Not found message by given id {message.Id}", ex);
|
||||
throw new UpdateException($"Not found message by given id {message.Id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -25,6 +25,7 @@ public class UsersRepository : IUsersRepository
|
||||
.Include(u => u.Invite)
|
||||
.Include(u => u.ReceivedFriendRequests)
|
||||
.Include(u => u.SentFriendRequests)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("Users in Database not exists"));
|
||||
}
|
||||
|
||||
@@ -38,6 +39,7 @@ public class UsersRepository : IUsersRepository
|
||||
.Include(u => u.Invite)
|
||||
.Include(u => u.ReceivedFriendRequests)
|
||||
.Include(u => u.SentFriendRequests)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(x => x.Id == id)
|
||||
?? throw new NotFoundByKeyException<Guid>(id, "User with given id does not exist");
|
||||
}
|
||||
@@ -53,6 +55,7 @@ public class UsersRepository : IUsersRepository
|
||||
.Include(u => u.Invite)
|
||||
.Include(u => u.ReceivedFriendRequests)
|
||||
.Include(u => u.SentFriendRequests)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<IEnumerable<Guid>>(ids,"Users with given ids not found"));
|
||||
}
|
||||
|
||||
@@ -66,6 +69,7 @@ public class UsersRepository : IUsersRepository
|
||||
.Include(u => u.Invite)
|
||||
.Include(u => u.ReceivedFriendRequests)
|
||||
.Include(u => u.SentFriendRequests)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefaultAsync(x => x.Username == username)
|
||||
?? throw new NotFoundByKeyException<string>(username, "User with given username does not exist");
|
||||
}
|
||||
@@ -77,6 +81,7 @@ public class UsersRepository : IUsersRepository
|
||||
.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 =>
|
||||
@@ -99,6 +104,7 @@ public class UsersRepository : IUsersRepository
|
||||
.Include(u => u.Invite)
|
||||
.Include(u => u.ReceivedFriendRequests)
|
||||
.Include(u => u.SentFriendRequests)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<IEnumerable<string>>(usernames, "Users with given usernames not found"));
|
||||
}
|
||||
|
||||
@@ -113,6 +119,7 @@ public class UsersRepository : IUsersRepository
|
||||
.Include(u => u.Invite)
|
||||
.Include(u => u.ReceivedFriendRequests)
|
||||
.Include(u => u.SentFriendRequests)
|
||||
.AsSplitQuery()
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<DateOnly>(createdDate, "Users with given created date do not exist"));
|
||||
}
|
||||
|
||||
@@ -145,20 +152,17 @@ public class UsersRepository : IUsersRepository
|
||||
var rowsAffected = await _context.Users
|
||||
.Where(u => u.Id == user.Id)
|
||||
.ExecuteUpdateAsync(u => u
|
||||
.SetProperty(a => a.Username, user.Username)
|
||||
.SetProperty(u => u.IconId, user.IconId)
|
||||
.SetProperty(u => u.Description, user.Description)
|
||||
.SetProperty(u => u.CreatedOn, user.CreatedOn)
|
||||
.SetProperty(u => u.PasswordHash, user.PasswordHash)
|
||||
.SetProperty(u => u.WasOnline, user.WasOnline)
|
||||
.SetProperty(a => a.Username, user.Username)
|
||||
.SetProperty(u => u.IconId, user.IconId)
|
||||
.SetProperty(u => u.Description, user.Description)
|
||||
.SetProperty(u => u.CreatedOn, user.CreatedOn)
|
||||
.SetProperty(u => u.PasswordHash, user.PasswordHash)
|
||||
.SetProperty(u => u.WasOnline, user.WasOnline)
|
||||
.SetProperty(u => u.InviteId, user.InviteId)
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new NotFoundByKeyException<Guid>(user.Id);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UpdateException($"Not found user by given id {user.Id}", ex);
|
||||
throw new UpdateException($"Not found user by given id {user.Id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user