From 6c32a49269c179ad733b91438418eb745a6dee70 Mon Sep 17 00:00:00 2001 From: alisec Date: Sat, 3 Jan 2026 17:58:45 +0300 Subject: [PATCH 1/2] feat(validation): implement role-based field validation and department integration --- OpsFlow/Core/Models/Department.cs | 20 +++ OpsFlow/Core/Models/User.cs | 10 +- OpsFlow/Data/Context/AppDbContext.cs | 1 + OpsFlow/Services/Helpers/ComboBoxHelper.cs | 8 ++ .../Services/Helpers/FormValidationHelper.cs | 33 ++++- .../Implementations/DepartmentService.cs | 63 +++++++++ .../UserRegistrationService.cs | 3 +- .../Services/Interfaces/IDepartmentService.cs | 12 ++ .../Interfaces/IUserRegistrationService.cs | 2 +- .../UI/Forms/Management/AddPersonelForm.cs | 131 +++++++++++++----- 10 files changed, 245 insertions(+), 38 deletions(-) create mode 100644 OpsFlow/Core/Models/Department.cs create mode 100644 OpsFlow/Services/Implementations/DepartmentService.cs create mode 100644 OpsFlow/Services/Interfaces/IDepartmentService.cs 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..0a67580 100644 --- a/OpsFlow/Core/Models/User.cs +++ b/OpsFlow/Core/Models/User.cs @@ -12,11 +12,14 @@ public class User public int Id { get; set; } [Column("company_id")] - public int CompanyId { get; set; } + public int? CompanyId { get; set; } [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; @@ -47,10 +50,13 @@ public class User public string? AvatarUrl { get; set; } [ForeignKey(nameof(CompanyId))] - public virtual Company Company { get; set; } = null!; + public virtual Company? Company { get; set; } [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/Helpers/FormValidationHelper.cs b/OpsFlow/Services/Helpers/FormValidationHelper.cs index b662774..dd9c169 100644 --- a/OpsFlow/Services/Helpers/FormValidationHelper.cs +++ b/OpsFlow/Services/Helpers/FormValidationHelper.cs @@ -8,7 +8,9 @@ public static (bool IsValid, string? ErrorMessage) ValidatePersonelForm( string email, string password, string? roleName, - string? companyName) + string? companyName, + int roleId, + string? departmentName) { if (string.IsNullOrWhiteSpace(name)) { @@ -35,9 +37,34 @@ public static (bool IsValid, string? ErrorMessage) ValidatePersonelForm( return (false, "Lütfen bir rol seçiniz."); } - if (string.IsNullOrWhiteSpace(companyName) || companyName == "Şirket seçiniz") + if (roleId == 1 || roleId == 2) { - return (false, "Lütfen bir şirket seçiniz."); + if (!string.IsNullOrWhiteSpace(companyName) && companyName != "Şirket seçiniz") + { + return (false, "Bu rol için şirket seçimi yapılamaz."); + } + } + else if (roleId == 3) + { + if (!string.IsNullOrWhiteSpace(departmentName) && departmentName != "Departman seçiniz") + { + return (false, "Bu rol için departman seçimi yapılamaz."); + } + } + else + { + if (string.IsNullOrWhiteSpace(companyName) || companyName == "Şirket seçiniz") + { + return (false, "Lütfen bir şirket seçiniz."); + } + } + + if (roleId != 3) + { + if (string.IsNullOrWhiteSpace(departmentName) || departmentName == "Departman seçiniz") + { + return (false, "Lütfen bir departman seçiniz."); + } } return (true, null); 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..25341e2 100644 --- a/OpsFlow/Services/Implementations/UserRegistrationService.cs +++ b/OpsFlow/Services/Implementations/UserRegistrationService.cs @@ -15,10 +15,11 @@ public UserRegistrationService(AppDbContext context, IUserService userService) _userService = userService; } - public Task RegisterPersonelAsync(User user, int roleId, int companyId) + public 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; 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..669c3ee 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/Management/AddPersonelForm.cs b/OpsFlow/UI/Forms/Management/AddPersonelForm.cs index 5236534..cb1f246 100644 --- a/OpsFlow/UI/Forms/Management/AddPersonelForm.cs +++ b/OpsFlow/UI/Forms/Management/AddPersonelForm.cs @@ -16,6 +16,7 @@ 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; @@ -27,6 +28,7 @@ public AddPersonelForm() this.Load += AddPersonelForm_Load; btnSave.Click += BtnSave_Click; + cmbRole.SelectedIndexChanged += CmbRole_SelectedIndexChanged; } private async void AddPersonelForm_Load(object? sender, EventArgs e) @@ -50,35 +52,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 +103,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,15 +144,12 @@ 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(); + + UpdateFieldStates(null); } private async void picProfilePhoto_Click_1(object sender, EventArgs e) @@ -222,9 +220,17 @@ private async void BtnSave_Click(object? sender, EventArgs e) string password = txtPassword.Text; string selectedRoleName = cmbRole.SelectedItem?.ToString() ?? string.Empty; string selectedCompanyName = cmbDepartment.SelectedItem?.ToString() ?? string.Empty; + string selectedDepartmentName = cmbTitle.SelectedItem?.ToString() ?? string.Empty; + + var selectedRole = ComboBoxHelper.FindRoleByName(_roles, selectedRoleName); + if (selectedRole == null) + { + Notifier.Show("Hata", "Seçilen rol bulunamadı.", NotificationType.Error); + return; + } var validationResult = FormValidationHelper.ValidatePersonelForm( - name, surname, email, password, selectedRoleName, selectedCompanyName); + name, surname, email, password, selectedRoleName, selectedCompanyName, selectedRole.Id, selectedDepartmentName); if (!validationResult.IsValid) { @@ -232,19 +238,26 @@ private async void BtnSave_Click(object? sender, EventArgs e) return; } - var selectedRole = ComboBoxHelper.FindRoleByName(_roles, selectedRoleName); var selectedCompany = ComboBoxHelper.FindCompanyByName(_companies, selectedCompanyName); - if (selectedRole == null) + if (selectedRole.Id == 1 || selectedRole.Id == 2) { - Notifier.Show("Hata", "Seçilen rol bulunamadı.", NotificationType.Error); - return; } - - if (selectedCompany == null) + else if (selectedRole.Id == 3) { - Notifier.Show("Hata", "Seçilen şirket bulunamadı.", NotificationType.Error); - return; + if (selectedCompany == null && !string.IsNullOrWhiteSpace(selectedCompanyName) && selectedCompanyName != "Şirket seçiniz") + { + Notifier.Show("Hata", "Seçilen şirket bulunamadı.", NotificationType.Error); + return; + } + } + else + { + if (selectedCompany == null) + { + Notifier.Show("Hata", "Seçilen şirket bulunamadı.", NotificationType.Error); + return; + } } this.Invoke(new Action(() => @@ -262,12 +275,38 @@ private async void BtnSave_Click(object? sender, EventArgs e) AvatarUrl = _uploadedFilePath }; + int? companyIdForRegistration = null; + int? departmentIdForRegistration = null; + + if (selectedRole.Id == 1 || selectedRole.Id == 2) + { + companyIdForRegistration = null; + var selectedDepartment = ComboBoxHelper.FindDepartmentByName(_departments, selectedDepartmentName); + departmentIdForRegistration = selectedDepartment?.Id; + } + else if (selectedRole.Id == 3) + { + companyIdForRegistration = selectedCompany?.Id; + departmentIdForRegistration = null; + } + else + { + if (selectedCompany == null) + { + Notifier.Show("Hata", "Şirket seçimi gereklidir.", NotificationType.Error); + return; + } + companyIdForRegistration = selectedCompany.Id; + var selectedDepartment = ComboBoxHelper.FindDepartmentByName(_departments, selectedDepartmentName); + departmentIdForRegistration = selectedDepartment?.Id; + } + 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); + await registrationService.RegisterPersonelAsync(newUser, selectedRole.Id, companyIdForRegistration, departmentIdForRegistration); }); this.Invoke(new Action(() => @@ -334,9 +373,39 @@ private void ClearForm() cmbTitle.SelectedIndex = 0; } - private void guna2ComboBox2_SelectedIndexChanged(object sender, EventArgs e) + private void CmbRole_SelectedIndexChanged(object? sender, EventArgs e) { + string selectedRoleName = cmbRole.SelectedItem?.ToString() ?? string.Empty; + var selectedRole = ComboBoxHelper.FindRoleByName(_roles, selectedRoleName); + UpdateFieldStates(selectedRole); + } + private void UpdateFieldStates(Role? selectedRole) + { + if (selectedRole == null) + { + cmbDepartment.Enabled = true; + cmbTitle.Enabled = true; + return; + } + + if (selectedRole.Id == 1 || selectedRole.Id == 2) + { + cmbDepartment.Enabled = false; + cmbDepartment.SelectedIndex = 0; + cmbTitle.Enabled = true; + } + else if (selectedRole.Id == 3) + { + cmbDepartment.Enabled = true; + cmbTitle.Enabled = false; + cmbTitle.SelectedIndex = 0; + } + else + { + cmbDepartment.Enabled = true; + cmbTitle.Enabled = true; + } } private void btnCompanyRegister_Click(object sender, EventArgs e) From b4a1708bca27e6cfffb030028e68edec047b5648 Mon Sep 17 00:00:00 2001 From: alisec Date: Sat, 3 Jan 2026 18:01:43 +0300 Subject: [PATCH 2/2] fix(merge): resolve conflicts in role-based validation merge --- OpsFlow/Services/Implementations/UserRegistrationService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OpsFlow/Services/Implementations/UserRegistrationService.cs b/OpsFlow/Services/Implementations/UserRegistrationService.cs index 25341e2..57dbae8 100644 --- a/OpsFlow/Services/Implementations/UserRegistrationService.cs +++ b/OpsFlow/Services/Implementations/UserRegistrationService.cs @@ -15,7 +15,7 @@ public UserRegistrationService(AppDbContext context, IUserService userService) _userService = userService; } - public Task RegisterPersonelAsync(User user, int roleId, int? companyId, int? departmentId = null) + public async Task RegisterPersonelAsync(User user, int roleId, int? companyId, int? departmentId = null) { user.RoleId = roleId; user.CompanyId = companyId; @@ -23,7 +23,7 @@ public Task RegisterPersonelAsync(User user, int roleId, int? companyId, int? de user.IsActive = true; user.CreatedAt = DateTime.UtcNow; - return Task.Run(() => _userService.RegisterUser(user)); + await Task.Run(() => _userService.RegisterUser(user)); } } }