You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.8 KiB

2 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToDoApp.Context;
using ToDoApp.Model;
using ToDoApp.Utility;
2 years ago
namespace ToDoApp.Service
2 years ago
{
2 years ago
public class UserService
2 years ago
{
2 years ago
public static UserService Instance { get; set; } = new UserService();
2 years ago
2 years ago
public User? FindUser(int userId)
2 years ago
{
using (var context = new ToDoContext())
{
2 years ago
return context.Users.Find(userId);
2 years ago
}
}
public User? FindUser(string username)
{
using (var context = new ToDoContext())
{
return context.Users.FirstOrDefault(u => u.Name == username);
}
}
2 years ago
public bool CheckUserLogin(string username, string password)
2 years ago
{
User? user = FindUser(username);
if (user == null)
return false;
return Utils.VerifyPassword(password, user.Password);
}
public void CreateUser(User user)
{
using (var context = new ToDoContext())
{
2 years ago
user.Password = Utils.HashPassword(user.Password);
2 years ago
user.CreateDate = DateTime.Now;
context.Users.Add(user);
context.SaveChanges();
}
}
2 years ago
public void DisableUser(int userId)
2 years ago
{
using (var context = new ToDoContext())
{
2 years ago
User? user = context.Users.Find(userId);
2 years ago
if (user == null)
return;
user.Status = UserStatus.Invalid;
context.SaveChanges();
}
}
public void DisableUser(string username)
{
using (var context = new ToDoContext())
{
User? user = context.Users.FirstOrDefault(u => u.Name == username);
if (user == null)
return;
user.Status = UserStatus.Invalid;
context.SaveChanges();
}
}
2 years ago
public void RemoveUser(int userId)
2 years ago
{
using (var context = new ToDoContext())
{
2 years ago
User? user = context.Users.Find(userId);
2 years ago
if (user == null)
return;
context.Users.Remove(user);
context.SaveChanges();
}
}
public void RemoveUser(string username)
{
using (var context = new ToDoContext())
{
User? user = context.Users.FirstOrDefault(u => u.Name == username);
if (user == null)
return;
context.Users.Remove(user);
context.SaveChanges();
}
}
2 years ago
public void RemoveUser(User user)
{
RemoveUser(user.Id);
}
2 years ago
}
}