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
5 changes: 5 additions & 0 deletions OpsFlow/Core/Models/Company.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Company
{
[Key]
[Column("id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Column("company_name")]
Expand All @@ -21,6 +22,10 @@ public class Company
[Column("address")]
public string? Address { get; set; }

[Column("phone")]
[MaxLength(20)]
public string? Phone { get; set; }

[Column("created_at")]
public DateTime CreatedAt { get; set; }

Expand Down
34 changes: 34 additions & 0 deletions OpsFlow/Services/Helpers/CompanyValidationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace OpsFlow.Services.Helpers
{
public static class CompanyValidationHelper
{
public static (bool IsValid, string? ErrorMessage) ValidateCompanyForm(
string companyName,
string? taxNumber,
string? phone)
{
if (string.IsNullOrWhiteSpace(companyName))
{
return (false, "Lütfen şirket adını giriniz.");
}

if (companyName.Length > 255)
{
return (false, "Şirket adı en fazla 255 karakter olabilir.");
}

if (!string.IsNullOrWhiteSpace(taxNumber) && taxNumber.Length > 50)
{
return (false, "Vergi numarası en fazla 50 karakter olabilir.");
}

if (!string.IsNullOrWhiteSpace(phone) && phone.Length > 20)
{
return (false, "Telefon numarası en fazla 20 karakter olabilir.");
}

return (true, null);
}
}
}

69 changes: 69 additions & 0 deletions OpsFlow/Services/Implementations/CompanyService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Npgsql;
using OpsFlow.Core.Exceptions;
using OpsFlow.Core.Models;
using OpsFlow.Data.Context;
Expand Down Expand Up @@ -58,6 +59,74 @@ public async Task<List<Company>> GetAllCompaniesAsync()
throw new DatabaseQueryException($"Şirket sorgulanırken bir hata oluştu: {id}", ex);
}
}

public void RegisterCompany(Company company)
{
if (company == null)
throw new ValidationException("Şirket bilgisi boş olamaz.");

if (string.IsNullOrWhiteSpace(company.CompanyName))
throw new ValidationException("Şirket adı boş olamaz.");

try
{
bool companyExists = _context.Companies.Any(c => c.CompanyName == company.CompanyName);
if (companyExists)
{
throw new BusinessException("Bu şirket adı sistemde zaten kayıtlı.");
}

if (!string.IsNullOrWhiteSpace(company.TaxNumber))
{
bool taxNumberExists = _context.Companies.Any(c => c.TaxNumber == company.TaxNumber);
if (taxNumberExists)
{
throw new BusinessException("Bu vergi numarası sistemde zaten kayıtlı.");
}
}

if (company.CreatedAt == default)
{
company.CreatedAt = DateTime.UtcNow;
}

_context.Companies.Add(company);
_context.SaveChanges();
}
catch (BusinessException)
{
throw;
}
catch (Microsoft.EntityFrameworkCore.DbUpdateException dbEx)
{
string errorDetail = "Veritabanı güncelleme hatası oluştu.";
if (dbEx.InnerException != null)
{
if (dbEx.InnerException is Npgsql.PostgresException pgEx)
{
errorDetail = $"Veritabanı hatası: {pgEx.MessageText}";
if (!string.IsNullOrEmpty(pgEx.Detail))
{
errorDetail += $"\nDetay: {pgEx.Detail}";
}
}
else
{
errorDetail = $"Veritabanı hatası: {dbEx.InnerException.Message}";
}
}
throw new DatabaseQueryException(errorDetail, dbEx);
}
catch (Exception ex)
{
throw new DatabaseQueryException("Şirket kaydı sırasında veritabanı hatası oluştu.", ex);
}
}

public async Task RegisterCompanyAsync(Company company)
{
await Task.Run(() => RegisterCompany(company));
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions OpsFlow/Services/Interfaces/ICompanyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public interface ICompanyService
List<Company> GetAllCompanies();
Task<List<Company>> GetAllCompaniesAsync();
Company? GetCompanyById(int id);
void RegisterCompany(Company company);
Task RegisterCompanyAsync(Company company);
}
}

Expand Down
7 changes: 6 additions & 1 deletion OpsFlow/UI/Forms/Auth/LoginForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ private async void btnLogin_Click(object sender, EventArgs e)
}
catch (Exception ex)
{
Notifier.Show("Sistem Hatası", "Beklenmedik bir hata oluştu.", NotificationType.Error);
var msg = "Beklenmedik bir hata oluştu.";
if (ex is OpsFlow.Core.Exceptions.DatabaseQueryException && ex.InnerException != null)
msg += $"\nDB Hatası: {ex.InnerException.Message}";
else
msg += $"\n{ex.Message}";
Notifier.Show("Sistem Hatası", msg, NotificationType.Error);
}
finally
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

190 changes: 179 additions & 11 deletions OpsFlow/UI/Forms/Management/CompanyRegisterForm.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,206 @@
using OpsFlow.Core.Enums;
using OpsFlow.Core.Exceptions;
using OpsFlow.Core.Models;
using OpsFlow.Core.Services;
using OpsFlow.Services.Helpers;
using OpsFlow.Services.Implementations;
using OpsFlow.UI.Forms.Core;
using OpsFlow.UI.Forms.Dialogs.Notifications;

namespace OpsFlow.UI.Forms.Management
{
public partial class CompanyRegisterForm : BaseForm
{
private bool _isProcessing = false;

public CompanyRegisterForm()
{
InitializeComponent();
this.btnSaveCompany.Click += new EventHandler(btnSaveCompany_Click);
btnSaveCompany.Click += BtnSaveCompany_Click;
}

private void guna2TextBox1_TextChanged(object sender, EventArgs e)
private void txtPhone_TextChanged(object sender, EventArgs e)
{

}

private void txtPhone_TextChanged(object sender, EventArgs e)
private void guna2TextBox1_TextChanged(object sender, EventArgs e)
{

}

private void btnSaveCompany_Click(object? sender, EventArgs e)
private async void BtnSaveCompany_Click(object? sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtName.Text) || string.IsNullOrWhiteSpace(txtTaxNumber.Text))
{
Notifier.Show("Hata", "Lütfen şirket adı ve vergi numarasını giriniz.", NotificationType.Error);
if (_isProcessing)
return;

_isProcessing = true;

if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
btnSaveCompany.Enabled = false;
btnSaveCompany.Text = "Kaydediliyor...";
}));
}
else
{
btnSaveCompany.Enabled = false;
btnSaveCompany.Text = "Kaydediliyor...";
}

Notifier.Show("Başarılı", "Şirket kaydı başarıyla alındı!", NotificationType.Success);
try
{
string companyName = txtName.Text.Trim();
string taxNumber = txtTaxNumber.Text.Trim();
string address = txtAddress.Text.Trim();
string phone = txtPhone.Text.Trim();

var validationResult = CompanyValidationHelper.ValidateCompanyForm(
companyName, taxNumber, phone);

if (!validationResult.IsValid)
{
Notifier.Show("Hata", validationResult.ErrorMessage!, NotificationType.Error);
return;
}

Notifier.Show("Kaydediliyor", "Şirket bilgileri kaydediliyor...", NotificationType.Information);

var newCompany = new Company
{
CompanyName = companyName,
TaxNumber = string.IsNullOrWhiteSpace(taxNumber) ? null : taxNumber,
Address = string.IsNullOrWhiteSpace(address) ? null : address,
Phone = string.IsNullOrWhiteSpace(phone) ? null : phone,
CreatedAt = DateTime.UtcNow
};

using var context = DatabaseManager.CreateContext();
var companyService = new CompanyService(context);
await companyService.RegisterCompanyAsync(newCompany);

this.Close();
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
Notifier.Show("Başarılı", "Şirket başarıyla kaydedildi.", NotificationType.Success);
ClearForm();
}));
}
else
{
Notifier.Show("Başarılı", "Şirket başarıyla kaydedildi.", NotificationType.Success);
ClearForm();
}

await Task.Delay(1200);

if (this.InvokeRequired)
{
this.Invoke(new Action(() => this.Close()));
}
else
{
this.Close();
}
}
catch (BusinessException ex)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
Notifier.Show("İş Kuralı Hatası", ex.Message, NotificationType.Error);
}));
}
else
{
Notifier.Show("İş Kuralı Hatası", ex.Message, NotificationType.Error);
}
}
catch (ValidationException ex)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
Notifier.Show("Doğrulama Hatası", ex.Message, NotificationType.Error);
}));
}
else
{
Notifier.Show("Doğrulama Hatası", ex.Message, NotificationType.Error);
}
}
catch (DatabaseQueryException ex)
{
string errorMessage = ex.Message;
if (ex.InnerException != null && !ex.Message.Contains(ex.InnerException.Message))
{
errorMessage += $"\n\nDetay: {ex.InnerException.Message}";
}

if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
Notifier.Show("Veritabanı Hatası", errorMessage, NotificationType.Error);
}));
}
else
{
Notifier.Show("Veritabanı Hatası", errorMessage, NotificationType.Error);
}
}
catch (Exception ex)
{
string errorMessage = $"Beklenmeyen bir hata oluştu: {ex.Message}";
if (ex.InnerException != null)
{
errorMessage += $"\n\nDetay: {ex.InnerException.Message}";
}

if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
Notifier.Show("Hata", errorMessage, NotificationType.Error);
}));
}
else
{
Notifier.Show("Hata", errorMessage, NotificationType.Error);
}
}
finally
{
if (!this.IsDisposed)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
btnSaveCompany.Enabled = true;
btnSaveCompany.Text = "Kaydet";
_isProcessing = false;
}));
}
else
{
btnSaveCompany.Enabled = true;
btnSaveCompany.Text = "Kaydet";
_isProcessing = false;
}
}
}
}

private void ClearForm()
{
txtName.Text = string.Empty;
txtTaxNumber.Text = string.Empty;
txtAddress.Text = string.Empty;
txtPhone.Text = string.Empty;
}
}
}