diff --git a/OpsFlow/Core/Models/Department.cs b/OpsFlow/Core/Models/Department.cs new file mode 100644 index 0000000..7c47bb3 --- /dev/null +++ b/OpsFlow/Core/Models/Department.cs @@ -0,0 +1,20 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace OpsFlow.Core.Models; + +[Table("departments")] +public class Department +{ + [Key] + [Column("id")] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int Id { get; set; } + + [Column("department_name")] + [MaxLength(100)] + public string DepartmentName { get; set; } = string.Empty; + + public virtual ICollection Users { get; set; } = new List(); +} + diff --git a/OpsFlow/Core/Models/User.cs b/OpsFlow/Core/Models/User.cs index 422ffbd..2c97143 100644 --- a/OpsFlow/Core/Models/User.cs +++ b/OpsFlow/Core/Models/User.cs @@ -17,6 +17,9 @@ public class User [Column("role_id")] public int RoleId { get; set; } + [Column("department_id")] + public int? DepartmentId { get; set; } + [Column("name")] [MaxLength(100)] public string Name { get; set; } = string.Empty; @@ -52,5 +55,8 @@ public class User [ForeignKey(nameof(RoleId))] public virtual Role Role { get; set; } = null!; + [ForeignKey(nameof(DepartmentId))] + public virtual Department? Department { get; set; } + public virtual IndividualCustomer? IndividualCustomer { get; set; } } \ No newline at end of file diff --git a/OpsFlow/Data/Context/AppDbContext.cs b/OpsFlow/Data/Context/AppDbContext.cs index 29f06c4..cda69a6 100644 --- a/OpsFlow/Data/Context/AppDbContext.cs +++ b/OpsFlow/Data/Context/AppDbContext.cs @@ -11,6 +11,7 @@ public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Users => Set(); public DbSet Roles => Set(); public DbSet Companies => Set(); + public DbSet Departments => Set(); public DbSet IndividualCustomers => Set(); } } \ No newline at end of file diff --git a/OpsFlow/Services/Helpers/ComboBoxHelper.cs b/OpsFlow/Services/Helpers/ComboBoxHelper.cs index 0f25d21..ff0f53f 100644 --- a/OpsFlow/Services/Helpers/ComboBoxHelper.cs +++ b/OpsFlow/Services/Helpers/ComboBoxHelper.cs @@ -19,6 +19,14 @@ public static class ComboBoxHelper return companies.FirstOrDefault(c => c.CompanyName == companyName); } + + public static Department? FindDepartmentByName(List departments, string departmentName) + { + if (string.IsNullOrWhiteSpace(departmentName) || departmentName == "Departman seçiniz") + return null; + + return departments.FirstOrDefault(d => d.DepartmentName == departmentName); + } } } diff --git a/OpsFlow/Services/Implementations/DepartmentService.cs b/OpsFlow/Services/Implementations/DepartmentService.cs new file mode 100644 index 0000000..3c95450 --- /dev/null +++ b/OpsFlow/Services/Implementations/DepartmentService.cs @@ -0,0 +1,63 @@ +using Microsoft.EntityFrameworkCore; +using OpsFlow.Core.Exceptions; +using OpsFlow.Core.Models; +using OpsFlow.Data.Context; +using OpsFlow.Services.Interfaces; + +namespace OpsFlow.Services.Implementations +{ + public class DepartmentService : IDepartmentService + { + private readonly AppDbContext _context; + + public DepartmentService(AppDbContext context) + { + _context = context; + } + + public List GetAllDepartments() + { + try + { + return _context.Departments + .AsNoTracking() + .OrderBy(d => d.DepartmentName) + .ToList(); + } + catch (Exception ex) + { + throw new DatabaseQueryException("Departmanlar alınırken bir hata oluştu.", ex); + } + } + + public async Task> GetAllDepartmentsAsync() + { + try + { + return await _context.Departments + .AsNoTracking() + .OrderBy(d => d.DepartmentName) + .ToListAsync(); + } + catch (Exception ex) + { + throw new DatabaseQueryException("Departmanlar alınırken bir hata oluştu.", ex); + } + } + + public Department? GetDepartmentById(int id) + { + try + { + return _context.Departments + .AsNoTracking() + .FirstOrDefault(d => d.Id == id); + } + catch (Exception ex) + { + throw new DatabaseQueryException($"Departman sorgulanırken bir hata oluştu: {id}", ex); + } + } + } +} + diff --git a/OpsFlow/Services/Implementations/UserRegistrationService.cs b/OpsFlow/Services/Implementations/UserRegistrationService.cs index 52b4ec3..ff133b1 100644 --- a/OpsFlow/Services/Implementations/UserRegistrationService.cs +++ b/OpsFlow/Services/Implementations/UserRegistrationService.cs @@ -15,14 +15,15 @@ public UserRegistrationService(AppDbContext context, IUserService userService) _userService = userService; } - public Task RegisterPersonelAsync(User user, int roleId, int companyId) + public async Task RegisterPersonelAsync(User user, int roleId, int companyId, int? departmentId = null) { user.RoleId = roleId; user.CompanyId = companyId; + user.DepartmentId = departmentId; user.IsActive = true; user.CreatedAt = DateTime.UtcNow; - return Task.Run(() => _userService.RegisterUser(user)); + await Task.Run(() => _userService.RegisterUser(user)); } } } diff --git a/OpsFlow/Services/Interfaces/IDepartmentService.cs b/OpsFlow/Services/Interfaces/IDepartmentService.cs new file mode 100644 index 0000000..6eadf08 --- /dev/null +++ b/OpsFlow/Services/Interfaces/IDepartmentService.cs @@ -0,0 +1,12 @@ +using OpsFlow.Core.Models; + +namespace OpsFlow.Services.Interfaces +{ + public interface IDepartmentService + { + List GetAllDepartments(); + Task> GetAllDepartmentsAsync(); + Department? GetDepartmentById(int id); + } +} + diff --git a/OpsFlow/Services/Interfaces/IUserRegistrationService.cs b/OpsFlow/Services/Interfaces/IUserRegistrationService.cs index e7bd93a..4ca29e5 100644 --- a/OpsFlow/Services/Interfaces/IUserRegistrationService.cs +++ b/OpsFlow/Services/Interfaces/IUserRegistrationService.cs @@ -4,7 +4,7 @@ namespace OpsFlow.Services.Interfaces { public interface IUserRegistrationService { - Task RegisterPersonelAsync(User user, int roleId, int companyId); + Task RegisterPersonelAsync(User user, int roleId, int companyId, int? departmentId = null); } } diff --git a/OpsFlow/UI/Forms/Auth/ResetPasswordForm.cs b/OpsFlow/UI/Forms/Auth/ResetPasswordForm.cs index ee40bec..0cf7e31 100644 --- a/OpsFlow/UI/Forms/Auth/ResetPasswordForm.cs +++ b/OpsFlow/UI/Forms/Auth/ResetPasswordForm.cs @@ -58,7 +58,7 @@ private async void btnSavePassword_Click(object sender, EventArgs e) { Notifier.Show("Uyarı", ex.Message, NotificationType.Warning); } - catch (Exception ex) + catch (Exception) { Notifier.Show("Hata", "Sistem hatası nedeniyle şifre güncellenemedi.", NotificationType.Error); } diff --git a/OpsFlow/UI/Forms/Auth/VerificationForm.cs b/OpsFlow/UI/Forms/Auth/VerificationForm.cs index 16ee9d0..a8d6e6d 100644 --- a/OpsFlow/UI/Forms/Auth/VerificationForm.cs +++ b/OpsFlow/UI/Forms/Auth/VerificationForm.cs @@ -55,7 +55,7 @@ private async Task SendInitialCodeFlowAsync() { Notifier.Show("Sınır Aşıldı", ex.Message, NotificationType.Warning); } - catch (Exception ex) + catch (Exception) { Notifier.Show("Hata", "Kod gönderilemedi.", NotificationType.Error); } @@ -83,7 +83,7 @@ private async void btnVerifyCode_Click(object sender, EventArgs e) Notifier.Show("Doğrulama Hatası", ex.Message, NotificationType.Error); ClearAndResetInput(); } - catch (Exception ex) + catch (Exception) { Notifier.Show("Hata", "Sistem hatası oluştu.", NotificationType.Error); ClearAndResetInput(); @@ -115,7 +115,7 @@ private async void lnkResendCode_LinkClicked(object sender, LinkLabelLinkClicked lnkResendCode.Cursor = Cursors.No; Notifier.Show("Sınır Aşıldı", ex.Message, NotificationType.Warning); } - catch (Exception ex) + catch (Exception) { Notifier.Show("Hata", "Kod gönderilirken bir sorun oluştu.", NotificationType.Error); } diff --git a/OpsFlow/UI/Forms/Management/AddPersonelForm.cs b/OpsFlow/UI/Forms/Management/AddPersonelForm.cs index 5236534..f53745c 100644 --- a/OpsFlow/UI/Forms/Management/AddPersonelForm.cs +++ b/OpsFlow/UI/Forms/Management/AddPersonelForm.cs @@ -16,7 +16,8 @@ public partial class AddPersonelForm : BaseForm private string? _uploadedFilePath; private List _roles = new List(); private List _companies = new List(); - + private List _departments = new List(); + public string? UploadedAvatarPath => _uploadedFilePath; public AddPersonelForm() @@ -50,35 +51,33 @@ private async Task LoadComboBoxDataAsync() cmbTitle.BeginUpdate(); cmbTitle.Items.Clear(); - cmbTitle.Items.Add("Departman seçiniz"); - cmbTitle.Items.Add("İnsan Kaynakları"); - cmbTitle.Items.Add("Bilgi İşlem"); - cmbTitle.Items.Add("Muhasebe"); - cmbTitle.Items.Add("Satış"); - cmbTitle.Items.Add("Pazarlama"); - cmbTitle.Items.Add("Üretim"); - cmbTitle.Items.Add("Yönetim"); + cmbTitle.Items.Add("Yükleniyor..."); cmbTitle.SelectedIndex = 0; cmbTitle.EndUpdate(); List? roles = null; List? companies = null; + List? departments = null; try { using var roleContext = DatabaseManager.CreateContext(); using var companyContext = DatabaseManager.CreateContext(); + using var departmentContext = DatabaseManager.CreateContext(); var roleService = new RoleService(roleContext); var companyService = new CompanyService(companyContext); + var departmentService = new DepartmentService(departmentContext); var rolesTask = roleService.GetAllRolesAsync(); var companiesTask = companyService.GetAllCompaniesAsync(); + var departmentsTask = departmentService.GetAllDepartmentsAsync(); - await Task.WhenAll(rolesTask, companiesTask); + await Task.WhenAll(rolesTask, companiesTask, departmentsTask); roles = await rolesTask; companies = await companiesTask; + departments = await departmentsTask; } catch (DatabaseQueryException dbEx) { @@ -103,26 +102,27 @@ private async Task LoadComboBoxDataAsync() return; } - if (roles != null && companies != null) + if (roles != null && companies != null && departments != null) { if (this.InvokeRequired) { this.Invoke(new Action(() => { - LoadComboBoxes(roles, companies); + LoadComboBoxes(roles, companies, departments); })); } else { - LoadComboBoxes(roles, companies); + LoadComboBoxes(roles, companies, departments); } } } - private void LoadComboBoxes(List roles, List companies) + private void LoadComboBoxes(List roles, List companies, List departments) { _roles = roles; _companies = companies; + _departments = departments; cmbRole.BeginUpdate(); cmbRole.Items.Clear(); @@ -143,13 +143,8 @@ private void LoadComboBoxes(List roles, List companies) cmbTitle.BeginUpdate(); cmbTitle.Items.Clear(); cmbTitle.Items.Add("Departman seçiniz"); - cmbTitle.Items.Add("İnsan Kaynakları"); - cmbTitle.Items.Add("Bilgi İşlem"); - cmbTitle.Items.Add("Muhasebe"); - cmbTitle.Items.Add("Satış"); - cmbTitle.Items.Add("Pazarlama"); - cmbTitle.Items.Add("Üretim"); - cmbTitle.Items.Add("Yönetim"); + foreach (var department in departments) + cmbTitle.Items.Add(department.DepartmentName); cmbTitle.SelectedIndex = 0; cmbTitle.EndUpdate(); } @@ -234,6 +229,8 @@ private async void BtnSave_Click(object? sender, EventArgs e) var selectedRole = ComboBoxHelper.FindRoleByName(_roles, selectedRoleName); var selectedCompany = ComboBoxHelper.FindCompanyByName(_companies, selectedCompanyName); + string selectedDepartmentName = cmbTitle.SelectedItem?.ToString() ?? string.Empty; + var selectedDepartment = ComboBoxHelper.FindDepartmentByName(_departments, selectedDepartmentName); if (selectedRole == null) { @@ -262,13 +259,10 @@ private async void BtnSave_Click(object? sender, EventArgs e) AvatarUrl = _uploadedFilePath }; - await Task.Run(async () => - { - using var context = DatabaseManager.CreateContext(); - var userService = new UserService(context); - var registrationService = new UserRegistrationService(context, userService); - await registrationService.RegisterPersonelAsync(newUser, selectedRole.Id, selectedCompany.Id); - }); + using var context = DatabaseManager.CreateContext(); + var userService = new UserService(context); + var registrationService = new UserRegistrationService(context, userService); + await registrationService.RegisterPersonelAsync(newUser, selectedRole.Id, selectedCompany.Id, selectedDepartment?.Id); this.Invoke(new Action(() => {