Enhance law management functionality and API

Updated ILawApplication with new methods for activating and deactivating laws by type. Added UpsertLaw method for creating and updating laws. Modified LawViewModel and EditLaw to include LawType. Enhanced LawApplication methods to support new features and updated GetLawByType to return default laws when none exist. Introduced LawController with endpoints for law operations.
This commit is contained in:
MahanCh
2025-09-14 10:47:31 +03:30
parent e1dfd8c8e1
commit 18e559f1ae
4 changed files with 220 additions and 8 deletions

View File

@@ -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<LawViewModel> GetList();
Task<LawViewModel> GetLawWithItems(long id);
Task<LawViewModel> GetLawByType(LawType type);
OperationResult UpsertLaw(EditLaw command);
}
public enum LawType
{
/// <summary>

View File

@@ -10,6 +10,7 @@ namespace CompanyManagment.App.Contracts.Law
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
public List<LawItemViewModel> 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<LawItemViewModel> Items { get; set; }
public LawType Type { get; set; }
}
}

View File

@@ -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<LawViewModel> 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<LawItemViewModel>()
});
}
}
return dbLaws;
}
private string GetDefaultTitleForLawType(LawType lawType)
{
return lawType switch
{
LawType.Register => "قوانین ثبت نام",
_ => $"قوانین {lawType}"
};
}
public async Task<LawViewModel> 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<LawViewModel> 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<LawItemViewModel>()
};
}
return lawViewModel;
}
}

View File

@@ -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;
}
/// <summary>
/// لیست قوانین بر اساس انواع تعریف شده در enum
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult<List<LawViewModel>> GetList()
{
return _lawApplication.GetList();
}
/// <summary>
/// جزئیات قانون بر اساس نوع
/// </summary>
/// <param name="type">نوع قانون</param>
/// <returns></returns>
[HttpGet("by-type/{type}")]
public async Task<ActionResult<LawViewModel>> GetLawByType(LawType type)
{
return await _lawApplication.GetLawByType(type);
}
/// <summary>
/// ایجاد یا ویرایش قانون بر اساس نوع
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
[HttpPut("upsert")]
public ActionResult<OperationResult> UpsertLaw([FromBody] EditLaw command)
{
var result = _lawApplication.UpsertLaw(command);
return result;
}
/// <summary>
/// فعال‌سازی قانون
/// </summary>
/// <param name="type">نوع قانون</param>
/// <returns></returns>
[HttpPatch("{type}/activate")]
public ActionResult<OperationResult> Activate(LawType type)
{
var result = _lawApplication.ActivateByType(type);
return result;
}
/// <summary>
/// غیرفعال‌سازی قانون
/// </summary>
/// <param name="type">نوع قانون</param>
/// <returns></returns>
[HttpPatch("{type}/deactivate")]
public ActionResult<OperationResult> Deactivate(LawType type)
{
var result = _lawApplication.DeactivateByType(type);
return result;
}
}