feat: 添加 Trace 服务

This commit is contained in:
zhangpeihang 2022-01-26 15:39:09 +08:00
parent 51ff64fb2a
commit 88268b450b
1 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,88 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BootstrapAdmin.DataAccess.EFCore.Services;
/// <summary>
///
/// </summary>
public class TraceService : ITrace
{
private IDbContextFactory<BootstrapAdminContext> DbFactory;
/// <summary>
///
/// </summary>
/// <param name="dbFactory"></param>
public TraceService(IDbContextFactory<BootstrapAdminContext> dbFactory) => DbFactory = dbFactory;
/// <summary>
///
/// </summary>
/// <param name="searchText"></param>
/// <param name="filter"></param>
/// <param name="pageIndex"></param>
/// <param name="pageItems"></param>
/// <param name="sortList"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public (IEnumerable<Trace> Items, int ItemsCount) GetAll(string? searchText, TraceFilter filter, int pageIndex, int pageItems, List<string> sortList)
{
using var dbcontext = DbFactory.CreateDbContext();
var items = dbcontext.Set<Trace>();
if (!string.IsNullOrEmpty(searchText))
{
items.Where(s => s.UserName!.Contains(searchText) || s.Ip!.Contains(searchText) || s.RequestUrl!.Contains(searchText));
}
if (!string.IsNullOrEmpty(filter.UserName))
{
items.Where(s => s.UserName!.Contains(filter.UserName));
}
if (!string.IsNullOrEmpty(filter.Ip))
{
items.Where(s => s.Ip!.Contains(filter.Ip));
}
if (!string.IsNullOrEmpty(filter.RequestUrl))
{
items.Where(s => s.RequestUrl!.Contains(filter.RequestUrl));
}
items.Where(s => s.LogTime >= filter.Star && s.LogTime <= filter.End);
if (sortList.Any())
{
items.Sort(sortList);
}
else
{
items.OrderByDescending(s => s.LogTime);
}
var data = items.Take(pageItems).Skip(pageItems * (pageIndex - 1)).AsNoTracking().ToList();
return (data, items.Count());
}
/// <summary>
///
/// </summary>
/// <param name="trace"></param>
/// <exception cref="NotImplementedException"></exception>
public void Log(Trace trace)
{
using var dbcontext = DbFactory.CreateDbContext();
dbcontext.Add(trace);
dbcontext.SaveChanges();
}
}