todo stage1
This commit is contained in:
22
MySolution/ToDoApp/Service/DbService.cs
Normal file
22
MySolution/ToDoApp/Service/DbService.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ToDoApp.Context;
|
||||
|
||||
namespace ToDoApp.Service
|
||||
{
|
||||
public class DbService
|
||||
{
|
||||
public static DbService Instance { get; set; } = new DbService();
|
||||
|
||||
public void EnsureCreated()
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
108
MySolution/ToDoApp/Service/ItemService.cs
Normal file
108
MySolution/ToDoApp/Service/ItemService.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
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 ItemService
|
||||
{
|
||||
public static ItemService Instance { get; set; } = new ItemService();
|
||||
|
||||
public List<Item> FindAllItems(int userId)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
return context.Items.Where(i => i.UserId == userId).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Item> FindAllItems(User user)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
return context.Items.Include(i => i.User).Where(i => i.User == user).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Item> FindAllNotCompletedItems(int userId)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
return context.Items.Include(i => i.User).Where(i => i.UserId == userId && i.IsCompleted != false).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Item> FindAllNotCompletedItems(User user)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
return context.Items.Include(i => i.User).Where(i => i.User == user && i.IsCompleted == false).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public Item? FindItem(int itemId)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
return context.Items.FirstOrDefault(i => i.Id == itemId);
|
||||
}
|
||||
}
|
||||
|
||||
public Item? FindItem(Item item)
|
||||
{
|
||||
return FindItem(item.Id);
|
||||
}
|
||||
|
||||
public void CreateItem(Item item)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
item.CreateDate = DateTime.Now;
|
||||
context.Items.Add(item);
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateItem(Item item)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
Item? original = context.Items.Find(item.Id);
|
||||
if (original == null)
|
||||
return;
|
||||
|
||||
original.Title = item.Title;
|
||||
original.Description = item.Description;
|
||||
original.DueDate = item.DueDate;
|
||||
original.IsToday = item.IsToday;
|
||||
original.IsCompleted = item.IsCompleted;
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveItem(int itemId)
|
||||
{
|
||||
using (var context = new ToDoContext())
|
||||
{
|
||||
Item? item = context.Items.Find(itemId);
|
||||
if (item == null)
|
||||
return;
|
||||
|
||||
context.Remove(item);
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveItem(Item item)
|
||||
{
|
||||
RemoveItem(item.Id);
|
||||
}
|
||||
}
|
||||
}
|
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