using InventoryManager.Data; using InventoryManager.Interfaces; using InventoryManager.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using InventoryManager.Tools; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace InventoryManager.Repositories { public class CollectionsRepo : ICollections { private readonly InventoryContext _context; public CollectionsRepo(InventoryContext context) { _context = context; } public Collections Create(Collections collection) { _context.Collections.Add(collection); _context.SaveChanges(); return collection; } public Collections Delete(Collections collection) { _context.Collections.Attach(collection); _context.Entry(collection).State = EntityState.Deleted; _context.SaveChanges(); return collection; } public Collections Edit(Collections collection) { _context.Collections.Attach(collection); _context.Entry(collection).State = EntityState.Modified; _context.SaveChanges(); return collection; } private List<Collections> Sort(List<Collections> items, string SortProperty, SortOrder sortOrder) { if (SortProperty.ToLower() == "name") { if (sortOrder == SortOrder.Ascending) items = items.OrderBy(n => n.Name).ToList(); else items = items.OrderByDescending(n => n.Name).ToList(); } else { if (sortOrder == SortOrder.Ascending) items = items.OrderBy(d => d.Description).ToList(); else items = items.OrderByDescending(d => d.Description).ToList(); } return items; } public PaginatedList<Collections> GetItems(string SortProperty, SortOrder sortOrder, string SearchText = "", int pageIndex=1, int pageSize=5) { List<Collections> items; if (SearchText != "" && SearchText != null) { items = _context.Collections.Where(n => n.Name.Contains(SearchText) || n.Description.Contains(SearchText)).ToList(); } else items = _context.Collections.ToList(); items = Sort(items, SortProperty, sortOrder); PaginatedList<Collections> retItems = new PaginatedList<Collections>(items, pageIndex, pageSize); return retItems; } public Collections GetItem(int id) { Collections item = _context.Collections.Where(u => u.Id == id).FirstOrDefault(); return item; } public bool IsItemUnique(string name) { int ct = _context.Collections.Where(n => n.Name.ToLower() == name.ToLower()).Count(); if(ct > 0) return true; else return false; } public bool IsItemUnique(string name, int id) { int ct = _context.Collections.Where(n => n.Name.ToLower() == name.ToLower() && n.Id!=id).Count(); if (ct > 0) return true; else return false; } public List<Collections> GetNameList() { List<Collections> items = _context.Collections.ToList(); return items; } } }