Add search functionality to law retrieval with filtering options
This commit is contained in:
@@ -10,5 +10,6 @@ namespace Company.Domain.LawAgg
|
||||
Task<Law> GetWithItems(long id);
|
||||
Task<List<Law>> GetActive();
|
||||
Task<LawViewModel> GetByType(LawType type);
|
||||
Task<List<LawViewModel>> GetList(LawSearchModel searchModel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ namespace Company.Domain.LawAgg
|
||||
public string Title { get; private set; }
|
||||
public bool IsActive { get; private set; }
|
||||
public List<LawItem> Items { get; private set; }
|
||||
|
||||
public LawType Type { get; private set; }
|
||||
|
||||
public Law(string title, LawType lawType)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using _0_Framework.Application;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.AccessControl;
|
||||
using System.Threading.Tasks;
|
||||
using CompanyManagment.App.Contracts.Workshop;
|
||||
|
||||
namespace CompanyManagment.App.Contracts.Law
|
||||
{
|
||||
@@ -13,12 +15,18 @@ namespace CompanyManagment.App.Contracts.Law
|
||||
OperationResult ActivateByType(LawType type);
|
||||
OperationResult DeactivateByType(LawType type);
|
||||
EditLaw GetDetails(long id);
|
||||
List<LawViewModel> GetList();
|
||||
Task<List<LawViewModel>> GetList(LawSearchModel searchModel);
|
||||
Task<LawViewModel> GetLawWithItems(long id);
|
||||
Task<LawViewModel> GetLawByType(LawType type);
|
||||
OperationResult UpsertLaw(EditLaw command);
|
||||
}
|
||||
|
||||
|
||||
public class LawSearchModel
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string Text { get; set; }
|
||||
}
|
||||
|
||||
public enum LawType
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -195,40 +195,12 @@ public class LawApplication : ILawApplication
|
||||
};
|
||||
}
|
||||
|
||||
public List<LawViewModel> GetList()
|
||||
public async Task<List<LawViewModel>> GetList(LawSearchModel searchModel)
|
||||
{
|
||||
// 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,
|
||||
Type = x.Type
|
||||
}).ToList();
|
||||
// Get filtered laws from database
|
||||
return await _lawRepository.GetList(searchModel);
|
||||
|
||||
// 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)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@@ -42,4 +43,58 @@ public class LawRepository:RepositoryBase<long,Law>,ILawRepository
|
||||
}).ToList()
|
||||
}).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<List<LawViewModel>> GetList(LawSearchModel searchModel)
|
||||
{
|
||||
var query = _context.Laws.Include(x => x.Items).AsQueryable();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(searchModel.Title))
|
||||
query = query.Where(x => x.Title.Contains(searchModel.Title));
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(searchModel.Text))
|
||||
query = query.Where(x => x.Title.Contains(searchModel.Text) || x.Items.Any(i => i.Header.Contains(searchModel.Text) || i.Details.Contains(searchModel.Text)));
|
||||
|
||||
var list = await query.Select(x => new LawViewModel
|
||||
{
|
||||
Id = x.id,
|
||||
Title = x.Title,
|
||||
IsActive = x.IsActive,
|
||||
CreatedAt = x.CreationDate,
|
||||
Type = x.Type
|
||||
}).ToListAsync();
|
||||
|
||||
// Create a set of existing law types
|
||||
var existingTypes = list.Select(x => x.Type).ToHashSet();
|
||||
if (string.IsNullOrWhiteSpace(searchModel.Text) && string.IsNullOrWhiteSpace(searchModel.Title))
|
||||
{
|
||||
// Add placeholder laws for any missing enum values
|
||||
foreach (LawType lawType in Enum.GetValues(typeof(LawType)))
|
||||
{
|
||||
if (!existingTypes.Contains(lawType))
|
||||
{
|
||||
list.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 list;
|
||||
}
|
||||
private string GetDefaultTitleForLawType(LawType lawType)
|
||||
{
|
||||
return lawType switch
|
||||
{
|
||||
LawType.Register => "قوانین ثبت نام",
|
||||
_ => $"قوانین {lawType}"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,9 @@ public class LawController : AdminBaseController
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public ActionResult<List<LawViewModel>> GetList()
|
||||
public async Task<ActionResult<List<LawViewModel>>> GetList(LawSearchModel searchModel)
|
||||
{
|
||||
return _lawApplication.GetList();
|
||||
return await _lawApplication.GetList(searchModel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user