From e00cb01846d4ebeb80b085a6e5e9fef201ea2ce5 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Sun, 23 Jul 2023 17:46:06 +0300 Subject: [PATCH] Fix many-to-many relationships --- .../Models/BoardModel.cs | 4 ++-- .../Models/CardModel.cs | 8 ++++---- .../Models/ColumnModel.cs | 4 ++-- .../Models/CommentModel.cs | 4 ++-- .../Models/RoleModel.cs | 2 +- .../Models/UserModel.cs | 10 +++++----- NG_2023_Kanban.DataLayer/Entities/Board.cs | 4 ++-- NG_2023_Kanban.DataLayer/Entities/Card.cs | 8 ++++---- NG_2023_Kanban.DataLayer/Entities/Column.cs | 4 ++-- NG_2023_Kanban.DataLayer/Entities/Comment.cs | 4 ++-- NG_2023_Kanban.DataLayer/Entities/Role.cs | 2 +- NG_2023_Kanban.DataLayer/Entities/User.cs | 10 +++++----- .../EntityConfiguration/UserConfiguration.cs | 8 -------- .../Repositories/BaseRepository.cs | 2 +- .../Repositories/BoardRepository.cs | 13 +++++++++++++ .../Repositories/CardRepository.cs | 11 +++++++++++ .../Repositories/ColumnRepository.cs | 11 +++++++++++ .../Repositories/RoleRepository.cs | 11 +++++++++++ .../Repositories/UserRepository.cs | 19 +++++++++++++++++++ NG_2023_Kanban/DTOs/BoardDto.cs | 4 ++-- NG_2023_Kanban/DTOs/CardDto.cs | 8 ++++---- NG_2023_Kanban/DTOs/ColumnDto.cs | 4 ++-- NG_2023_Kanban/DTOs/CommentDto.cs | 4 ++-- NG_2023_Kanban/DTOs/RoleDto.cs | 2 +- NG_2023_Kanban/DTOs/UserDto.cs | 10 +++++----- 25 files changed, 114 insertions(+), 57 deletions(-) diff --git a/NG_2023_Kanban.BusinessLayer/Models/BoardModel.cs b/NG_2023_Kanban.BusinessLayer/Models/BoardModel.cs index 6fbf216..2b4fba2 100644 --- a/NG_2023_Kanban.BusinessLayer/Models/BoardModel.cs +++ b/NG_2023_Kanban.BusinessLayer/Models/BoardModel.cs @@ -4,8 +4,8 @@ { public string Name { get; set; } - public virtual ICollection? Users { get; set; } = new HashSet(); + public ICollection Users { get; set; } = new List(); - public virtual ICollection? Columns { get; set; } = new HashSet(); + public ICollection Columns { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.BusinessLayer/Models/CardModel.cs b/NG_2023_Kanban.BusinessLayer/Models/CardModel.cs index 36a5575..1acdaec 100644 --- a/NG_2023_Kanban.BusinessLayer/Models/CardModel.cs +++ b/NG_2023_Kanban.BusinessLayer/Models/CardModel.cs @@ -8,10 +8,10 @@ public int ColumnId { get; set; } public int SenderId { get; set; } - public virtual UserModel Assigned { get; set; } - public virtual ColumnModel Column { get; set; } - public virtual UserModel Sender { get; set; } + public UserModel Assigned { get; set; } + public ColumnModel Column { get; set; } + public UserModel Sender { get; set; } - public virtual ICollection Comments { get; set; } = new HashSet(); + public ICollection Comments { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.BusinessLayer/Models/ColumnModel.cs b/NG_2023_Kanban.BusinessLayer/Models/ColumnModel.cs index 600bed5..518a009 100644 --- a/NG_2023_Kanban.BusinessLayer/Models/ColumnModel.cs +++ b/NG_2023_Kanban.BusinessLayer/Models/ColumnModel.cs @@ -5,8 +5,8 @@ public string Name { get; set; } public int BoardId { get; set; } - public virtual BoardModel Board { get; set; } + public BoardModel Board { get; set; } - public virtual ICollection? Cards { get; set; } = new HashSet(); + public ICollection Cards { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.BusinessLayer/Models/CommentModel.cs b/NG_2023_Kanban.BusinessLayer/Models/CommentModel.cs index a1e19b2..5d259d5 100644 --- a/NG_2023_Kanban.BusinessLayer/Models/CommentModel.cs +++ b/NG_2023_Kanban.BusinessLayer/Models/CommentModel.cs @@ -6,7 +6,7 @@ public int SenderId { get; set; } public int CardId { get; set; } - public virtual UserModel? Sender { get; set; } - public virtual CardModel? Card { get; set; } + public UserModel Sender { get; set; } + public CardModel Card { get; set; } } } diff --git a/NG_2023_Kanban.BusinessLayer/Models/RoleModel.cs b/NG_2023_Kanban.BusinessLayer/Models/RoleModel.cs index d63d5d8..19a918a 100644 --- a/NG_2023_Kanban.BusinessLayer/Models/RoleModel.cs +++ b/NG_2023_Kanban.BusinessLayer/Models/RoleModel.cs @@ -4,6 +4,6 @@ { public string Name { get; set; } - public virtual ICollection? Members { get; set; } = new HashSet(); + public ICollection Members { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.BusinessLayer/Models/UserModel.cs b/NG_2023_Kanban.BusinessLayer/Models/UserModel.cs index fcf7847..3955020 100644 --- a/NG_2023_Kanban.BusinessLayer/Models/UserModel.cs +++ b/NG_2023_Kanban.BusinessLayer/Models/UserModel.cs @@ -7,10 +7,10 @@ namespace NG_2023_Kanban.BusinessLayer.Models public string Username { get; set; } public string Password { get; set; } - public virtual ICollection? Boards { get; set; } = new HashSet(); - public virtual ICollection? CardsAssigned { get; set; } = new HashSet(); - public virtual ICollection? CardsSent { get; set; } = new HashSet(); - public virtual ICollection? Comments { get; set; } = new HashSet(); - public virtual ICollection? Roles { get; set; } = new HashSet(); + public ICollection Boards { get; set; } = new List(); + public ICollection CardsAssigned { get; set; } = new List(); + public ICollection CardsSent { get; set; } = new List(); + public ICollection Comments { get; set; } = new List(); + public ICollection Roles { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.DataLayer/Entities/Board.cs b/NG_2023_Kanban.DataLayer/Entities/Board.cs index 4631ee0..6da6b89 100644 --- a/NG_2023_Kanban.DataLayer/Entities/Board.cs +++ b/NG_2023_Kanban.DataLayer/Entities/Board.cs @@ -4,8 +4,8 @@ { public string Name { get; set; } - public virtual ICollection? Users { get; set; } = new HashSet(); + public ICollection Users { get; set; } = new List(); - public virtual ICollection? Columns { get; set; } = new HashSet(); + public ICollection Columns { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.DataLayer/Entities/Card.cs b/NG_2023_Kanban.DataLayer/Entities/Card.cs index d61df48..e2032dd 100644 --- a/NG_2023_Kanban.DataLayer/Entities/Card.cs +++ b/NG_2023_Kanban.DataLayer/Entities/Card.cs @@ -8,10 +8,10 @@ public int ColumnId { get; set; } public int SenderId { get; set; } - public virtual User Assigned { get; set; } - public virtual Column Column { get; set; } - public virtual User Sender { get; set; } + public User Assigned { get; set; } + public Column Column { get; set; } + public User Sender { get; set; } - public virtual ICollection Comments { get; set; } = new HashSet(); + public ICollection Comments { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.DataLayer/Entities/Column.cs b/NG_2023_Kanban.DataLayer/Entities/Column.cs index 2c58462..60bd835 100644 --- a/NG_2023_Kanban.DataLayer/Entities/Column.cs +++ b/NG_2023_Kanban.DataLayer/Entities/Column.cs @@ -5,8 +5,8 @@ public string Name { get; set; } public int BoardId { get; set; } - public virtual Board Board { get; set; } + public Board Board { get; set; } - public virtual ICollection? Cards { get; set; } = new HashSet(); + public ICollection Cards { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.DataLayer/Entities/Comment.cs b/NG_2023_Kanban.DataLayer/Entities/Comment.cs index 1446506..0db61a5 100644 --- a/NG_2023_Kanban.DataLayer/Entities/Comment.cs +++ b/NG_2023_Kanban.DataLayer/Entities/Comment.cs @@ -6,7 +6,7 @@ public int SenderId { get; set; } public int CardId { get; set; } - public virtual User? Sender { get; set; } - public virtual Card? Card { get; set; } + public User Sender { get; set; } + public Card Card { get; set; } } } diff --git a/NG_2023_Kanban.DataLayer/Entities/Role.cs b/NG_2023_Kanban.DataLayer/Entities/Role.cs index 19b9818..b3aa5c8 100644 --- a/NG_2023_Kanban.DataLayer/Entities/Role.cs +++ b/NG_2023_Kanban.DataLayer/Entities/Role.cs @@ -4,6 +4,6 @@ { public string Name { get; set; } - public virtual ICollection? Members { get; set; } = new HashSet(); + public virtual ICollection Members { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.DataLayer/Entities/User.cs b/NG_2023_Kanban.DataLayer/Entities/User.cs index 16fe1c9..5d4480e 100644 --- a/NG_2023_Kanban.DataLayer/Entities/User.cs +++ b/NG_2023_Kanban.DataLayer/Entities/User.cs @@ -7,10 +7,10 @@ namespace NG_2023_Kanban.DataLayer.Entities public string Username { get; set; } public string Password { get; set; } - public virtual ICollection? Boards { get; set; } = new HashSet(); - public virtual ICollection? CardsAssigned { get; set; } = new HashSet(); - public virtual ICollection? Comments { get; set; } = new HashSet(); - public virtual ICollection? CardsSent { get; set; } = new HashSet(); - public virtual ICollection? Roles { get; set; } = new HashSet(); + public ICollection Boards { get; set; } = new List(); + public ICollection CardsAssigned { get; set; } = new List(); + public ICollection Comments { get; set; } = new List(); + public ICollection CardsSent { get; set; } = new List(); + public ICollection Roles { get; set; } = new List(); } } diff --git a/NG_2023_Kanban.DataLayer/EntityConfiguration/UserConfiguration.cs b/NG_2023_Kanban.DataLayer/EntityConfiguration/UserConfiguration.cs index 631d70f..8b31139 100644 --- a/NG_2023_Kanban.DataLayer/EntityConfiguration/UserConfiguration.cs +++ b/NG_2023_Kanban.DataLayer/EntityConfiguration/UserConfiguration.cs @@ -17,14 +17,6 @@ namespace NG_2023_Kanban.DataLayer.EntityConfiguration builder.HasIndex(x => x.Username).IsUnique(true); builder.Property(x => x.Username).IsRequired(); builder.Property(x => x.Password).IsRequired(); - - builder - .HasMany(x => x.Boards) - .WithMany(x => x.Users); - - builder - .HasMany(x => x.Roles) - .WithMany(x => x.Members); } } } diff --git a/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs index 0bd0363..e576a88 100644 --- a/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs +++ b/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs @@ -8,7 +8,7 @@ namespace NG_2023_Kanban.DataLayer.Repositories; public class BaseRepository : IRepository where T : BaseEntity { - private DatabaseContext _context; + protected DatabaseContext _context; public BaseRepository(DatabaseContext context) => _context = context; diff --git a/NG_2023_Kanban.DataLayer/Repositories/BoardRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/BoardRepository.cs index c32bea0..88042ab 100644 --- a/NG_2023_Kanban.DataLayer/Repositories/BoardRepository.cs +++ b/NG_2023_Kanban.DataLayer/Repositories/BoardRepository.cs @@ -1,10 +1,23 @@ using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Interfaces; +using Microsoft.EntityFrameworkCore; namespace NG_2023_Kanban.DataLayer.Repositories; public class BoardRepository : BaseRepository, IBoardRepository { public BoardRepository(DatabaseContext context) : base(context) { } + + public async Task> GetAllAsync() + => await _context.Set().Select(x => x). + Include(x => x.Users). + Include(x => x.Columns). + ToListAsync(); + + public async Task GetAsync(int id) + => await _context.Set(). + Include(x => x.Users). + Include(x => x.Columns). + FirstAsync(x => x.Id == id); } diff --git a/NG_2023_Kanban.DataLayer/Repositories/CardRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/CardRepository.cs index 9011ad0..6d44d62 100644 --- a/NG_2023_Kanban.DataLayer/Repositories/CardRepository.cs +++ b/NG_2023_Kanban.DataLayer/Repositories/CardRepository.cs @@ -1,10 +1,21 @@ using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Interfaces; +using Microsoft.EntityFrameworkCore; namespace NG_2023_Kanban.DataLayer.Repositories; public class CardRepository : BaseRepository, ICardRepository { public CardRepository(DatabaseContext context) : base(context) { } + + public async Task> GetAllAsync() + => await _context.Set().Select(x => x). + Include(x => x.Comments). + ToListAsync(); + + public async Task GetAsync(int id) + => await _context.Set(). + Include(x => x.Comments). + FirstAsync(x => x.Id == id); } diff --git a/NG_2023_Kanban.DataLayer/Repositories/ColumnRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/ColumnRepository.cs index 249ed05..24f4500 100644 --- a/NG_2023_Kanban.DataLayer/Repositories/ColumnRepository.cs +++ b/NG_2023_Kanban.DataLayer/Repositories/ColumnRepository.cs @@ -1,10 +1,21 @@ using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Interfaces; +using Microsoft.EntityFrameworkCore; namespace NG_2023_Kanban.DataLayer.Repositories; public class ColumnRepository : BaseRepository, IColumnRepository { public ColumnRepository(DatabaseContext context) : base(context) { } + + public async Task> GetAllAsync() + => await _context.Set().Select(x => x). + Include(x => x.Cards). + ToListAsync(); + + public async Task GetAsync(int id) + => await _context.Set(). + Include(x => x.Cards). + FirstAsync(x => x.Id == id); } diff --git a/NG_2023_Kanban.DataLayer/Repositories/RoleRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/RoleRepository.cs index bd3db72..a6103fe 100644 --- a/NG_2023_Kanban.DataLayer/Repositories/RoleRepository.cs +++ b/NG_2023_Kanban.DataLayer/Repositories/RoleRepository.cs @@ -1,10 +1,21 @@ using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Interfaces; +using Microsoft.EntityFrameworkCore; namespace NG_2023_Kanban.DataLayer.Repositories; public class RoleRepository : BaseRepository, IRoleRepository { public RoleRepository(DatabaseContext context) : base(context) { } + + public async Task> GetAllAsync() + => await _context.Set().Select(x => x). + Include(x => x.Members). + ToListAsync(); + + public async Task GetAsync(int id) + => await _context.Set(). + Include(x => x.Members). + FirstAsync(x => x.Id == id); } diff --git a/NG_2023_Kanban.DataLayer/Repositories/UserRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/UserRepository.cs index 9188896..0cf7ba3 100644 --- a/NG_2023_Kanban.DataLayer/Repositories/UserRepository.cs +++ b/NG_2023_Kanban.DataLayer/Repositories/UserRepository.cs @@ -1,10 +1,29 @@ using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Interfaces; +using Microsoft.EntityFrameworkCore; namespace NG_2023_Kanban.DataLayer.Repositories; public class UserRepository : BaseRepository, IUserRepository { public UserRepository(DatabaseContext context) : base(context) { } + + public async Task> GetAllAsync() + => await _context.Set().Select(x => x). + Include(x => x.Boards). + Include(x => x.CardsAssigned). + Include(x => x.CardsSent). + Include(x => x.Comments). + Include(x => x.Roles). + ToListAsync(); + + public async Task GetAsync(int id) + => await _context.Set(). + Include(x => x.Boards). + Include(x => x.CardsAssigned). + Include(x => x.CardsSent). + Include(x => x.Comments). + Include(x => x.Roles). + FirstAsync(x => x.Id == id); } diff --git a/NG_2023_Kanban/DTOs/BoardDto.cs b/NG_2023_Kanban/DTOs/BoardDto.cs index 1f2ecdb..2fecb8c 100644 --- a/NG_2023_Kanban/DTOs/BoardDto.cs +++ b/NG_2023_Kanban/DTOs/BoardDto.cs @@ -4,8 +4,8 @@ { public string Name { get; set; } - public virtual ICollection? Users { get; set; } = new HashSet(); + public ICollection Users { get; set; } = new List(); - public virtual ICollection? Columns { get; set; } = new HashSet(); + public ICollection Columns { get; set; } = new List(); } } diff --git a/NG_2023_Kanban/DTOs/CardDto.cs b/NG_2023_Kanban/DTOs/CardDto.cs index 96a784f..418541f 100644 --- a/NG_2023_Kanban/DTOs/CardDto.cs +++ b/NG_2023_Kanban/DTOs/CardDto.cs @@ -8,10 +8,10 @@ public int ColumnId { get; set; } public int SenderId { get; set; } - public virtual UserDto Assigned { get; set; } - public virtual ColumnDto Column { get; set; } - public virtual UserDto Sender { get; set; } + public UserDto Assigned { get; set; } + public ColumnDto Column { get; set; } + public UserDto Sender { get; set; } - public virtual ICollection Comments { get; set; } = new HashSet(); + public ICollection Comments { get; set; } = new List(); } } diff --git a/NG_2023_Kanban/DTOs/ColumnDto.cs b/NG_2023_Kanban/DTOs/ColumnDto.cs index 3907f3f..a5ec782 100644 --- a/NG_2023_Kanban/DTOs/ColumnDto.cs +++ b/NG_2023_Kanban/DTOs/ColumnDto.cs @@ -5,8 +5,8 @@ public string Name { get; set; } public int BoardId { get; set; } - public virtual BoardDto Board { get; set; } + public BoardDto Board { get; set; } - public virtual ICollection? Cards { get; set; } = new HashSet(); + public ICollection Cards { get; set; } = new List(); } } diff --git a/NG_2023_Kanban/DTOs/CommentDto.cs b/NG_2023_Kanban/DTOs/CommentDto.cs index 0313367..c382cc8 100644 --- a/NG_2023_Kanban/DTOs/CommentDto.cs +++ b/NG_2023_Kanban/DTOs/CommentDto.cs @@ -6,7 +6,7 @@ public int SenderId { get; set; } public int CardId { get; set; } - public virtual UserDto? Sender { get; set; } - public virtual CardDto? Card { get; set; } + public UserDto Sender { get; set; } + public CardDto Card { get; set; } } } diff --git a/NG_2023_Kanban/DTOs/RoleDto.cs b/NG_2023_Kanban/DTOs/RoleDto.cs index eb20f4a..831bbfe 100644 --- a/NG_2023_Kanban/DTOs/RoleDto.cs +++ b/NG_2023_Kanban/DTOs/RoleDto.cs @@ -4,6 +4,6 @@ { public string Name { get; set; } - public virtual ICollection? Members { get; set; } = new HashSet(); + public virtual ICollection Members { get; set; } = new List(); } } diff --git a/NG_2023_Kanban/DTOs/UserDto.cs b/NG_2023_Kanban/DTOs/UserDto.cs index 3c816f1..cd937e4 100644 --- a/NG_2023_Kanban/DTOs/UserDto.cs +++ b/NG_2023_Kanban/DTOs/UserDto.cs @@ -7,10 +7,10 @@ namespace NG_2023_Kanban.DTOs public string Username { get; set; } public string Password { get; set; } - public virtual ICollection? Boards { get; set; } = new HashSet(); - public virtual ICollection? CardsAssigned { get; set; } = new HashSet(); - public virtual ICollection? CardsSent { get; set; } = new HashSet(); - public virtual ICollection? Comments { get; set; } = new HashSet(); - public virtual ICollection? Roles { get; set; } = new HashSet(); + public ICollection Boards { get; set; } = new List(); + public ICollection CardsAssigned { get; set; } = new List(); + public ICollection CardsSent { get; set; } = new List(); + public ICollection Comments { get; set; } = new List(); + public ICollection Roles { get; set; } = new List(); } }