Started working on listing page
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
namespace HopFrame.Core.Services;
|
||||
|
||||
public interface ITableManager {
|
||||
public Task<IEnumerable<object>> LoadPage(int page, int perPage = 25);
|
||||
public IQueryable<object> LoadPage(int page, int perPage = 20);
|
||||
public (IEnumerable<object>, int) Search(string searchTerm, int page = 0, int perPage = 20);
|
||||
public int TotalPages(int perPage = 20);
|
||||
public Task DeleteItem(object item);
|
||||
}
|
||||
@@ -15,7 +15,7 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
|
||||
|
||||
public TableConfig? GetTable(string tableName) {
|
||||
foreach (var context in config.Contexts) {
|
||||
var table = context.Tables.FirstOrDefault(table => table.PropertyName == tableName);
|
||||
var table = context.Tables.FirstOrDefault(table => table.PropertyName.Equals(tableName, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (table is not null)
|
||||
return table;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
|
||||
if (dbContext is null) return null;
|
||||
|
||||
var type = typeof(TableManager<>).MakeGenericType(table.TableType);
|
||||
return Activator.CreateInstance(type, dbContext) as ITableManager;
|
||||
return Activator.CreateInstance(type, dbContext, table) as ITableManager;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -1,15 +1,49 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using HopFrame.Core.Config;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Services.Implementations;
|
||||
|
||||
internal sealed class TableManager<TModel>(DbContext context) : ITableManager where TModel : class {
|
||||
internal sealed class TableManager<TModel>(DbContext context, TableConfig config) : ITableManager where TModel : class {
|
||||
|
||||
public async Task<IEnumerable<object>> LoadPage(int page, int perPage = 25) {
|
||||
public IQueryable<object> LoadPage(int page, int perPage = 20) {
|
||||
var table = context.Set<TModel>();
|
||||
return await table
|
||||
return table
|
||||
.Skip(page * perPage)
|
||||
.Take(perPage)
|
||||
.ToArrayAsync();
|
||||
.Take(perPage);
|
||||
}
|
||||
|
||||
public (IEnumerable<object>, int) Search(string searchTerm, int page = 0, int perPage = 20) {
|
||||
var table = context.Set<TModel>();
|
||||
var all = table
|
||||
.AsEnumerable()
|
||||
.Where(item => ItemSearched(item, searchTerm))
|
||||
.ToList();
|
||||
|
||||
return (all.Skip(page * perPage).Take(perPage), (int)Math.Ceiling(all.Count / (double)perPage));
|
||||
}
|
||||
|
||||
public int TotalPages(int perPage = 20) {
|
||||
var table = context.Set<TModel>();
|
||||
return (int)Math.Ceiling(table.Count() / (double)perPage);
|
||||
}
|
||||
|
||||
public async Task DeleteItem(object item) {
|
||||
var table = context.Set<TModel>();
|
||||
table.Remove((item as TModel)!);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private bool ItemSearched(TModel item, string searchTerm) {
|
||||
foreach (var property in config.Properties) {
|
||||
if (!property.Searchable) continue;
|
||||
var value = property.Info.GetValue(item);
|
||||
if (value is null) continue;
|
||||
|
||||
var strValue = value.ToString();
|
||||
if (strValue?.Contains(searchTerm) == true)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user