diff --git a/NG_2023_Kanban.BusinessLayer/Service/Injecting.cs b/NG_2023_Kanban.BusinessLayer/Injecting.cs similarity index 71% rename from NG_2023_Kanban.BusinessLayer/Service/Injecting.cs rename to NG_2023_Kanban.BusinessLayer/Injecting.cs index f74800f..daa2e30 100644 --- a/NG_2023_Kanban.BusinessLayer/Service/Injecting.cs +++ b/NG_2023_Kanban.BusinessLayer/Injecting.cs @@ -1,4 +1,4 @@ -using NG_2023_Kanban.BusinessLayer.Service; +using NG_2023_Kanban.BusinessLayer.Services; using Microsoft.Extensions.DependencyInjection; namespace NG_2023_Kanban.BusinessLayer.Inject @@ -8,7 +8,7 @@ namespace NG_2023_Kanban.BusinessLayer.Inject public static void InjectBLL( this IServiceCollection services) { - services.AddScoped(); + services.AddScoped(); } } } diff --git a/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs b/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs new file mode 100644 index 0000000..f40c43e --- /dev/null +++ b/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs @@ -0,0 +1,10 @@ +using NG_2023_Kanban.DataLayer.Entities; + +namespace NG_2023_Kanban.BusinessLayer.Interfaces +{ + public interface IUserService + { + Task LoginAsync(User user); + Task RegisterAsync(User user); + } +} diff --git a/NG_2023_Kanban.BusinessLayer/Service/BusinessService.cs b/NG_2023_Kanban.BusinessLayer/Service/BusinessService.cs deleted file mode 100644 index 9ee0223..0000000 --- a/NG_2023_Kanban.BusinessLayer/Service/BusinessService.cs +++ /dev/null @@ -1,33 +0,0 @@ -using NG_2023_Kanban.DataLayer.Service; -using NG_2023_Kanban.DataLayer.Entities; -using Microsoft.EntityFrameworkCore; - -namespace NG_2023_Kanban.BusinessLayer.Service -{ - public class BusinessService - { - private readonly DataService _service; - public BusinessService(DataService service) - { - _service = service; - } - - public async Task LoginAsync(string username, string password) - { - return await _service.LoginAsync(username, password); - } - - public async Task RegisterAsync(string fullName, string username, string password) - { - User account = await _service.AddAsync(new User - { - FullName = fullName, - Username = username, - Password = password, // TODO: hashing - IsAdmin = false - }); - - return account; - } - } -} diff --git a/NG_2023_Kanban.BusinessLayer/Service/UserService.cs b/NG_2023_Kanban.BusinessLayer/Service/UserService.cs new file mode 100644 index 0000000..d544b8c --- /dev/null +++ b/NG_2023_Kanban.BusinessLayer/Service/UserService.cs @@ -0,0 +1,33 @@ +using NG_2023_Kanban.BusinessLayer.Interfaces; +using NG_2023_Kanban.DataLayer.Repositories; +using NG_2023_Kanban.DataLayer.Entities; +using NG_2023_Kanban.DataLayer.Interfaces; +using Microsoft.EntityFrameworkCore; + +namespace NG_2023_Kanban.BusinessLayer.Services +{ + public class UserService : IUserService + { + private readonly IUserRepository _userRepository; + + public UserService(IUserRepository userRepository) + { + _userRepository = userRepository; + } + + public async Task LoginAsync(User user) + { + var data = await _userRepository.FindAsync(x => x.Username == user.Username && x.Password == user.Password); + if (data.Any()) + return data.First(); + else + return null; + } + + public async Task RegisterAsync(User user) + { + await _userRepository.CreateAsync(user); + return user; + } + } +} diff --git a/NG_2023_Kanban.DataLayer/DbStartup/DatabaseContext.cs b/NG_2023_Kanban.DataLayer/DbStartup/DatabaseContext.cs index 2739ea7..7886c58 100644 --- a/NG_2023_Kanban.DataLayer/DbStartup/DatabaseContext.cs +++ b/NG_2023_Kanban.DataLayer/DbStartup/DatabaseContext.cs @@ -6,18 +6,20 @@ namespace NG_2023_Kanban.DataLayer.DbStartup { public class DatabaseContext : DbContext { - public DatabaseContext(DbContextOptions options) : base(options) { } + public DatabaseContext(DbContextOptions options) : base(options) { } public DbSet Boards { get; set; } - public DbSet Comments { get; set; } public DbSet Cards { get; set; } + public DbSet Columns { get; set; } + public DbSet Comments { get; set; } public DbSet Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(new BoardConfiguration()); - modelBuilder.ApplyConfiguration(new CommentConfiguration()); modelBuilder.ApplyConfiguration(new CardConfiguration()); + modelBuilder.ApplyConfiguration(new ColumnConfiguration()); + modelBuilder.ApplyConfiguration(new CommentConfiguration()); modelBuilder.ApplyConfiguration(new UserConfiguration()); base.OnModelCreating(modelBuilder); diff --git a/NG_2023_Kanban.DataLayer/DbStartup/Injecting.cs b/NG_2023_Kanban.DataLayer/DbStartup/Injecting.cs index 1c3d8fc..78e2469 100644 --- a/NG_2023_Kanban.DataLayer/DbStartup/Injecting.cs +++ b/NG_2023_Kanban.DataLayer/DbStartup/Injecting.cs @@ -1,4 +1,6 @@ using NG_2023_Kanban.DataLayer.Service; +using NG_2023_Kanban.DataLayer.Repositories; +using NG_2023_Kanban.DataLayer.Interfaces; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -11,12 +13,23 @@ namespace NG_2023_Kanban.DataLayer.DbStartup this IServiceCollection services, IConfiguration configuration) { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + var optionsBuilder = new DbContextOptionsBuilder(); + + optionsBuilder.UseSqlServer(configuration["ConnectionString"]); + services.AddDbContext(options => { options.UseSqlServer( configuration["ConnectionString"]); }); - services.AddScoped(); + + (new DatabaseContext(optionsBuilder.Options)).Database.EnsureCreated(); // possibly misplaced } } } diff --git a/NG_2023_Kanban.DataLayer/Entities/User.cs b/NG_2023_Kanban.DataLayer/Entities/User.cs index 8bd8cf7..b9aaf90 100644 --- a/NG_2023_Kanban.DataLayer/Entities/User.cs +++ b/NG_2023_Kanban.DataLayer/Entities/User.cs @@ -7,7 +7,7 @@ public string Username { get; set; } public string Password { get; set; } - public bool IsAdmin { get; set; } + public bool IsAdmin { get; set; } = false; public virtual ICollection? Boards { get; set; } = new HashSet(); public virtual ICollection? Cards { get; set; } = new HashSet(); diff --git a/NG_2023_Kanban.DataLayer/EntityConfiguration/ColumnConfiguration.cs b/NG_2023_Kanban.DataLayer/EntityConfiguration/ColumnConfiguration.cs index a964ce6..7294c78 100644 --- a/NG_2023_Kanban.DataLayer/EntityConfiguration/ColumnConfiguration.cs +++ b/NG_2023_Kanban.DataLayer/EntityConfiguration/ColumnConfiguration.cs @@ -14,9 +14,6 @@ namespace NG_2023_Kanban.DataLayer.EntityConfiguration builder.Property(x => x.Name).IsRequired(); builder.Property(x => x.Name).HasMaxLength(100); - builder.Property(x => x.Board).IsRequired(); - builder.Property(x => x.Board).HasMaxLength(100); - builder .HasOne(x => x.Board) .WithMany(x => x.Columns) diff --git a/NG_2023_Kanban.DataLayer/Interfaces/IBoardRepository.cs b/NG_2023_Kanban.DataLayer/Interfaces/IBoardRepository.cs new file mode 100644 index 0000000..ecb6ee4 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Interfaces/IBoardRepository.cs @@ -0,0 +1,8 @@ +using NG_2023_Kanban.DataLayer.Entities; + +namespace NG_2023_Kanban.DataLayer.Interfaces +{ + public interface IBoardRepository : IRepository + { + } +} diff --git a/NG_2023_Kanban.DataLayer/Interfaces/ICardRepository.cs b/NG_2023_Kanban.DataLayer/Interfaces/ICardRepository.cs new file mode 100644 index 0000000..9ec467b --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Interfaces/ICardRepository.cs @@ -0,0 +1,8 @@ +using NG_2023_Kanban.DataLayer.Entities; + +namespace NG_2023_Kanban.DataLayer.Interfaces +{ + public interface ICardRepository : IRepository + { + } +} diff --git a/NG_2023_Kanban.DataLayer/Interfaces/IColumnRepository.cs b/NG_2023_Kanban.DataLayer/Interfaces/IColumnRepository.cs new file mode 100644 index 0000000..0762298 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Interfaces/IColumnRepository.cs @@ -0,0 +1,8 @@ +using NG_2023_Kanban.DataLayer.Entities; + +namespace NG_2023_Kanban.DataLayer.Interfaces +{ + public interface IColumnRepository : IRepository + { + } +} diff --git a/NG_2023_Kanban.DataLayer/Interfaces/ICommentRepository.cs b/NG_2023_Kanban.DataLayer/Interfaces/ICommentRepository.cs new file mode 100644 index 0000000..db59bb6 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Interfaces/ICommentRepository.cs @@ -0,0 +1,8 @@ +using NG_2023_Kanban.DataLayer.Entities; + +namespace NG_2023_Kanban.DataLayer.Interfaces +{ + public interface ICommentRepository : IRepository + { + } +} diff --git a/NG_2023_Kanban.DataLayer/Interfaces/IRepository.cs b/NG_2023_Kanban.DataLayer/Interfaces/IRepository.cs new file mode 100644 index 0000000..281f955 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Interfaces/IRepository.cs @@ -0,0 +1,13 @@ +namespace NG_2023_Kanban.DataLayer.Interfaces +{ + public interface IRepository where T : class + { + Task> GetAllAsync(); + Task GetAsync(int id); + Task> FindAsync(Func predicate); + Task CreateAsync(T entity); + Task UpdateAsync(T entity); + Task DeleteAsync(int id); + Task DeleteAsync(T entity); + } +} diff --git a/NG_2023_Kanban.DataLayer/Interfaces/IUserRepository.cs b/NG_2023_Kanban.DataLayer/Interfaces/IUserRepository.cs new file mode 100644 index 0000000..8ba267e --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Interfaces/IUserRepository.cs @@ -0,0 +1,8 @@ +using NG_2023_Kanban.DataLayer.Entities; + +namespace NG_2023_Kanban.DataLayer.Interfaces +{ + public interface IUserRepository : IRepository + { + } +} diff --git a/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs new file mode 100644 index 0000000..f0502ae --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs @@ -0,0 +1,50 @@ +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 BaseRepository : IRepository where T : BaseEntity +{ + private DatabaseContext _context; + + public BaseRepository(DatabaseContext context) + => _context = context; + + public async Task> GetAllAsync() + => await _context.Set().Select(x => x).ToListAsync(); + + public async Task GetAsync(int id) + => await _context.Set().FirstAsync(x => x.Id == id); + + public async Task> FindAsync(Func predicate) + { + var entities = await GetAllAsync(); + return entities.Where(predicate).ToList(); + } + + public async Task CreateAsync(T entity) + { + await _context.Set().AddAsync(entity); + await _context.SaveChangesAsync(); + } + + public async Task UpdateAsync(T entity) + { + _context.Set().Update(entity); + await _context.SaveChangesAsync(); + } + + public async Task DeleteAsync(int id) + { + var entity = await GetAsync(id); + await DeleteAsync(entity); + } + + public async Task DeleteAsync(T entity) + { + _context.Set().Remove(entity); + await _context.SaveChangesAsync(); + } +} diff --git a/NG_2023_Kanban.DataLayer/Repositories/BoardRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/BoardRepository.cs new file mode 100644 index 0000000..c32bea0 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Repositories/BoardRepository.cs @@ -0,0 +1,10 @@ +using NG_2023_Kanban.DataLayer.DbStartup; +using NG_2023_Kanban.DataLayer.Entities; +using NG_2023_Kanban.DataLayer.Interfaces; + +namespace NG_2023_Kanban.DataLayer.Repositories; + +public class BoardRepository : BaseRepository, IBoardRepository +{ + public BoardRepository(DatabaseContext context) : base(context) { } +} diff --git a/NG_2023_Kanban.DataLayer/Repositories/CardRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/CardRepository.cs new file mode 100644 index 0000000..9011ad0 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Repositories/CardRepository.cs @@ -0,0 +1,10 @@ +using NG_2023_Kanban.DataLayer.DbStartup; +using NG_2023_Kanban.DataLayer.Entities; +using NG_2023_Kanban.DataLayer.Interfaces; + +namespace NG_2023_Kanban.DataLayer.Repositories; + +public class CardRepository : BaseRepository, ICardRepository +{ + public CardRepository(DatabaseContext context) : base(context) { } +} diff --git a/NG_2023_Kanban.DataLayer/Repositories/ColumnRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/ColumnRepository.cs new file mode 100644 index 0000000..249ed05 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Repositories/ColumnRepository.cs @@ -0,0 +1,10 @@ +using NG_2023_Kanban.DataLayer.DbStartup; +using NG_2023_Kanban.DataLayer.Entities; +using NG_2023_Kanban.DataLayer.Interfaces; + +namespace NG_2023_Kanban.DataLayer.Repositories; + +public class ColumnRepository : BaseRepository, IColumnRepository +{ + public ColumnRepository(DatabaseContext context) : base(context) { } +} diff --git a/NG_2023_Kanban.DataLayer/Repositories/CommentRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/CommentRepository.cs new file mode 100644 index 0000000..2ae2f90 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Repositories/CommentRepository.cs @@ -0,0 +1,10 @@ +using NG_2023_Kanban.DataLayer.DbStartup; +using NG_2023_Kanban.DataLayer.Entities; +using NG_2023_Kanban.DataLayer.Interfaces; + +namespace NG_2023_Kanban.DataLayer.Repositories; + +public class CommentRepository : BaseRepository, ICommentRepository +{ + public CommentRepository(DatabaseContext context) : base(context) { } +} diff --git a/NG_2023_Kanban.DataLayer/Repositories/UserRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/UserRepository.cs new file mode 100644 index 0000000..9188896 --- /dev/null +++ b/NG_2023_Kanban.DataLayer/Repositories/UserRepository.cs @@ -0,0 +1,10 @@ +using NG_2023_Kanban.DataLayer.DbStartup; +using NG_2023_Kanban.DataLayer.Entities; +using NG_2023_Kanban.DataLayer.Interfaces; + +namespace NG_2023_Kanban.DataLayer.Repositories; + +public class UserRepository : BaseRepository, IUserRepository +{ + public UserRepository(DatabaseContext context) : base(context) { } +} diff --git a/NG_2023_Kanban.DataLayer/Service/DataService.cs b/NG_2023_Kanban.DataLayer/Service/DataService.cs deleted file mode 100644 index 27f5a62..0000000 --- a/NG_2023_Kanban.DataLayer/Service/DataService.cs +++ /dev/null @@ -1,59 +0,0 @@ -using NG_2023_Kanban.DataLayer.DbStartup; -using NG_2023_Kanban.DataLayer.Entities; -using Microsoft.EntityFrameworkCore; - -namespace NG_2023_Kanban.DataLayer.Service -{ - public class DataService - { - private readonly DatabaseContext _context; - public DataService(DatabaseContext context) - { - _context = context; - _context.Database.EnsureCreated(); - } - - public async Task AddAsync(User entity) - { - await _context.Set().AddAsync(entity); - await _context.SaveChangesAsync(); - - return entity; - } - - public async Task GetByIdAsync(int id) - { - var entity = await _context.Set().FirstOrDefaultAsync(x => x.Id == id); - - return entity; - } - - public async Task LoginAsync(string username, string password) - { - User? entity = await _context.Set().FirstOrDefaultAsync(x => x.Username == username); - - if (entity != null && entity.Password == password) // TODO: hashing - return entity; - return null; - } - - public async Task> GetAllAsync() - { - var entities = await _context.Set().Select(x => x).ToListAsync(); - - return entities; - } - - public async Task UpdateAsync(User model) - { - _context.Set().Update(model); - await _context.SaveChangesAsync(); - } - - public async Task DeleteAsync(User model) - { - _context.Set().Remove(model); - await _context.SaveChangesAsync(); - } - } -} diff --git a/NG_2023_Kanban/Controllers/HomeController.cs b/NG_2023_Kanban/Controllers/HomeController.cs index ffd9ab5..ea38165 100644 --- a/NG_2023_Kanban/Controllers/HomeController.cs +++ b/NG_2023_Kanban/Controllers/HomeController.cs @@ -3,19 +3,19 @@ using Microsoft.AspNetCore.Mvc; using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.Extensions; using NG_2023_Kanban.DataLayer.Models; -using NG_2023_Kanban.BusinessLayer.Service; +using NG_2023_Kanban.BusinessLayer.Services; namespace NG_2023_Kanban.Controllers; public class HomeController : Controller { - private readonly BusinessService _service; + private readonly UserService _userService; private readonly ILogger _logger; - public HomeController(ILogger logger, BusinessService service) + public HomeController(ILogger logger, UserService userService) { _logger = logger; - _service = service; + _userService = userService; } public IActionResult Index() @@ -39,13 +39,13 @@ public class HomeController : Controller } [HttpPost] - public async Task Login(string username, string password) + public async Task Login(User user) { User? currentAccount = HttpContext.Session.GetObject("Account"); if (currentAccount != null) return Redirect("/Home/Index"); - User? account = await _service.LoginAsync(username, password); + User? account = await _userService.LoginAsync(user); if (account != null) { HttpContext.Session.SetObject("Account", account); @@ -74,7 +74,7 @@ public class HomeController : Controller } [HttpPost] - public async Task Register(string fullName, string username, string password) + public async Task Register(User user) { User? currentAccount = HttpContext.Session.GetObject("Account"); if (currentAccount != null) @@ -82,7 +82,7 @@ public class HomeController : Controller try { - User account = await _service.RegisterAsync(fullName, username, password); + User account = await _userService.RegisterAsync(user); HttpContext.Session.SetObject("Account", account); return Redirect("/Home/Index");