Fix many-to-many relationships

This commit is contained in:
Stanislav Mykhailenko 2023-07-23 17:46:06 +03:00
parent 11ac997ca7
commit e00cb01846
GPG key ID: 1E95E66A9C9D6A36
25 changed files with 114 additions and 57 deletions

View file

@ -4,8 +4,8 @@
{ {
public string Name { get; set; } public string Name { get; set; }
public virtual ICollection<UserModel>? Users { get; set; } = new HashSet<UserModel>(); public ICollection<UserModel> Users { get; set; } = new List<UserModel>();
public virtual ICollection<ColumnModel>? Columns { get; set; } = new HashSet<ColumnModel>(); public ICollection<ColumnModel> Columns { get; set; } = new List<ColumnModel>();
} }
} }

View file

@ -8,10 +8,10 @@
public int ColumnId { get; set; } public int ColumnId { get; set; }
public int SenderId { get; set; } public int SenderId { get; set; }
public virtual UserModel Assigned { get; set; } public UserModel Assigned { get; set; }
public virtual ColumnModel Column { get; set; } public ColumnModel Column { get; set; }
public virtual UserModel Sender { get; set; } public UserModel Sender { get; set; }
public virtual ICollection<CommentModel> Comments { get; set; } = new HashSet<CommentModel>(); public ICollection<CommentModel> Comments { get; set; } = new List<CommentModel>();
} }
} }

View file

@ -5,8 +5,8 @@
public string Name { get; set; } public string Name { get; set; }
public int BoardId { get; set; } public int BoardId { get; set; }
public virtual BoardModel Board { get; set; } public BoardModel Board { get; set; }
public virtual ICollection<CardModel>? Cards { get; set; } = new HashSet<CardModel>(); public ICollection<CardModel> Cards { get; set; } = new List<CardModel>();
} }
} }

View file

@ -6,7 +6,7 @@
public int SenderId { get; set; } public int SenderId { get; set; }
public int CardId { get; set; } public int CardId { get; set; }
public virtual UserModel? Sender { get; set; } public UserModel Sender { get; set; }
public virtual CardModel? Card { get; set; } public CardModel Card { get; set; }
} }
} }

View file

@ -4,6 +4,6 @@
{ {
public string Name { get; set; } public string Name { get; set; }
public virtual ICollection<UserModel>? Members { get; set; } = new HashSet<UserModel>(); public ICollection<UserModel> Members { get; set; } = new List<UserModel>();
} }
} }

View file

@ -7,10 +7,10 @@ namespace NG_2023_Kanban.BusinessLayer.Models
public string Username { get; set; } public string Username { get; set; }
public string Password { get; set; } public string Password { get; set; }
public virtual ICollection<BoardModel>? Boards { get; set; } = new HashSet<BoardModel>(); public ICollection<BoardModel> Boards { get; set; } = new List<BoardModel>();
public virtual ICollection<CardModel>? CardsAssigned { get; set; } = new HashSet<CardModel>(); public ICollection<CardModel> CardsAssigned { get; set; } = new List<CardModel>();
public virtual ICollection<CardModel>? CardsSent { get; set; } = new HashSet<CardModel>(); public ICollection<CardModel> CardsSent { get; set; } = new List<CardModel>();
public virtual ICollection<CommentModel>? Comments { get; set; } = new HashSet<CommentModel>(); public ICollection<CommentModel> Comments { get; set; } = new List<CommentModel>();
public virtual ICollection<RoleModel>? Roles { get; set; } = new HashSet<RoleModel>(); public ICollection<RoleModel> Roles { get; set; } = new List<RoleModel>();
} }
} }

View file

@ -4,8 +4,8 @@
{ {
public string Name { get; set; } public string Name { get; set; }
public virtual ICollection<User>? Users { get; set; } = new HashSet<User>(); public ICollection<User> Users { get; set; } = new List<User>();
public virtual ICollection<Column>? Columns { get; set; } = new HashSet<Column>(); public ICollection<Column> Columns { get; set; } = new List<Column>();
} }
} }

View file

@ -8,10 +8,10 @@
public int ColumnId { get; set; } public int ColumnId { get; set; }
public int SenderId { get; set; } public int SenderId { get; set; }
public virtual User Assigned { get; set; } public User Assigned { get; set; }
public virtual Column Column { get; set; } public Column Column { get; set; }
public virtual User Sender { get; set; } public User Sender { get; set; }
public virtual ICollection<Comment> Comments { get; set; } = new HashSet<Comment>(); public ICollection<Comment> Comments { get; set; } = new List<Comment>();
} }
} }

View file

@ -5,8 +5,8 @@
public string Name { get; set; } public string Name { get; set; }
public int BoardId { get; set; } public int BoardId { get; set; }
public virtual Board Board { get; set; } public Board Board { get; set; }
public virtual ICollection<Card>? Cards { get; set; } = new HashSet<Card>(); public ICollection<Card> Cards { get; set; } = new List<Card>();
} }
} }

View file

