CodeSnippets-EZ

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Ez.Model;

namespace Ez.Business.Repository
{
    public class FolderRepository : IFolderRepository
    {
        EzReportsEntities context = new EzReportsEntities();

        /// <summary>
        /// Gets all folders.
        /// </summary>
        public IEnumerable<Folder> All
        {
            get { return context.Folders; }
        }

        /// <summary>
        /// Alls folders with filter.
        /// </summary>
        /// <param name="includeProperties">The include properties.</param>
        /// <returns></returns>
        public IEnumerable<Folder> AllIncluding(params Expression<Func<Folder, object>>[] includeProperties)
        {
            IQueryable<Folder> query = context.Folders;
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
            return query;
        }

        /// <summary>
        /// Finds the specified folder.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Folder Find(int id)
        {
            return context.Folders.FirstOrDefault(x => x.FolderId == id);
        }
        /// <summary>
        /// Gets the root folder.
        /// </summary>
        /// <returns></returns>
        public Folder GetRoot()
        {
            IQueryable<Folder> myFolders = Find("ROOT");

            if (myFolders == nullthrow (new Exception("cannot locate root folder"));
            if (myFolders.Count() != 1) throw (new Exception("cannot locate root folder"));

            return myFolders.First();
        }
        /// <summary>
        /// Finds the specified folder by name.
        /// </summary>
        /// <param name="folderName">Name of the folder.</param>
        /// <returns></returns>
        public IQueryable<Folder> Find(string folderName)
        {
            return context.Folders.Where(x => x.Name == folderName);
        }

        public IEnumerable<Folder> RootFolders()
        {
            int myRootId = GetRoot().FolderId;
            IEnumerable<Folder> mySubFolders = FindFolderChildren(myRootId);

            return mySubFolders;
        }

        /// <summary>
        /// Finds the folder children (subfolders).
        /// </summary>
        /// <param name="parentId">The parent id.</param>
        /// <returns></returns>
        public IEnumerable<Folder> FindFolderChildren(int parentId)
        {
            return context.Folders.Where(x => x.ParentFolderID == parentId);
        }

        /// <summary>
        /// Finds the report children(subfolders).
        /// </summary>
        /// <param name="parentId">The parent id.</param>
        /// <returns></returns>
        public IEnumerable<Report> FindReportChildren(int parentId)
        {
            Folder myFolder = this.Find(parentId);

            var myReports =
                from p in myFolder.ReportFolders
                select p.Report;

            return myReports.AsQueryable();
        }

        /// <summary>
        /// Inserts the or update folder.
        /// </summary>
        /// <param name="folder">The folder.</param>
        public void InsertOrUpdate(Folder folder)
        {
            if (folder.FolderId == default(int))
            {
                // New entity
                context.Folders.AddObject(folder);
            }
            else
            {
                // Existing entity
                context.Folders.Attach(folder);
                context.ObjectStateManager.ChangeObjectState(folder, EntityState.Modified);
            }
        }

        /// <summary>
        /// Deletes the folder.
        /// </summary>
        /// <param name="id">The id.</param>
        public void Delete(int id)
        {
            Folder myFolder = context.Folders.SingleOrDefault(x => x.FolderId == id);
            if (myFolder == nullreturn;

            context.Folders.DeleteObject(myFolder);
        }

        /// <summary>
        /// Saves the folder.
        /// </summary>
        public void Save()
        {
            context.SaveChanges();
        }
    }

    public interface IFolderRepository
    {
        IEnumerable<Folder> All { get; }
        IEnumerable<Folder> AllIncluding(params Expression<Func<Folder, object>>[] includeProperties);
        IEnumerable<Folder> RootFolders();
        IEnumerable<Folder> FindFolderChildren(int parentId);
        IEnumerable<Report> FindReportChildren(int parentId);

        Folder Find(int id);
        void InsertOrUpdate(Folder folder);
        void Delete(int id);
        void Save();
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s