Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2011
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.52 KB | None | 0 0
  1.     public class TagsRepository : ITagsRepository
  2.     {
  3.         public long? GetTagIdByName(string tagName, Language language)
  4.         {
  5.             using (var ctx = new RepositoryContext())
  6.             {
  7.                 var tagId = ctx.BaseTags.
  8.                     Where(bt => bt.BaseTagLocalizations.
  9.                                     Any(btl => btl.Name == tagName && btl.LanguageId == (long) language)).
  10.                     Select(bt => (long?) bt.Id).
  11.                     SingleOrDefault();
  12.                 if (!tagId.HasValue)
  13.                     tagId = ctx.BaseTags.
  14.                         Where(bt => bt.BaseTagLocalizations.
  15.                             Any(btl => btl.Name == tagName && btl.IsDefault == 1)).
  16.                             Select(bt => (long?)bt.Id).
  17.                             SingleOrDefault();
  18.                 return tagId;
  19.             }
  20.         }
  21.  
  22.         public BaseTag GetTagByIdWithParentsAndPaths(long tagId)
  23.         {
  24.             using (var ctx = new RepositoryContext())
  25.             {
  26.                 return ctx.BaseTags.
  27.                     Include(bt => bt.BaseTagParents).
  28.                     Include(bt => bt.BaseTagPaths).Where(bt => bt.Id == tagId).
  29.                     Single();
  30.             }
  31.         }
  32.  
  33.         public IList<BaseTag> GetTagsWithLocalizations(IList<long> tagsIds)
  34.         {
  35.             using (var ctx = new RepositoryContext())
  36.             {
  37.                 return ctx.BaseTags.
  38.                     Include(bt => bt.BaseTagLocalizations).Where(bt => tagsIds.Contains(bt.Id)).ToList();
  39.             }
  40.         }
  41.  
  42.         public IList<BaseTag> GetTagsWithParentsAndLocalizations(IList<long> tagsIds)
  43.         {
  44.             using (var ctx = new RepositoryContext())
  45.             {
  46.                 return ctx.BaseTags.
  47.                     Include(bt => bt.BaseTagParents).
  48.                     Include(bt => bt.BaseTagLocalizations).Where(bt => tagsIds.Contains(bt.Id)).ToList();
  49.             }
  50.         }
  51.  
  52.         public IList<BaseTag> GetTagsWithParentsAndPaths(IList<long> tagsIds)
  53.         {
  54.             using (var ctx = new RepositoryContext())
  55.             {
  56.                 return ctx.BaseTags.
  57.                     Include(bt => bt.BaseTagParents).
  58.                     Include(bt => bt.BaseTagPaths).Where(bt => tagsIds.Contains(bt.Id)).ToList();
  59.             }
  60.         }
  61.  
  62.         public IList<string> GetTagPaths(long tagId)
  63.         {
  64.             using (var ctx = new RepositoryContext())
  65.             {
  66.                 return ctx.BaseTagPaths.Where(btp => btp.BaseTagId == tagId).Select(btp => btp.Path).ToList();
  67.             }
  68.         }
  69.  
  70.         public IList<string> GetTagsPaths(IList<long> tagsIds)
  71.         {
  72.             using (var ctx = new RepositoryContext())
  73.             {
  74.                 return ctx.BaseTagPaths.Where(btp => tagsIds.Contains(btp.BaseTagId)).Select(btp => btp.Path).ToList();
  75.             }
  76.         }
  77.  
  78.         public IList<string> GetItemTagPaths(long itemId)
  79.         {
  80.             using (var ctx = new RepositoryContext())
  81.             {
  82.                 return ctx.BaseTags.
  83.                     OfType<ItemTag>().
  84.                     Where(it => it.ItemId == itemId).
  85.                     SelectMany(it => it.BaseTagPaths).
  86.                     Select(btp => btp.Path).
  87.                     ToList();
  88.             }
  89.         }
  90.  
  91.         public long GetItemTagId(long itemId)
  92.         {
  93.             using (var ctx = new RepositoryContext())
  94.             {
  95.                 return ctx.BaseTags.
  96.                     OfType<ItemTag>().
  97.                     Where(it => it.ItemId == itemId).
  98.                     Select(it => it.Id).
  99.                     Single();
  100.             }
  101.         }
  102.  
  103.         public ItemTag GetItemTag(long itemTagId)
  104.         {
  105.             using (var ctx = new RepositoryContext())
  106.             {
  107.                 return ctx.BaseTags.
  108.                     OfType<ItemTag>().
  109.                     Where(it => it.Id == itemTagId).
  110.                     Single();
  111.             }
  112.         }
  113.  
  114.         public ItemTag GetItemTagWithParentsAndPaths(long itemId)
  115.         {
  116.             using (var ctx = new RepositoryContext())
  117.             {
  118.                 return ctx.BaseTags.
  119.                     OfType<ItemTag>().
  120.                     Include(it => it.BaseTagParents).
  121.                     Include(it => it.BaseTagPaths).Where(it => it.ItemId == itemId).
  122.                     Single();
  123.             }
  124.         }
  125.  
  126.         public ItemTag GetItemTagWithLocalizationsAndPaths(long itemId)
  127.         {
  128.             using (var ctx = new RepositoryContext())
  129.             {
  130.                 return ctx.BaseTags.
  131.                     OfType<ItemTag>().
  132.                     Include(it => it.BaseTagLocalizations).
  133.                     Include(it => it.BaseTagPaths).
  134.                     Where(it => it.ItemId == itemId).
  135.                     Single();
  136.             }
  137.         }
  138.  
  139.         public long GetFirstItemId(IList<string> tagsPaths)
  140.         {
  141.             using (var ctx = new RepositoryContext())
  142.             {
  143.                 return TaggedEntitiesRetrievalHelper.GetBaseTagPathCore(ctx, tagsPaths, false, true).
  144.                     Join(ctx.BaseTags.OfType<ItemTag>(), btp => btp.BaseTagId, it => it.Id, (btp, it) => it).
  145.                     Select(it => it.ItemId).
  146.                     First();
  147.             }
  148.         }
  149.  
  150.         public CompositeTag GetGenerationSetTag(long generationSetId, ObjectType objectType)
  151.         {
  152.             using (var ctx = new RepositoryContext())
  153.             {
  154.                 return ctx.BaseTags.
  155.                     OfType<CompositeTag>().
  156.                     Include(ct => ct.BaseTagLocalizations).
  157.                     Where(ct => ct.ObjectId == generationSetId && ct.ObjectType == (long)objectType).
  158.                     Single();
  159.             }
  160.         }
  161.  
  162.         public IDictionary<long, CompositeTag> GetCompositeTags(IList<long> objectsIds, ObjectType objectType)
  163.         {
  164.             using (var ctx = new RepositoryContext())
  165.             {
  166.                 return Enumerable.ToDictionary(Queryable.Where(ctx.BaseTags.
  167.                                                      Include(bt => bt.BaseTagLocalizations).
  168.                                                      OfType<CompositeTag>(), ct => objectsIds.Contains(ct.ObjectId) && ct.ObjectType == (long)objectType), ct => ct.ObjectId, ct => ct);
  169.             }
  170.         }
  171.  
  172.         public long GetCompositeTagId(long objectId, ObjectType objectType)
  173.         {
  174.             using (var ctx = new RepositoryContext())
  175.             {
  176.                 return Queryable.Single(Queryable.Select(Queryable.Where(ctx.BaseTags.
  177.                                                                 OfType<CompositeTag>(), ct => ct.ObjectId == objectId && ct.ObjectType == (long)objectType), ct => ct.Id));
  178.             }
  179.         }
  180.  
  181.         public IList<long> GetChildAndSelfTagsIds(IList<string> paths)
  182.         {
  183.             return GetChildTagsIdsCore(paths, true);
  184.         }
  185.  
  186.         public IList<long> GetChildTagsIds(IList<string> paths)
  187.         {
  188.             return GetChildTagsIdsCore(paths, false);
  189.         }
  190.  
  191.         private IList<long> GetChildTagsIdsCore(IList<string> paths, bool includeSelf)
  192.         {
  193.             using (var ctx = new RepositoryContext())
  194.             {
  195.                 return TaggedEntitiesRetrievalHelper.GetBaseTagPathCore(ctx, paths, false, includeSelf).
  196.                     Join(ctx.BaseTags, btp => btp.BaseTagId, bt => bt.Id, (btp, bt) => bt).
  197.                     Select(bt => bt.Id).
  198.                     ToList();
  199.             }
  200.         }
  201.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement