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
20 changes: 20 additions & 0 deletions OpsFlow/Core/Models/Department.cs
Original file line number Diff line number Diff line change
@@ -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<User> Users { get; set; } = new List<User>();
}

6 changes: 6 additions & 0 deletions OpsFlow/Core/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
}
1 change: 1 addition & 0 deletions OpsFlow/Data/Context/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<User> Users => Set<User>();
public DbSet<Role> Roles => Set<Role>();
public DbSet<Company> Companies => Set<Company>();
public DbSet<Department> Departments => Set<Department>();
public DbSet<IndividualCustomer> IndividualCustomers => Set<IndividualCustomer>();
}
}
8 changes: 8 additions & 0 deletions OpsFlow/Services/Helpers/ComboBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public static class ComboBoxHelper

return companies.FirstOrDefault(c => c.CompanyName == companyName);
}

public static Department? FindDepartmentByName(List<Department> departments, string departmentName)
{
if (string.IsNullOrWhiteSpace(departmentName) || departmentName == "Departman seçiniz")
return null;

return departments.FirstOrDefault(d => d.DepartmentName == departmentName);
}
}
}

63 changes: 63 additions & 0 deletions OpsFlow/Services/Implementations/DepartmentService.cs
Original file line number Diff line number Diff line change
@@ -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<Department> 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<List<Department>> 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);
}
}
}
}

5 changes: 3 additions & 2 deletions OpsFlow/Services/Implementations/UserRegistrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions OpsFlow/Services/Interfaces/IDepartmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using OpsFlow.Core.Models;

namespace OpsFlow.Services.Interfaces
{
public interface IDepartmentService
{
List<Department> GetAllDepartments();
Task<List<Department>> GetAllDepartmentsAsync();
Department? GetDepartmentById(int id);
}
}

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

2 changes: 1 addition & 1 deletion OpsFlow/UI/Forms/Auth/ResetPasswordForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions OpsFlow/UI/Forms/Auth/VerificationForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
50 changes: 22 additions & 28 deletions OpsFlow/UI/Forms/Management/AddPersonelForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public partial class AddPersonelForm : BaseForm
private string? _uploadedFilePath;
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 Down Expand Up @@ -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<Role>? roles = null;
List<Company>? companies = null;
List<Department>? 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)
{
Expand All @@ -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<Role> roles, List<Company> companies)
private void LoadComboBoxes(List<Role> roles, List<Company> companies, List<Department> departments)
{
_roles = roles;
_companies = companies;
_departments = departments;

cmbRole.BeginUpdate();
cmbRole.Items.Clear();
Expand All @@ -143,13 +143,8 @@ private void LoadComboBoxes(List<Role> roles, List<Company> 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();
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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(() =>
{
Expand Down
Loading