diff --git a/CompanyManagment.App.Contracts/Law/ILawApplication.cs b/CompanyManagment.App.Contracts/Law/ILawApplication.cs index 86033679..147d4bd8 100644 --- a/CompanyManagment.App.Contracts/Law/ILawApplication.cs +++ b/CompanyManagment.App.Contracts/Law/ILawApplication.cs @@ -10,13 +10,15 @@ namespace CompanyManagment.App.Contracts.Law OperationResult Edit(EditLaw command); OperationResult Activate(long id); OperationResult Deactivate(long id); + OperationResult ActivateByType(LawType type); + OperationResult DeactivateByType(LawType type); EditLaw GetDetails(long id); List GetList(); Task GetLawWithItems(long id); - Task GetLawByType(LawType type); - + OperationResult UpsertLaw(EditLaw command); } + public enum LawType { /// diff --git a/CompanyManagment.App.Contracts/Law/LawViewModel.cs b/CompanyManagment.App.Contracts/Law/LawViewModel.cs index 06a85b18..48f5ee46 100644 --- a/CompanyManagment.App.Contracts/Law/LawViewModel.cs +++ b/CompanyManagment.App.Contracts/Law/LawViewModel.cs @@ -10,6 +10,7 @@ namespace CompanyManagment.App.Contracts.Law public bool IsActive { get; set; } public DateTime CreatedAt { get; set; } public List Items { get; set; } + public LawType Type { get; set; } } public class LawItemViewModel @@ -33,6 +34,7 @@ namespace CompanyManagment.App.Contracts.Law public long Id { get; set; } public string Title { get; set; } public List Items { get; set; } - + public LawType Type { get; set; } + } } diff --git a/CompanyManagment.Application/LawApplication.cs b/CompanyManagment.Application/LawApplication.cs index 2bff1518..0797cc2a 100644 --- a/CompanyManagment.Application/LawApplication.cs +++ b/CompanyManagment.Application/LawApplication.cs @@ -1,6 +1,7 @@ using _0_Framework.Application; using Company.Domain.LawAgg; using CompanyManagment.App.Contracts.Law; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -37,7 +38,6 @@ public class LawApplication : ILawApplication orderNumber++; } - _lawRepository.Create(law); _lawRepository.SaveChanges(); @@ -59,7 +59,7 @@ public class LawApplication : ILawApplication var orderNumber = 1; var lawItems = command.Items.Select(x => { - var res= new LawItem(x.Header, x.Details, orderNumber); + var res = new LawItem(x.Header, x.Details, orderNumber); orderNumber++; return res; }).ToList(); @@ -72,6 +72,54 @@ public class LawApplication : ILawApplication return operation.Succcedded(); } + public OperationResult UpsertLaw(EditLaw command) + { + var operation = new OperationResult(); + + // Validate items + if (command.Items == null || command.Items.Count == 0) + { + return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود"); + } + + // Check if law exists by type + var existingLaw = _lawRepository.Get().FirstOrDefault(x => x.Type == command.Type); + + if (existingLaw == null) + { + // If law doesn't exist, create a new one + var law = new Law(command.Title, command.Type); + + var orderNumber = 1; + foreach (var item in command.Items) + { + law.AddItem(item.Header, item.Details, orderNumber); + orderNumber++; + } + + _lawRepository.Create(law); + _lawRepository.SaveChanges(); + } + else + { + // If law exists, update it + var orderNumber = 1; + var lawItems = command.Items.Select(x => + { + var res = new LawItem(x.Header, x.Details, orderNumber); + orderNumber++; + return res; + }).ToList(); + + existingLaw.Edit(command.Title); + existingLaw.SetItem(lawItems); + + _lawRepository.SaveChanges(); + } + + return operation.Succcedded(); + } + public OperationResult Activate(long id) { var operation = new OperationResult(); @@ -98,6 +146,39 @@ public class LawApplication : ILawApplication return operation.Succcedded(); } + public OperationResult ActivateByType(LawType type) + { + var operation = new OperationResult(); + var law = _lawRepository.Get().FirstOrDefault(x => x.Type == type); + + if (law == null) + { + // If law doesn't exist, create a new active one with default values + var newLaw = new Law(GetDefaultTitleForLawType(type), type); + newLaw.Activate(); + _lawRepository.Create(newLaw); + _lawRepository.SaveChanges(); + return operation.Succcedded(); + } + + law.Activate(); + _lawRepository.SaveChanges(); + return operation.Succcedded(); + } + + public OperationResult DeactivateByType(LawType type) + { + var operation = new OperationResult(); + var law = _lawRepository.Get().FirstOrDefault(x => x.Type == type); + + if (law == null) + return operation.Failed("قانون مورد نظر یافت نشد"); + + law.Deactivate(); + _lawRepository.SaveChanges(); + return operation.Succcedded(); + } + public EditLaw GetDetails(long id) { var law = _lawRepository.Get(id); @@ -105,6 +186,7 @@ public class LawApplication : ILawApplication { Id = law.id, Title = law.Title, + Type = law.Type, Items = law.Items.OrderBy(x => x.OrderNumber).Select(x => new LawItemViewModel { Header = x.Header, @@ -115,25 +197,62 @@ public class LawApplication : ILawApplication public List GetList() { - return _lawRepository.Get() + // Get all laws from database + var dbLaws = _lawRepository.Get() .Select(x => new LawViewModel { Id = x.id, Title = x.Title, IsActive = x.IsActive, - CreatedAt = x.CreationDate + CreatedAt = x.CreationDate, + Type = x.Type }).ToList(); + + // Create a set of existing law types + var existingTypes = dbLaws.Select(x => x.Type).ToHashSet(); + + // Add placeholder laws for any missing enum values + foreach (LawType lawType in Enum.GetValues(typeof(LawType))) + { + if (!existingTypes.Contains(lawType)) + { + dbLaws.Add(new LawViewModel + { + Id = 0, // Indicates it doesn't exist in the database yet + Title = GetDefaultTitleForLawType(lawType), + IsActive = false, + CreatedAt = DateTime.Now, + Type = lawType, + Items = new List() + }); + } + } + + return dbLaws; + } + + private string GetDefaultTitleForLawType(LawType lawType) + { + return lawType switch + { + LawType.Register => "قوانین ثبت نام", + _ => $"قوانین {lawType}" + }; } public async Task GetLawWithItems(long id) { var law = await _lawRepository.GetWithItems(id); + if (law == null) + return null; + return new LawViewModel { Id = law.id, Title = law.Title, IsActive = law.IsActive, CreatedAt = law.CreationDate, + Type = law.Type, Items = law.Items.OrderBy(x=>x.OrderNumber).Select(x => new LawItemViewModel { Header = x.Header, @@ -144,6 +263,22 @@ public class LawApplication : ILawApplication public async Task GetLawByType(LawType type) { - return await _lawRepository.GetByType(type); + var lawViewModel = await _lawRepository.GetByType(type); + + // If no law exists for this type, return a default empty law + if (lawViewModel == null) + { + return new LawViewModel + { + Id = 0, + Title = GetDefaultTitleForLawType(type), + IsActive = false, + CreatedAt = DateTime.Now, + Type = type, + Items = new List() + }; + } + + return lawViewModel; } } \ No newline at end of file diff --git a/ServiceHost/Areas/Admin/Controllers/LawController.cs b/ServiceHost/Areas/Admin/Controllers/LawController.cs new file mode 100644 index 00000000..d2ade21a --- /dev/null +++ b/ServiceHost/Areas/Admin/Controllers/LawController.cs @@ -0,0 +1,73 @@ +using _0_Framework.Application; +using CompanyManagment.App.Contracts.Law; +using Microsoft.AspNetCore.Mvc; +using ServiceHost.BaseControllers; + +namespace ServiceHost.Areas.Admin.Controllers; + +public class LawController : AdminBaseController +{ + private readonly ILawApplication _lawApplication; + + public LawController(ILawApplication lawApplication) + { + _lawApplication = lawApplication; + } + + /// + /// لیست قوانین بر اساس انواع تعریف شده در enum + /// + /// + [HttpGet] + public ActionResult> GetList() + { + return _lawApplication.GetList(); + } + + /// + /// جزئیات قانون بر اساس نوع + /// + /// نوع قانون + /// + [HttpGet("by-type/{type}")] + public async Task> GetLawByType(LawType type) + { + return await _lawApplication.GetLawByType(type); + } + + /// + /// ایجاد یا ویرایش قانون بر اساس نوع + /// + /// + /// + [HttpPut("upsert")] + public ActionResult UpsertLaw([FromBody] EditLaw command) + { + var result = _lawApplication.UpsertLaw(command); + return result; + } + + /// + /// فعال‌سازی قانون + /// + /// نوع قانون + /// + [HttpPatch("{type}/activate")] + public ActionResult Activate(LawType type) + { + var result = _lawApplication.ActivateByType(type); + return result; + } + + /// + /// غیرفعال‌سازی قانون + /// + /// نوع قانون + /// + [HttpPatch("{type}/deactivate")] + public ActionResult Deactivate(LawType type) + { + var result = _lawApplication.DeactivateByType(type); + return result; + } +}