diff --git a/OpsFlow/Core/Models/User.cs b/OpsFlow/Core/Models/User.cs index 2c97143..0a67580 100644 --- a/OpsFlow/Core/Models/User.cs +++ b/OpsFlow/Core/Models/User.cs @@ -12,7 +12,7 @@ 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; } @@ -50,7 +50,7 @@ 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!; 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/UserRegistrationService.cs b/OpsFlow/Services/Implementations/UserRegistrationService.cs index ff133b1..6739b7c 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 async 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; @@ -26,5 +26,4 @@ public async Task RegisterPersonelAsync(User user, int roleId, int companyId, in await Task.Run(() => _userService.RegisterUser(user)); } } -} - +} \ No newline at end of file diff --git a/OpsFlow/Services/Interfaces/IUserRegistrationService.cs b/OpsFlow/Services/Interfaces/IUserRegistrationService.cs index 4ca29e5..62ad560 100644 --- a/OpsFlow/Services/Interfaces/IUserRegistrationService.cs +++ b/OpsFlow/Services/Interfaces/IUserRegistrationService.cs @@ -4,7 +4,6 @@ namespace OpsFlow.Services.Interfaces { public interface IUserRegistrationService { - Task RegisterPersonelAsync(User user, int roleId, int companyId, int? departmentId = null); + Task RegisterPersonelAsync(User user, int roleId, int? companyId, int? departmentId = null); } -} - +} \ No newline at end of file diff --git a/OpsFlow/UI/Forms/Management/AddPersonelForm.cs b/OpsFlow/UI/Forms/Management/AddPersonelForm.cs index f53745c..7003fab 100644 --- a/OpsFlow/UI/Forms/Management/AddPersonelForm.cs +++ b/OpsFlow/UI/Forms/Management/AddPersonelForm.cs @@ -1,4 +1,4 @@ -using OpsFlow.Core.Enums; +using OpsFlow.Core.Enums; using OpsFlow.Core.Exceptions; using OpsFlow.Core.Models; using OpsFlow.Core.Services; @@ -17,7 +17,7 @@ public partial class AddPersonelForm : BaseForm private List _roles = new List(); private List _companies = new List(); private List _departments = new List(); - + public string? UploadedAvatarPath => _uploadedFilePath; public AddPersonelForm() @@ -28,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) @@ -147,6 +148,8 @@ private void LoadComboBoxes(List roles, List companies, List @@ -259,10 +275,39 @@ private async void BtnSave_Click(object? sender, EventArgs e) AvatarUrl = _uploadedFilePath }; - 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); + 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, companyIdForRegistration, departmentIdForRegistration); + }); this.Invoke(new Action(() => { @@ -328,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)