测试
This commit is contained in:
28
MinimalAPI/Repositories/CategoryRepository.cs
Normal file
28
MinimalAPI/Repositories/CategoryRepository.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using HardwarePerformance.Data;
|
||||
using HardwarePerformance.Models;
|
||||
|
||||
namespace HardwarePerformance.Repositories
|
||||
{
|
||||
public class CategoryRepository : Repository<Category>, ICategoryRepository
|
||||
{
|
||||
public CategoryRepository(AppDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Category?> GetByNameAsync(string name)
|
||||
{
|
||||
return await _dbSet.FirstOrDefaultAsync(c => c.Name == name);
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(int id)
|
||||
{
|
||||
return await _dbSet.AnyAsync(c => c.Id == id);
|
||||
}
|
||||
|
||||
public async Task<bool> NameExistsAsync(string name)
|
||||
{
|
||||
return await _dbSet.AnyAsync(c => c.Name == name);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
MinimalAPI/Repositories/ICategoryRepository.cs
Normal file
11
MinimalAPI/Repositories/ICategoryRepository.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using HardwarePerformance.Models;
|
||||
|
||||
namespace HardwarePerformance.Repositories
|
||||
{
|
||||
public interface ICategoryRepository : IRepository<Category>
|
||||
{
|
||||
Task<Category?> GetByNameAsync(string name);
|
||||
Task<bool> ExistsAsync(int id);
|
||||
Task<bool> NameExistsAsync(string name);
|
||||
}
|
||||
}
|
||||
18
MinimalAPI/Repositories/IProductRepository.cs
Normal file
18
MinimalAPI/Repositories/IProductRepository.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using HardwarePerformance.Models;
|
||||
|
||||
namespace HardwarePerformance.Repositories
|
||||
{
|
||||
public interface IProductRepository : IRepository<Product>
|
||||
{
|
||||
Task<IEnumerable<Product>> GetByCategoryAsync(int categoryId);
|
||||
Task<IEnumerable<Product>> GetByCategoryAsync(int categoryId, int page, int pageSize, string sortBy = "CurrentRank", string sortOrder = "asc");
|
||||
Task<IEnumerable<Product>> SearchAsync(string query);
|
||||
Task<IEnumerable<Product>> SearchAsync(string query, int? categoryId = null, string? manufacturer = null, int? minScore = null, int? maxScore = null, int page = 1, int pageSize = 20);
|
||||
Task<IEnumerable<Product>> FilterAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year);
|
||||
Task<IEnumerable<Product>> FilterAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year, int page, int pageSize, string sortBy = "CurrentRank", string sortOrder = "asc");
|
||||
Task<int> CountAsync(int? categoryId = null);
|
||||
Task<int> CountSearchResultsAsync(string query, int? categoryId = null, string? manufacturer = null, int? minScore = null, int? maxScore = null);
|
||||
Task<int> CountFilterResultsAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year);
|
||||
Task<Category?> GetCategoryByProductIdAsync(int productId);
|
||||
}
|
||||
}
|
||||
16
MinimalAPI/Repositories/IRepository.cs
Normal file
16
MinimalAPI/Repositories/IRepository.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Linq.Expressions;
|
||||
using HardwarePerformance.Models;
|
||||
|
||||
namespace HardwarePerformance.Repositories
|
||||
{
|
||||
public interface IRepository<T> where T : class
|
||||
{
|
||||
Task<T?> GetByIdAsync(int id);
|
||||
Task<IEnumerable<T>> GetAllAsync();
|
||||
Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate);
|
||||
Task<T> AddAsync(T entity);
|
||||
Task UpdateAsync(T entity);
|
||||
Task DeleteAsync(T entity);
|
||||
Task<int> CountAsync(Expression<Func<T, bool>>? predicate = null);
|
||||
}
|
||||
}
|
||||
265
MinimalAPI/Repositories/ProductRepository.cs
Normal file
265
MinimalAPI/Repositories/ProductRepository.cs
Normal file
@@ -0,0 +1,265 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using HardwarePerformance.Data;
|
||||
using HardwarePerformance.Models;
|
||||
|
||||
namespace HardwarePerformance.Repositories
|
||||
{
|
||||
public class ProductRepository : Repository<Product>, IProductRepository
|
||||
{
|
||||
public ProductRepository(AppDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> GetByCategoryAsync(int categoryId)
|
||||
{
|
||||
return await _dbSet
|
||||
.Where(p => p.CategoryId == categoryId)
|
||||
.OrderBy(p => p.CurrentRank)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> GetByCategoryAsync(int categoryId, int page, int pageSize, string sortBy = "CurrentRank", string sortOrder = "asc")
|
||||
{
|
||||
var query = _dbSet.Where(p => p.CategoryId == categoryId);
|
||||
|
||||
// 排序
|
||||
query = sortBy.ToLower() switch
|
||||
{
|
||||
"name" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Name) : query.OrderBy(p => p.Name),
|
||||
"manufacturer" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Manufacturer) : query.OrderBy(p => p.Manufacturer),
|
||||
"releasedate" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.ReleaseDate) : query.OrderBy(p => p.ReleaseDate),
|
||||
"price" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Price) : query.OrderBy(p => p.Price),
|
||||
_ => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.CurrentRank) : query.OrderBy(p => p.CurrentRank)
|
||||
};
|
||||
|
||||
return await query
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> SearchAsync(string query)
|
||||
{
|
||||
return await _dbSet
|
||||
.Where(p =>
|
||||
p.Name.Contains(query) ||
|
||||
p.Model.Contains(query) ||
|
||||
p.Manufacturer.Contains(query))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> SearchAsync(string query, int? categoryId = null, string? manufacturer = null, int? minScore = null, int? maxScore = null, int page = 1, int pageSize = 20)
|
||||
{
|
||||
var queryable = _dbSet.AsQueryable();
|
||||
|
||||
if (categoryId.HasValue)
|
||||
{
|
||||
queryable = queryable.Where(p => p.CategoryId == categoryId.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(manufacturer))
|
||||
{
|
||||
queryable = queryable.Where(p => p.Manufacturer == manufacturer);
|
||||
}
|
||||
|
||||
if (minScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
queryable = queryable.Where(p => (100 - p.CurrentRank) >= minScore.Value);
|
||||
}
|
||||
|
||||
if (maxScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
queryable = queryable.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
queryable = queryable.Where(p =>
|
||||
p.Name.Contains(query) ||
|
||||
p.Model.Contains(query) ||
|
||||
p.Manufacturer.Contains(query));
|
||||
}
|
||||
|
||||
return await queryable
|
||||
.OrderBy(p => p.CurrentRank)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> FilterAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year)
|
||||
{
|
||||
var query = _dbSet.AsQueryable();
|
||||
|
||||
if (categoryId.HasValue)
|
||||
{
|
||||
query = query.Where(p => p.CategoryId == categoryId.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(manufacturer))
|
||||
{
|
||||
query = query.Where(p => p.Manufacturer == manufacturer);
|
||||
}
|
||||
|
||||
if (minScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
query = query.Where(p => (100 - p.CurrentRank) >= minScore.Value);
|
||||
}
|
||||
|
||||
if (maxScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
query = query.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
|
||||
}
|
||||
|
||||
if (year.HasValue)
|
||||
{
|
||||
query = query.Where(p => p.ReleaseDate.Year == year.Value);
|
||||
}
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> FilterAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year, int page, int pageSize, string sortBy = "CurrentRank", string sortOrder = "asc")
|
||||
{
|
||||
var query = _dbSet.AsQueryable();
|
||||
|
||||
if (categoryId.HasValue)
|
||||
{
|
||||
query = query.Where(p => p.CategoryId == categoryId.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(manufacturer))
|
||||
{
|
||||
query = query.Where(p => p.Manufacturer == manufacturer);
|
||||
}
|
||||
|
||||
if (minScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
query = query.Where(p => (100 - p.CurrentRank) >= minScore.Value);
|
||||
}
|
||||
|
||||
if (maxScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
query = query.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
|
||||
}
|
||||
|
||||
if (year.HasValue)
|
||||
{
|
||||
query = query.Where(p => p.ReleaseDate.Year == year.Value);
|
||||
}
|
||||
|
||||
// 排序
|
||||
query = sortBy.ToLower() switch
|
||||
{
|
||||
"name" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Name) : query.OrderBy(p => p.Name),
|
||||
"manufacturer" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Manufacturer) : query.OrderBy(p => p.Manufacturer),
|
||||
"releasedate" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.ReleaseDate) : query.OrderBy(p => p.ReleaseDate),
|
||||
"price" => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.Price) : query.OrderBy(p => p.Price),
|
||||
_ => sortOrder.ToLower() == "desc" ? query.OrderByDescending(p => p.CurrentRank) : query.OrderBy(p => p.CurrentRank)
|
||||
};
|
||||
|
||||
return await query
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<int> CountAsync(int? categoryId = null)
|
||||
{
|
||||
var query = _dbSet.AsQueryable();
|
||||
|
||||
if (categoryId.HasValue)
|
||||
{
|
||||
query = query.Where(p => p.CategoryId == categoryId.Value);
|
||||
}
|
||||
|
||||
return await query.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<int> CountSearchResultsAsync(string query, int? categoryId = null, string? manufacturer = null, int? minScore = null, int? maxScore = null)
|
||||
{
|
||||
var queryable = _dbSet.AsQueryable();
|
||||
|
||||
if (categoryId.HasValue)
|
||||
{
|
||||
queryable = queryable.Where(p => p.CategoryId == categoryId.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(manufacturer))
|
||||
{
|
||||
queryable = queryable.Where(p => p.Manufacturer == manufacturer);
|
||||
}
|
||||
|
||||
if (minScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
queryable = queryable.Where(p => (100 - p.CurrentRank) >= minScore.Value);
|
||||
}
|
||||
|
||||
if (maxScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
queryable = queryable.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
queryable = queryable.Where(p =>
|
||||
p.Name.Contains(query) ||
|
||||
p.Model.Contains(query) ||
|
||||
p.Manufacturer.Contains(query));
|
||||
}
|
||||
|
||||
return await queryable.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<int> CountFilterResultsAsync(int? categoryId, string? manufacturer, int? minScore, int? maxScore, int? year)
|
||||
{
|
||||
var query = _dbSet.AsQueryable();
|
||||
|
||||
if (categoryId.HasValue)
|
||||
{
|
||||
query = query.Where(p => p.CategoryId == categoryId.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(manufacturer))
|
||||
{
|
||||
query = query.Where(p => p.Manufacturer == manufacturer);
|
||||
}
|
||||
|
||||
if (minScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
query = query.Where(p => (100 - p.CurrentRank) >= minScore.Value);
|
||||
}
|
||||
|
||||
if (maxScore.HasValue)
|
||||
{
|
||||
// 使用排名计算性能分数(100 - 排名)
|
||||
query = query.Where(p => (100 - p.CurrentRank) <= maxScore.Value);
|
||||
}
|
||||
|
||||
if (year.HasValue)
|
||||
{
|
||||
query = query.Where(p => p.ReleaseDate.Year == year.Value);
|
||||
}
|
||||
|
||||
return await query.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<Category?> GetCategoryByProductIdAsync(int productId)
|
||||
{
|
||||
var product = await _dbSet
|
||||
.Include(p => p.Category)
|
||||
.FirstOrDefaultAsync(p => p.Id == productId);
|
||||
|
||||
return product?.Category;
|
||||
}
|
||||
}
|
||||
}
|
||||
60
MinimalAPI/Repositories/Repository.cs
Normal file
60
MinimalAPI/Repositories/Repository.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using HardwarePerformance.Data;
|
||||
using HardwarePerformance.Models;
|
||||
|
||||
namespace HardwarePerformance.Repositories
|
||||
{
|
||||
public class Repository<T> : IRepository<T> where T : class
|
||||
{
|
||||
protected readonly AppDbContext _context;
|
||||
protected readonly DbSet<T> _dbSet;
|
||||
|
||||
public Repository(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
_dbSet = context.Set<T>();
|
||||
}
|
||||
|
||||
public async Task<T?> GetByIdAsync(int id)
|
||||
{
|
||||
return await _dbSet.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> GetAllAsync()
|
||||
{
|
||||
return await _dbSet.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
return await _dbSet.Where(predicate).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<T> AddAsync(T entity)
|
||||
{
|
||||
await _dbSet.AddAsync(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(T entity)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(T entity)
|
||||
{
|
||||
_dbSet.Remove(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> CountAsync(Expression<Func<T, bool>>? predicate = null)
|
||||
{
|
||||
if (predicate == null)
|
||||
return await _dbSet.CountAsync();
|
||||
return await _dbSet.CountAsync(predicate);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user