Source

main / Source / WebClient / Web.DataLayer / DocumentStoreRepository.cs

using System;
using System.Linq;
using System.Data.Objects;
using System.Collections.Generic;

namespace DataUp.Web.DataLayer
{
    /// <summary>
    /// Entity representing document store repository
    /// </summary>
    public class DocumentStoreRepository : RepositoryBase<DocumentStore>, IDocumentStoreRepository
    {
        private readonly DataUpEntities dataUpEntities;

        public DocumentStoreRepository(DataUpEntities dataUpEntities)
            :base  (dataUpEntities)
        {
            this.dataUpEntities = dataUpEntities;
        }

        public DocumentStore GetItem(int userId, int documentId)
        {
            return getDocumentQuery.Invoke(dataUpEntities, userId, documentId);
        }

        public IEnumerable<DocumentStore> GetItems(int userId)
        {
            var documents = getAllDocumentsQuery.Invoke(dataUpEntities, userId);
            if (documents != null)
            {
                documents = documents.OrderByDescending(q => q.UploadedDate);
            }
            return documents;
        }

        static readonly Func<DataUpEntities, int, int, DocumentStore> getDocumentQuery =
                                    CompiledQuery.Compile<DataUpEntities, int, int, DocumentStore>(
                                            (ctx, userId, documentId) => ctx.DocumentStores
                                                                            .Where(q => q.UserId == userId && q.Id == documentId).FirstOrDefault());

        static readonly Func<DataUpEntities, int, IQueryable<DocumentStore>> getAllDocumentsQuery =
                                    CompiledQuery.Compile<DataUpEntities, int, IQueryable<DocumentStore>>(
                                            (ctx, userId) => ctx.DocumentStores
                                                                            .Where(q => q.UserId == userId));
    
    }
}