todo stage1
This commit is contained in:
109
MySolution/ToDoApp/Service/UserService.cs
Normal file
109
MySolution/ToDoApp/Service/UserService.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
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;
|
||||
|
||||
namespace ToDoApp.Service
|
||||
{
|
||||
public class UserService
|
||||
{
|
||||
public static UserService Instance { get; set; } = new UserService();
|
||||
|
||||
public User? FindUser(int userId)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
return context.Users.Find(userId);
|
||||
}
|
||||
}
|
||||
|
||||
public User? FindUser(string username)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
return context.Users.FirstOrDefault(u => u.Name == username);
|
||||
}
|
||||
}
|
||||
|
||||
public bool CheckUserLogin(string username, string password)
|
||||
{
|
||||
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())
|
||||
{
|
||||
user.Password = Utils.HashPassword(user.Password);
|
||||
user.CreateDate = DateTime.Now;
|
||||
context.Users.Add(user);
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void DisableUser(int userId)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
User? user = context.Users.Find(userId);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveUser(int userId)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
User? user = context.Users.Find(userId);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveUser(User user)
|
||||
{
|
||||
RemoveUser(user.Id);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user