Files
dotNetStudyWithGPT/MySolution/ToDoApp/Service/UserService.cs

110 lines
2.8 KiB
C#
Raw Normal View History

2023-10-26 18:11:58 +09:00
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;
2023-10-27 18:07:18 +09:00
namespace ToDoApp.Service
2023-10-26 18:11:58 +09:00
{
2023-10-27 18:07:18 +09:00
public class UserService
2023-10-26 18:11:58 +09:00
{
2023-10-27 18:07:18 +09:00
public static UserService Instance { get; set; } = new UserService();
2023-10-26 18:11:58 +09:00
2023-10-27 18:07:18 +09:00
public User? FindUser(int userId)
2023-10-26 18:11:58 +09:00
{
using (var context = new ToDoContext())
{
2023-10-27 18:07:18 +09:00
return context.Users.Find(userId);
2023-10-26 18:11:58 +09:00
}
}
public User? FindUser(string username)
{
using (var context = new ToDoContext())
{
return context.Users.FirstOrDefault(u => u.Name == username);
}
}
2023-10-27 18:07:18 +09:00
public bool CheckUserLogin(string username, string password)
2023-10-26 18:11:58 +09:00
{
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())
{
2023-10-27 18:07:18 +09:00
user.Password = Utils.HashPassword(user.Password);
2023-10-26 18:11:58 +09:00
user.CreateDate = DateTime.Now;
context.Users.Add(user);
context.SaveChanges();
}
}
2023-10-27 18:07:18 +09:00
public void DisableUser(int userId)
2023-10-26 18:11:58 +09:00
{
using (var context = new ToDoContext())
{
2023-10-27 18:07:18 +09:00
User? user = context.Users.Find(userId);
2023-10-26 18:11:58 +09:00
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();
}
}
2023-10-27 18:07:18 +09:00
public void RemoveUser(int userId)
2023-10-26 18:11:58 +09:00
{
using (var context = new ToDoContext())
{
2023-10-27 18:07:18 +09:00
User? user = context.Users.Find(userId);
2023-10-26 18:11:58 +09:00
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();
}
}
2023-10-27 18:07:18 +09:00
public void RemoveUser(User user)
{
RemoveUser(user.Id);
}
2023-10-26 18:11:58 +09:00
}
}