diff --git a/Govor.API/Controllers/AdminStuff/InviteUserController.cs b/Govor.API/Controllers/AdminStuff/InviteUserController.cs index 0e2724b..372549a 100644 --- a/Govor.API/Controllers/AdminStuff/InviteUserController.cs +++ b/Govor.API/Controllers/AdminStuff/InviteUserController.cs @@ -52,13 +52,13 @@ public class InviteUserController : Controller _logger.LogInformation("Getting all active invitations by administrator"); var read = await _repository.GetAllAsync(); - var result = read.Where(x => x.IsActive == true).ToList(); + var result = read.Where(x => x.IsActive).ToList(); - List dtos = new List(); + List dtos = new List(); foreach (var inv in result) { - dtos.Add(new InvitationDto(){ + dtos.Add(new InvitationResponses(){ Id = inv.Id, Description = inv.Description, IsAdmin = inv.IsAdmin, @@ -67,6 +67,7 @@ public class InviteUserController : Controller CreatedAt = inv.DateCreated, EndAt = inv.EndDate, IsActive = inv.IsActive, + ParticipantCount = inv.Users.Count, }); } @@ -87,11 +88,11 @@ public class InviteUserController : Controller _logger.LogInformation("Getting all invitations by administrator"); var read = await _repository.GetAllAsync(); - List dtos = new List(); + List dtos = new List(); foreach (var inv in read) { - dtos.Add(new InvitationDto(){ + dtos.Add(new InvitationResponses(){ Id = inv.Id, Description = inv.Description, IsAdmin = inv.IsAdmin, @@ -100,6 +101,7 @@ public class InviteUserController : Controller CreatedAt = inv.DateCreated, EndAt = inv.EndDate, IsActive = inv.IsActive, + ParticipantCount = inv.Users.Count, }); } @@ -120,18 +122,18 @@ public class InviteUserController : Controller _logger.LogInformation("Getting invitations {id} by administrator"); var read = await _repository.FindByIdAsync(id); - var response = new InvitationDto(){ - Id = read.Id, - Description = read.Description, - IsAdmin = read.IsAdmin, - MaxParticipants = read.MaxParticipants, - Code = read.Code, - CreatedAt = read.DateCreated, - EndAt = read.EndDate, - IsActive = read.IsActive, + var response = new InvitationResponses(){ + Id = read.Id, + Description = read.Description, + IsAdmin = read.IsAdmin, + MaxParticipants = read.MaxParticipants, + Code = read.Code, + CreatedAt = read.DateCreated, + EndAt = read.EndDate, + IsActive = read.IsActive, + ParticipantCount = read.Users.Count, }; - return Ok(response); } catch (Exception e) diff --git a/Govor.API/Extensions/ConfigurationProgramExtensions.cs b/Govor.API/Extensions/ConfigurationProgramExtensions.cs index 15b3eeb..b09764b 100644 --- a/Govor.API/Extensions/ConfigurationProgramExtensions.cs +++ b/Govor.API/Extensions/ConfigurationProgramExtensions.cs @@ -67,19 +67,29 @@ public static class ConfigurationProgramExtensions public static void AddGovorDbContext(this IServiceCollection services, IConfiguration configuration) { - var useMySql = configuration.GetValue("UseMySql"); // Получаем значение из конфигурации + var useMySql = configuration.GetValue("UseMySql"); if (useMySql) { services.AddDbContext(options => - options.UseMySql(configuration.GetConnectionString(nameof(GovorDbContext)), - new MySqlServerVersion(new Version(8, 0, 21)))); + { + options + .UseMySql( + configuration.GetConnectionString(nameof(GovorDbContext)), + new MySqlServerVersion(new Version(8, 0, 21)) + ); + options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); + }); } else { services.AddDbContext(options => - options.UseNpgsql(configuration.GetConnectionString(nameof(GovorDbContext)))); + { + options.UseNpgsql(configuration.GetConnectionString(nameof(GovorDbContext))); + options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); + }); } } + } \ No newline at end of file diff --git a/Govor.Contracts/DTOs/InvitationDto.cs b/Govor.Contracts/Responses/Admins/InvitationResponses.cs similarity index 81% rename from Govor.Contracts/DTOs/InvitationDto.cs rename to Govor.Contracts/Responses/Admins/InvitationResponses.cs index 3cc7838..89fdf0d 100644 --- a/Govor.Contracts/DTOs/InvitationDto.cs +++ b/Govor.Contracts/Responses/Admins/InvitationResponses.cs @@ -1,6 +1,6 @@ namespace Govor.Contracts.DTOs; -public class InvitationDto +public class InvitationResponses { public Guid Id { get; set; } public bool IsAdmin { get; set; } @@ -9,5 +9,6 @@ public class InvitationDto public DateTime CreatedAt { get; set; } public DateTime EndAt { get; set; } public int MaxParticipants { get; set; } + public int ParticipantCount { get; set; } public string Description { get; set; } } \ No newline at end of file diff --git a/Govor.Data/Repositories/AdminsRepository.cs b/Govor.Data/Repositories/AdminsRepository.cs index 0e5783b..d6a050c 100644 --- a/Govor.Data/Repositories/AdminsRepository.cs +++ b/Govor.Data/Repositories/AdminsRepository.cs @@ -16,6 +16,7 @@ public class AdminsRepository(GovorDbContext context, IObjectValidator 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 va return await _context.Admins .AsNoTracking() .Include(a => a.User) + .AsSplitQuery() .FirstOrDefaultAsync(u => u.UserId == id) ?? throw new NotFoundByKeyException(id, "User with given id does not exist"); } @@ -60,11 +62,7 @@ public class AdminsRepository(GovorDbContext context, IObjectValidator va ); if (rowsAffected == 0) - throw new NotFoundByKeyException(admin.UserId); - } - catch (NotFoundByKeyException 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) { diff --git a/Govor.Data/Repositories/Exceptions/UpdateException.cs b/Govor.Data/Repositories/Exceptions/UpdateException.cs index 25c8a70..a44e54c 100644 --- a/Govor.Data/Repositories/Exceptions/UpdateException.cs +++ b/Govor.Data/Repositories/Exceptions/UpdateException.cs @@ -1,4 +1,12 @@ +using Govor.Core; + namespace Govor.Data.Repositories.Exceptions; -public class UpdateException(string s, Exception ex) - : Exception(s, ex); \ No newline at end of file +public class UpdateException : GovorCoreException +{ + public UpdateException(string s) + : base(s) { } + + public UpdateException(string s, Exception ex) + : base(s, ex) { } +} \ No newline at end of file diff --git a/Govor.Data/Repositories/FriendshipsRepository.cs b/Govor.Data/Repositories/FriendshipsRepository.cs index 1e7eed6..f2a0d7a 100644 --- a/Govor.Data/Repositories/FriendshipsRepository.cs +++ b/Govor.Data/Repositories/FriendshipsRepository.cs @@ -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(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(userId, "Friendship with given user id was not found")); } @@ -81,11 +84,7 @@ public class FriendshipsRepository : IFriendshipsRepository ); if (rowsAffected == 0) - throw new NotFoundByKeyException(friendship.Id); - } - catch (NotFoundByKeyException 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) { diff --git a/Govor.Data/Repositories/InvitesRepository.cs b/Govor.Data/Repositories/InvitesRepository.cs index 5ae9da4..9e2c2f3 100644 --- a/Govor.Data/Repositories/InvitesRepository.cs +++ b/Govor.Data/Repositories/InvitesRepository.cs @@ -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(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(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(true, "Admins invites do not exist")); } @@ -94,11 +98,7 @@ public class InvitesRepository : IInvitesRepository ); if (rowsAffected == 0) - throw new NotFoundByKeyException(invitation.Id, "Invitation with given data invalid"); - } - catch (NotFoundByKeyException 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) { diff --git a/Govor.Data/Repositories/MediaAttachmentsRepository.cs b/Govor.Data/Repositories/MediaAttachmentsRepository.cs index 83ded41..391c18c 100644 --- a/Govor.Data/Repositories/MediaAttachmentsRepository.cs +++ b/Govor.Data/Repositories/MediaAttachmentsRepository.cs @@ -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(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(id, "No media attachments found by given Id"); } @@ -80,11 +83,7 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository ); if (rowsAffected == 0) - throw new NotFoundByKeyException(attachments.Id); - } - catch (NotFoundByKeyException 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) { diff --git a/Govor.Data/Repositories/MessagesRepository.cs b/Govor.Data/Repositories/MessagesRepository.cs index 3da8cd6..bad8ce2 100644 --- a/Govor.Data/Repositories/MessagesRepository.cs +++ b/Govor.Data/Repositories/MessagesRepository.cs @@ -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(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(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(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(date, "Messages sent at date do not exist")); } @@ -116,11 +122,7 @@ public class MessagesRepository : IMessagesRepository ); if (rowsAffected == 0) - throw new NotFoundByKeyException(message.Id); - } - catch (NotFoundByKeyException 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) { diff --git a/Govor.Data/Repositories/UsersRepository.cs b/Govor.Data/Repositories/UsersRepository.cs index d665b8d..c9822aa 100644 --- a/Govor.Data/Repositories/UsersRepository.cs +++ b/Govor.Data/Repositories/UsersRepository.cs @@ -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(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>(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(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>(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(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(user.Id); - } - catch (NotFoundByKeyException 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) {