InventoryManager / Repositories / SuppliersRepo.cs
SuppliersRepo.cs
Raw
using InventoryManager.Data;
using InventoryManager.Interfaces;
using InventoryManager.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using InventoryManager.Tools;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;

namespace InventoryManager.Repositories
{
    public class SuppliersRepo : ISuppliers
    {
        private readonly InventoryContext _context;

        public SuppliersRepo(InventoryContext context)
        {
            _context = context;
        }

        public Suppliers Create(Suppliers supplier)
        {
            _context.Suppliers.Add(supplier);
            _context.SaveChanges();
            return supplier;
        }

        public Suppliers Delete(Suppliers supplier)
        {
            _context.Suppliers.Attach(supplier);
            _context.Entry(supplier).State = EntityState.Deleted;
            _context.SaveChanges();
            return supplier;
        }

        public Suppliers Edit(Suppliers supplier)
        {
            _context.Suppliers.Attach(supplier);
            _context.Entry(supplier).State = EntityState.Modified;
            _context.SaveChanges();
            return supplier;
        }

        private List<Suppliers> Sort(List<Suppliers> 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.Contact).ToList();
                else
                    items = items.OrderByDescending(d => d.Contact).ToList();
            }
            return items;
        }

        public PaginatedList<Suppliers> GetItems(string SortProperty, SortOrder sortOrder, string SearchText = "", int pageIndex=1, int pageSize=5)
        {
            List<Suppliers> items;

            if (SearchText != "" && SearchText != null)
            {
                items = _context.Suppliers.Where(n => n.Name.Contains(SearchText) || n.Contact.Contains(SearchText)).ToList();
            }
            else
                items = _context.Suppliers.ToList();


            items = Sort(items, SortProperty, sortOrder);

            PaginatedList<Suppliers> retItems = new PaginatedList<Suppliers>(items, pageIndex, pageSize);

            return retItems;
        }

        public Suppliers GetItem(int id)
        {
            Suppliers item = _context.Suppliers.Where(u => u.Id == id).FirstOrDefault();
            return item;
        }


        public bool IsItemUnique(string name)
        {
            int ct = _context.Suppliers.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.Suppliers.Where(n => n.Name.ToLower() == name.ToLower() && n.Id!=id).Count();
            if (ct > 0)
                return true;
            else return false;
        }

        public List<Suppliers> GetNameList()
        {
            List<Suppliers> items = _context.Suppliers.ToList();
            return items;

        }
    }
}