diff --git a/OpsFlow/Core/Models/Company.cs b/OpsFlow/Core/Models/Company.cs index fcff0ca..e9dc2af 100644 --- a/OpsFlow/Core/Models/Company.cs +++ b/OpsFlow/Core/Models/Company.cs @@ -8,6 +8,7 @@ public class Company { [Key] [Column("id")] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Column("company_name")] @@ -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; } diff --git a/OpsFlow/Services/Helpers/CompanyValidationHelper.cs b/OpsFlow/Services/Helpers/CompanyValidationHelper.cs new file mode 100644 index 0000000..d6646e9 --- /dev/null +++ b/OpsFlow/Services/Helpers/CompanyValidationHelper.cs @@ -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); + } + } +} + diff --git a/OpsFlow/Services/Implementations/CompanyService.cs b/OpsFlow/Services/Implementations/CompanyService.cs index f529688..04c3615 100644 --- a/OpsFlow/Services/Implementations/CompanyService.cs +++ b/OpsFlow/Services/Implementations/CompanyService.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Npgsql; using OpsFlow.Core.Exceptions; using OpsFlow.Core.Models; using OpsFlow.Data.Context; @@ -58,6 +59,74 @@ public async Task> 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)); + } } } diff --git a/OpsFlow/Services/Interfaces/ICompanyService.cs b/OpsFlow/Services/Interfaces/ICompanyService.cs index cd6e3d2..ccc6f67 100644 --- a/OpsFlow/Services/Interfaces/ICompanyService.cs +++ b/OpsFlow/Services/Interfaces/ICompanyService.cs @@ -7,6 +7,8 @@ public interface ICompanyService List GetAllCompanies(); Task> GetAllCompaniesAsync(); Company? GetCompanyById(int id); + void RegisterCompany(Company company); + Task RegisterCompanyAsync(Company company); } } diff --git a/OpsFlow/UI/Forms/Auth/LoginForm.cs b/OpsFlow/UI/Forms/Auth/LoginForm.cs index 6a02d32..9cccda4 100644 --- a/OpsFlow/UI/Forms/Auth/LoginForm.cs +++ b/OpsFlow/UI/Forms/Auth/LoginForm.cs @@ -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 { diff --git a/OpsFlow/UI/Forms/Management/CompanyRegisterForm.Designer.cs b/OpsFlow/UI/Forms/Management/CompanyRegisterForm.Designer.cs index bc2f228..71afc72 100644 --- a/OpsFlow/UI/Forms/Management/CompanyRegisterForm.Designer.cs +++ b/OpsFlow/UI/Forms/Management/CompanyRegisterForm.Designer.cs @@ -207,7 +207,7 @@ private void InitializeComponent() btnSaveCompany.Size = new Size(190, 43); btnSaveCompany.TabIndex = 16; btnSaveCompany.Text = "Kaydet"; - btnSaveCompany.Click += btnSaveCompany_Click; + btnSaveCompany.Click += BtnSaveCompany_Click; // // label2 // diff --git a/OpsFlow/UI/Forms/Management/CompanyRegisterForm.cs b/OpsFlow/UI/Forms/Management/CompanyRegisterForm.cs index bdf04e9..901363e 100644 --- a/OpsFlow/UI/Forms/Management/CompanyRegisterForm.cs +++ b/OpsFlow/UI/Forms/Management/CompanyRegisterForm.cs @@ -1,4 +1,9 @@ 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; @@ -6,33 +11,196 @@ 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; } } } \ No newline at end of file