From 88268b450bc8f1b37f1793455fa8b8d16c86647c Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Wed, 26 Jan 2022 15:39:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Trace=20=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/TraceService.cs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/TraceService.cs diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/TraceService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/TraceService.cs new file mode 100644 index 00000000..ed0cf1f6 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/TraceService.cs @@ -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; + +/// +/// +/// +public class TraceService : ITrace +{ + private IDbContextFactory DbFactory; + + /// + /// + /// + /// + public TraceService(IDbContextFactory dbFactory) => DbFactory = dbFactory; + + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public (IEnumerable Items, int ItemsCount) GetAll(string? searchText, TraceFilter filter, int pageIndex, int pageItems, List sortList) + { + using var dbcontext = DbFactory.CreateDbContext(); + + var items = dbcontext.Set(); + + 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()); + } + + /// + /// + /// + /// + /// + public void Log(Trace trace) + { + using var dbcontext = DbFactory.CreateDbContext(); + + dbcontext.Add(trace); + dbcontext.SaveChanges(); + } +}