Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions OpsFlow/Core/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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!;
Expand Down
33 changes: 30 additions & 3 deletions OpsFlow/Services/Helpers/FormValidationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions OpsFlow/Services/Implementations/UserRegistrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,5 +26,4 @@ public async Task RegisterPersonelAsync(User user, int roleId, int companyId, in
await Task.Run(() => _userService.RegisterUser(user));
}
}
}

}
5 changes: 2 additions & 3 deletions OpsFlow/Services/Interfaces/IUserRegistrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
111 changes: 93 additions & 18 deletions OpsFlow/UI/Forms/Management/AddPersonelForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OpsFlow.Core.Enums;
using OpsFlow.Core.Enums;
using OpsFlow.Core.Exceptions;
using OpsFlow.Core.Models;
using OpsFlow.Core.Services;
Expand All @@ -17,7 +17,7 @@ public partial class AddPersonelForm : BaseForm
private List<Role> _roles = new List<Role>();
private List<Company> _companies = new List<Company>();
private List<Department> _departments = new List<Department>();

public string? UploadedAvatarPath => _uploadedFilePath;

public AddPersonelForm()
Expand All @@ -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)
Expand Down Expand Up @@ -147,6 +148,8 @@ private void LoadComboBoxes(List<Role> roles, List<Company> companies, List<Depa
cmbTitle.Items.Add(department.DepartmentName);
cmbTitle.SelectedIndex = 0;
cmbTitle.EndUpdate();

UpdateFieldStates(null);
}

private async void picProfilePhoto_Click_1(object sender, EventArgs e)
Expand Down Expand Up @@ -217,31 +220,44 @@ 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)
{
Notifier.Show("Hata", validationResult.ErrorMessage!, NotificationType.Error);
return;
}

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)
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(() =>
Expand All @@ -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(() =>
{
Expand Down Expand Up @@ -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)
Expand Down
Loading