@ -6,7 +6,7 @@
public int SenderId { get; set; } public int SenderId { get; set; }
public int CardId { get; set; } public int CardId { get; set; }
public virtual User? Sender { get; set; } public User Sender { get; set; }
public virtual Card? Card { get; set; } public Card Card { get; set; }
} }
} }

View file

@ -4,6 +4,6 @@
{ {
public string Name { get; set; } public string Name { get; set; }
public virtual ICollection<User>? Members { get; set; } = new HashSet<User>(); public virtual ICollection<User> Members { get; set; } = new List<User>();
} }
} }

View file

@ -7,10 +7,10 @@ namespace NG_2023_Kanban.DataLayer.Entities
public string Username { get; set; } public string Username { get; set; }
public string Password { get; set; } public string Password { get; set; }
public virtual ICollection<Board>? Boards { get; set; } = new HashSet<Board>(); public ICollection<Board> Boards { get; set; } = new List<Board>();
public virtual ICollection<Card>? CardsAssigned { get; set; } = new HashSet<Card>(); public ICollection<Card> CardsAssigned { get; set; } = new List<Card>();
public virtual ICollection<Comment>? Comments { get; set; } = new HashSet<Comment>(); public ICollection<Comment> Comments { get; set; } = new List<Comment>();
public virtual ICollection<Card>? CardsSent { get; set; } = new HashSet<Card>(); public ICollection<Card> CardsSent { get; set; } = new List<Card>();
public virtual ICollection<Role>? Roles { get; set; } = new HashSet<Role>(); public ICollection<Role> Roles { get; set; } = new List<Role>();
} }
} }

View file

@ -17,14 +17,6 @@ namespace NG_2023_Kanban.DataLayer.EntityConfiguration
builder.HasIndex(x => x.Username).IsUnique(true); builder.HasIndex(x => x.Username).IsUnique(true);
builder.Property(x => x.Username).IsRequired(); builder.Property(x => x.Username).IsRequired();
builder.Property(x => x.Password).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);
} }
} }
} }

View file

@ -8,7 +8,7 @@ namespace NG_2023_Kanban.DataLayer.Repositories;
public class BaseRepository<T> : IRepository<T> where T : BaseEntity public class BaseRepository<T> : IRepository<T> where T : BaseEntity
{ {
private DatabaseContext _context; protected DatabaseContext _context;
public BaseRepository(DatabaseContext context) public BaseRepository(DatabaseContext context)
=> _context = context; => _context = context;

View file

@ -1,10 +1,23 @@
using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.DbStartup;
using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Entities;
using NG_2023_Kanban.DataLayer.Interfaces; using NG_2023_Kanban.DataLayer.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace NG_2023_Kanban.DataLayer.Repositories; namespace NG_2023_Kanban.DataLayer.Repositories;
public class BoardRepository : BaseRepository<Board>, IBoardRepository public class BoardRepository : BaseRepository<Board>, IBoardRepository
{ {
public BoardRepository(DatabaseContext context) : base(context) { } public BoardRepository(DatabaseContext context) : base(context) { }
public async Task<ICollection<Board>> GetAllAsync()
=> await _context.Set<Board>().Select(x => x).
Include(x => x.Users).
Include(x => x.Columns).
ToListAsync();
public async Task<Board> GetAsync(int id)
=> await _context.Set<Board>().
Include(x => x.Users).
Include(x => x.Columns).
FirstAsync(x => x.Id == id);
} }

View file

@ -1,10 +1,21 @@
using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.DbStartup;
using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Entities;
using NG_2023_Kanban.DataLayer.Interfaces; using NG_2023_Kanban.DataLayer.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace NG_2023_Kanban.DataLayer.Repositories; namespace NG_2023_Kanban.DataLayer.Repositories;
public class CardRepository : BaseRepository<Card>, ICardRepository public class CardRepository : BaseRepository<Card>, ICardRepository
{ {
public CardRepository(DatabaseContext context) : base(context) { } public CardRepository(DatabaseContext context) : base(context) { }
public async Task<ICollection<Card>> GetAllAsync()
=> await _context.Set<Card>().Select(x => x).
Include(x => x.Comments).
ToListAsync();
public async Task<Card> GetAsync(int id)
=> await _context.Set<Card>().
Include(x => x.Comments).
FirstAsync(x => x.Id == id);
} }

View file

@ -1,10 +1,21 @@
using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.DbStartup;
using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Entities;
using NG_2023_Kanban.DataLayer.Interfaces; using NG_2023_Kanban.DataLayer.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace NG_2023_Kanban.DataLayer.Repositories; namespace NG_2023_Kanban.DataLayer.Repositories;
public class ColumnRepository : BaseRepository<Column>, IColumnRepository public class ColumnRepository : BaseRepository<Column>, IColumnRepository
{ {
public ColumnRepository(DatabaseContext context) : base(context) { } public ColumnRepository(DatabaseContext context) : base(context) { }
public async Task<ICollection<Column>> GetAllAsync()
=> await _context.Set<Column>().Select(x => x).
Include(x => x.Cards).
ToListAsync();
public async Task<Column> GetAsync(int id)
=> await _context.Set<Column>().
Include(x => x.Cards).
FirstAsync(x => x.Id == id);
} }

View file

@ -1,10 +1,21 @@
using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.DbStartup;
using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Entities;
using NG_2023_Kanban.DataLayer.Interfaces; using NG_2023_Kanban.DataLayer.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace NG_2023_Kanban.DataLayer.Repositories; namespace NG_2023_Kanban.DataLayer.Repositories;
public class RoleRepository : BaseRepository<Role>, IRoleRepository public class RoleRepository : BaseRepository<Role>, IRoleRepository
{ {
public RoleRepository(DatabaseContext context) : base(context) { } public RoleRepository(DatabaseContext context) : base(context) { }
public async Task<ICollection<Role>> GetAllAsync()
=> await _context.Set<Role>().Select(x => x).
Include(x => x.Members).
ToListAsync();
public async Task<Role> GetAsync(int id)
=> await _context.Set<Role>().
Include(x => x.Members).
FirstAsync(x => x.Id == id);
} }

View file

@ -1,10 +1,29 @@
using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.DbStartup;
using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Entities;
using NG_2023_Kanban.DataLayer.Interfaces; using NG_2023_Kanban.DataLayer.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace NG_2023_Kanban.DataLayer.Repositories; namespace NG_2023_Kanban.DataLayer.Repositories;
public class UserRepository : BaseRepository<User>, IUserRepository public class UserRepository : BaseRepository<User>, IUserRepository
{ {
public UserRepository(DatabaseContext context) : base(context) { } public UserRepository(DatabaseContext context) : base(context) { }
public async Task<ICollection<User>> GetAllAsync()
=> await _context.Set<User>().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<User> GetAsync(int id)
=> await _context.Set<User>().
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);
} }

View file

@ -4,8 +4,8 @@
{ {
public string Name { get; set; } public string Name { get; set; }
public virtual ICollection<UserDto>? Users { get; set; } = new HashSet<UserDto>(); public ICollection<UserDto> Users { get; set; } = new List<UserDto>();
public virtual ICollection<ColumnDto>? Columns { get; set; } = new HashSet<ColumnDto>(); public ICollection<ColumnDto> Columns { get; set; } = new List<ColumnDto>();
} }
} }

