Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TagsRepository : ITagsRepository
- {
- public long? GetTagIdByName(string tagName, Language language)
- {
- using (var ctx = new RepositoryContext())
- {
- var tagId = ctx.BaseTags.
- Where(bt => bt.BaseTagLocalizations.
- Any(btl => btl.Name == tagName && btl.LanguageId == (long) language)).
- Select(bt => (long?) bt.Id).
- SingleOrDefault();
- if (!tagId.HasValue)
- tagId = ctx.BaseTags.
- Where(bt => bt.BaseTagLocalizations.
- Any(btl => btl.Name == tagName && btl.IsDefault == 1)).
- Select(bt => (long?)bt.Id).
- SingleOrDefault();
- return tagId;
- }
- }
- public BaseTag GetTagByIdWithParentsAndPaths(long tagId)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- Include(bt => bt.BaseTagParents).
- Include(bt => bt.BaseTagPaths).Where(bt => bt.Id == tagId).
- Single();
- }
- }
- public IList<BaseTag> GetTagsWithLocalizations(IList<long> tagsIds)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- Include(bt => bt.BaseTagLocalizations).Where(bt => tagsIds.Contains(bt.Id)).ToList();
- }
- }
- public IList<BaseTag> GetTagsWithParentsAndLocalizations(IList<long> tagsIds)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- Include(bt => bt.BaseTagParents).
- Include(bt => bt.BaseTagLocalizations).Where(bt => tagsIds.Contains(bt.Id)).ToList();
- }
- }
- public IList<BaseTag> GetTagsWithParentsAndPaths(IList<long> tagsIds)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- Include(bt => bt.BaseTagParents).
- Include(bt => bt.BaseTagPaths).Where(bt => tagsIds.Contains(bt.Id)).ToList();
- }
- }
- public IList<string> GetTagPaths(long tagId)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTagPaths.Where(btp => btp.BaseTagId == tagId).Select(btp => btp.Path).ToList();
- }
- }
- public IList<string> GetTagsPaths(IList<long> tagsIds)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTagPaths.Where(btp => tagsIds.Contains(btp.BaseTagId)).Select(btp => btp.Path).ToList();
- }
- }
- public IList<string> GetItemTagPaths(long itemId)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- OfType<ItemTag>().
- Where(it => it.ItemId == itemId).
- SelectMany(it => it.BaseTagPaths).
- Select(btp => btp.Path).
- ToList();
- }
- }
- public long GetItemTagId(long itemId)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- OfType<ItemTag>().
- Where(it => it.ItemId == itemId).
- Select(it => it.Id).
- Single();
- }
- }
- public ItemTag GetItemTag(long itemTagId)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- OfType<ItemTag>().
- Where(it => it.Id == itemTagId).
- Single();
- }
- }
- public ItemTag GetItemTagWithParentsAndPaths(long itemId)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- OfType<ItemTag>().
- Include(it => it.BaseTagParents).
- Include(it => it.BaseTagPaths).Where(it => it.ItemId == itemId).
- Single();
- }
- }
- public ItemTag GetItemTagWithLocalizationsAndPaths(long itemId)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- OfType<ItemTag>().
- Include(it => it.BaseTagLocalizations).
- Include(it => it.BaseTagPaths).
- Where(it => it.ItemId == itemId).
- Single();
- }
- }
- public long GetFirstItemId(IList<string> tagsPaths)
- {
- using (var ctx = new RepositoryContext())
- {
- return TaggedEntitiesRetrievalHelper.GetBaseTagPathCore(ctx, tagsPaths, false, true).
- Join(ctx.BaseTags.OfType<ItemTag>(), btp => btp.BaseTagId, it => it.Id, (btp, it) => it).
- Select(it => it.ItemId).
- First();
- }
- }
- public CompositeTag GetGenerationSetTag(long generationSetId, ObjectType objectType)
- {
- using (var ctx = new RepositoryContext())
- {
- return ctx.BaseTags.
- OfType<CompositeTag>().
- Include(ct => ct.BaseTagLocalizations).
- Where(ct => ct.ObjectId == generationSetId && ct.ObjectType == (long)objectType).
- Single();
- }
- }
- public IDictionary<long, CompositeTag> GetCompositeTags(IList<long> objectsIds, ObjectType objectType)
- {
- using (var ctx = new RepositoryContext())
- {
- return Enumerable.ToDictionary(Queryable.Where(ctx.BaseTags.
- Include(bt => bt.BaseTagLocalizations).
- OfType<CompositeTag>(), ct => objectsIds.Contains(ct.ObjectId) && ct.ObjectType == (long)objectType), ct => ct.ObjectId, ct => ct);
- }
- }
- public long GetCompositeTagId(long objectId, ObjectType objectType)
- {
- using (var ctx = new RepositoryContext())
- {
- return Queryable.Single(Queryable.Select(Queryable.Where(ctx.BaseTags.
- OfType<CompositeTag>(), ct => ct.ObjectId == objectId && ct.ObjectType == (long)objectType), ct => ct.Id));
- }
- }
- public IList<long> GetChildAndSelfTagsIds(IList<string> paths)
- {
- return GetChildTagsIdsCore(paths, true);
- }
- public IList<long> GetChildTagsIds(IList<string> paths)
- {
- return GetChildTagsIdsCore(paths, false);
- }
- private IList<long> GetChildTagsIdsCore(IList<string> paths, bool includeSelf)
- {
- using (var ctx = new RepositoryContext())
- {
- return TaggedEntitiesRetrievalHelper.GetBaseTagPathCore(ctx, paths, false, includeSelf).
- Join(ctx.BaseTags, btp => btp.BaseTagId, bt => bt.Id, (btp, bt) => bt).
- Select(bt => bt.Id).
- ToList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement