Files
DMS/DMS.Infrastructure/Repositories/UserRepository.cs

74 lines
2.1 KiB
C#
Raw Normal View History

2025-07-21 22:02:42 +08:00
using System.Diagnostics;
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
/// <summary>
/// 用户仓储类用于操作DbUser实体
/// </summary>
using AutoMapper;
using DMS.Core.Helper;
2025-07-21 18:49:49 +08:00
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
2025-07-19 11:11:01 +08:00
using DMS.Core.Models;
2025-07-19 09:25:01 +08:00
using DMS.Infrastructure.Data;
2025-07-21 18:49:49 +08:00
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
/// <summary>
2025-07-21 18:49:49 +08:00
/// 用户仓储类用于操作DbUser实体
/// </summary>
2025-07-21 18:49:49 +08:00
public class UserRepository : BaseRepository<DbUser>, IUserRepository
{
2025-07-21 22:02:42 +08:00
private readonly IMapper _mapper;
public UserRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
2025-07-21 22:02:42 +08:00
_mapper = mapper;
}
2025-07-21 18:49:49 +08:00
2025-07-21 22:02:42 +08:00
public async Task<User> GetByIdAsync(int id)
{
var dbUser = await base.GetByIdAsync(id);
return _mapper.Map<User>(dbUser);
}
2025-07-21 18:49:49 +08:00
2025-07-21 22:02:42 +08:00
public async Task<List<User>> GetAllAsync()
{
var dbList = await base.GetAllAsync();
return _mapper.Map<List<User>>(dbList);
}
2025-07-21 18:49:49 +08:00
2025-07-21 22:02:42 +08:00
public async Task<User> AddAsync(User entity)
{
var dbUser = await base.AddAsync(_mapper.Map<DbUser>(entity));
return _mapper.Map(dbUser, entity);
}
2025-07-21 18:49:49 +08:00
2025-07-21 22:02:42 +08:00
public async Task<int> UpdateAsync(User entity) => await base.UpdateAsync(_mapper.Map<DbUser>(entity));
2025-07-21 18:49:49 +08:00
2025-07-21 22:02:42 +08:00
public async Task<int> DeleteAsync(User entity) => await base.DeleteAsync(_mapper.Map<DbUser>(entity));
2025-07-24 18:09:46 +08:00
public async Task<int> DeleteByIdAsync(int id)
2025-07-21 22:02:42 +08:00
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Deleteable(new User() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
2025-07-22 21:36:33 +08:00
public new async Task<List<User>> TakeAsync(int number)
{
var dbList = await base.TakeAsync(number);
return _mapper.Map<List<User>>(dbList);
}
}