View file

@ -8,10 +8,10 @@
public int ColumnId { get; set; } public int ColumnId { get; set; }
public int SenderId { get; set; } public int SenderId { get; set; }
public virtual UserDto Assigned { get; set; } public UserDto Assigned { get; set; }
public virtual ColumnDto Column { get; set; } public ColumnDto Column { get; set; }
public virtual UserDto Sender { get; set; } public UserDto Sender { get; set; }
public virtual ICollection<CommentDto> Comments { get; set; } = new HashSet<CommentDto>(); public ICollection<CommentDto> Comments { get; set; } = new List<CommentDto>();
} }
} }

View file

@ -5,8 +5,8 @@
public string Name { get; set; } public string Name { get; set; }
public int BoardId { get; set; } public int BoardId { get; set; }
public virtual BoardDto Board { get; set; } public BoardDto Board { get; set; }
public virtual ICollection<CardDto>? Cards { get; set; } = new HashSet<CardDto>(); public ICollection<CardDto> Cards { get; set; } = new List<CardDto>();
} }
} }

View file

@ -6,7 +6,7 @@
public int SenderId { get; set; } public int SenderId { get; set; }
public int CardId { get; set; } public int CardId { get; set; }
public virtual UserDto? Sender { get; set; } public UserDto Sender { get; set; }
public virtual CardDto? Card { get; set; } public CardDto Card { get; set; }
} }
} }

View file

@ -4,6 +4,6 @@
{ {
public string Name { get; set; } public string Name { get; set; }
public virtual ICollection<UserDto>? Members { get; set; } = new HashSet<UserDto>(); public virtual ICollection<UserDto> Members { get; set; } = new List<UserDto>();
} }
} }

View file

@ -7,10 +7,10 @@ namespace NG_2023_Kanban.DTOs
public string Username { get; set; } public string Username { get; set; }
public string Password { get; set; } public string Password { get; set; }
public virtual ICollection<BoardDto>? Boards { get; set; } = new HashSet<BoardDto>(); public ICollection<BoardDto> Boards { get; set; } = new List<BoardDto>();
public virtual ICollection<CardDto>? CardsAssigned { get; set; } = new HashSet<CardDto>(); public ICollection<CardDto> CardsAssigned { get; set; } = new List<CardDto>();
public virtual ICollection<CardDto>? CardsSent { get; set; } = new HashSet<CardDto>(); public ICollection<CardDto> CardsSent { get; set; } = new List<CardDto>();
public virtual ICollection<CommentDto>? Comments { get; set; } = new HashSet<CommentDto>(); public ICollection<CommentDto> Comments { get; set; } = new List<CommentDto>();
public virtual ICollection<RoleDto>? Roles { get; set; } = new HashSet<RoleDto>(); public ICollection<RoleDto> Roles { get; set; } = new List<RoleDto>();
} }
} }