|
|
|
|
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 != true).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Item> FindTodayItems(int userId)
|
|
|
|
|
{
|
|
|
|
|
using (var context = new ToDoContext())
|
|
|
|
|
{
|
|
|
|
|
return context.Items.Include(i => i.User).Where(i => i.UserId == userId && i.IsCompleted != true && i.IsToday == true).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Item> FindPlannedItems(int userId)
|
|
|
|
|
{
|
|
|
|
|
using (var context = new ToDoContext())
|
|
|
|
|
{
|
|
|
|
|
return context.Items.Include(i => i.User).Where(i => i.UserId == userId && i.IsCompleted != true && i.DueDate != null).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Item> FindCompletedItems(int userId)
|
|
|
|
|
{
|
|
|
|
|
using (var context = new ToDoContext())
|
|
|
|
|
{
|
|
|
|
|
return context.Items.Include(i => i.User).Where(i => i.UserId == userId && i.IsCompleted == true).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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|