mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.DTOs;
|
|
using API.Entities;
|
|
using API.Interfaces;
|
|
using AutoMapper;
|
|
using AutoMapper.QueryableExtensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace API.Data
|
|
{
|
|
public class UserRepository : IUserRepository
|
|
{
|
|
private readonly DataContext _context;
|
|
private readonly IMapper _mapper;
|
|
|
|
public UserRepository(DataContext context, IMapper mapper)
|
|
{
|
|
_context = context;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public void Update(AppUser user)
|
|
{
|
|
_context.Entry(user).State = EntityState.Modified;
|
|
}
|
|
|
|
public async Task<bool> SaveAllAsync()
|
|
{
|
|
return await _context.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
public async Task<IEnumerable<AppUser>> GetUsersAsync()
|
|
{
|
|
return await _context.Users.ToListAsync();
|
|
}
|
|
|
|
public async Task<AppUser> GetUserByIdAsync(int id)
|
|
{
|
|
return await _context.Users.FindAsync(id);
|
|
}
|
|
|
|
public async Task<AppUser> GetUserByUsernameAsync(string username)
|
|
{
|
|
return await _context.Users
|
|
.SingleOrDefaultAsync(x => x.UserName == username);
|
|
}
|
|
|
|
public async Task<IEnumerable<MemberDto>> GetMembersAsync()
|
|
{
|
|
return await _context.Users.Include(x => x.Libraries)
|
|
.Include(x => x.Libraries)
|
|
.ProjectTo<MemberDto>(_mapper.ConfigurationProvider)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<MemberDto> GetMemberAsync(string username)
|
|
{
|
|
return await _context.Users.Where(x => x.UserName == username)
|
|
.Include(x => x.Libraries)
|
|
.ProjectTo<MemberDto>(_mapper.ConfigurationProvider)
|
|
.SingleOrDefaultAsync();
|
|
}
|
|
|
|
public async Task<bool> AdminExists()
|
|
{
|
|
return await _context.Users.AnyAsync(x => x.IsAdmin);
|
|
|
|
}
|
|
}
|
|
} |