skantravels/API/Persistence/Repositories/UserRepository.cs

95 lines
2.3 KiB
C#
Raw Normal View History

using API.Models;
using Microsoft.EntityFrameworkCore;
2024-08-14 09:28:12 +01:00
using Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Templates.BlazorIdentity.Pages;
namespace API.Persistence.Repositories
{
public class UserRepository(AppDBContext context) : IUserRepository
{
private readonly AppDBContext _context = context;
public async Task<List<User>> QueryAllUsersAsync()
{
return await _context.Users.ToListAsync();
}
public async Task<User> QueryUserByIdAsync(string id)
{
try
{
return await _context.Users
.FirstOrDefaultAsync(user => user.Id == id);
}
catch (Exception)
{
return new User();
}
}
public async Task<string> CreateUserAsync(User user)
{
try
{
_context.Users.Add(user);
await _context.SaveChangesAsync();
}
catch (Exception)
{
return "";
}
return user.Id;
}
public async Task<bool> UpdateUserAsync(User user)
{
try
{
_context.Entry(user).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
catch (Exception)
{
return false;
}
return true;
}
2024-08-16 11:53:39 +01:00
public async Task<bool> UpdateUserPasswordAsync(User user)
{
try
{
_context.Entry(user).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
catch (Exception)
{
return false;
}
return true;
}
public async Task<bool> DeleteUserAsync(string id)
{
var user = await _context.Users.FindAsync(id);
if (user == null)
{
return false;
}
_context.Users.Remove(user);
await _context.SaveChangesAsync();
return true;
}
2024-08-14 09:28:12 +01:00
public async Task<User> QueryUserByEmailAsync(string email)
{
return await _context.Users.SingleOrDefaultAsync(u => u.Email == email);
}
}
}