Added new API for getting Member (aka Users but for use in FE). User is just used for login/registering.

This commit is contained in:
Joseph Milazzo 2020-12-14 14:33:09 -06:00
parent a920be092d
commit 13ed323949
14 changed files with 172 additions and 65 deletions

View file

@ -0,0 +1,61 @@
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.ProjectTo<MemberDto>(_mapper.ConfigurationProvider).ToListAsync();
}
public async Task<MemberDto> GetMemberAsync(string username)
{
return await _context.Users.Where(x => x.UserName == username)
.ProjectTo<MemberDto>(_mapper.ConfigurationProvider)
.SingleOrDefaultAsync();
}
}
}