feat: 添加 Exception 服务

This commit is contained in:
zhangpeihang 2022-01-26 15:38:35 +08:00
parent 25197d9cb3
commit 51ff64fb2a
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 ExceptionService : IException
{
private IDbContextFactory<BootstrapAdminContext> DbFactory;
/// <summary>
///
/// </summary>
/// <param name="dbFactory"></param>
public ExceptionService(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<Error> Items, int ItemsCount) GetAll(string? searchText, ExceptionFilter filter, int pageIndex, int pageItems, List<string> sortList)
{
using var dbcontext = DbFactory.CreateDbContext();
var items = dbcontext.Set<Error>();
if (!string.IsNullOrEmpty(searchText))
{
items.Where(s => s.Message!.Contains(searchText) || s.StackTrace!.Contains(searchText) || s.ErrorPage!.Contains(searchText));
}
if (!string.IsNullOrEmpty(filter.Category))
{
items.Where(s => s.Category!.Contains(filter.Category));
}
if (!string.IsNullOrEmpty(filter.UserId))
{
items.Where(s => s.UserId!.Contains(filter.UserId));
}
if (!string.IsNullOrEmpty(filter.ErrorPage))
{
items.Where(s => s.ErrorPage!.Contains(filter.ErrorPage));
}
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="exception"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Log(Error exception)
{
using var dbcontext = DbFactory.CreateDbContext();
dbcontext.Add(exception);
return dbcontext.SaveChanges() > 0;
}
}