<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Core.Common.Models;
using Orchard.Data;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.ContentManagement;
using Orchard.Security;
using Orchard.Tags.Models;
using Orchard.UI.Notify;

namespace Orchard.Tags.Services {
    public class TagService : ITagService {
        private readonly IRepository<TagRecord> _tagRepository;
        private readonly IRepository<ContentTagRecord> _contentTagRepository;
        private readonly INotifier _notifier;
        private readonly IAuthorizationService _authorizationService;
        private readonly IOrchardServices _orchardServices;
        private readonly ISessionFactoryHolder _sessionFactoryHolder;

        public TagService(IRepository<TagRecord> tagRepository,
                          IRepository<ContentTagRecord> contentTagRepository,
                          INotifier notifier,
                          IAuthorizationService authorizationService,
                          IOrchardServices orchardServices,
                          ISessionFactoryHolder sessionFactoryHolder) {
            _tagRepository = tagRepository;
            _contentTagRepository = contentTagRepository;
            _notifier = notifier;
            _authorizationService = authorizationService;
            _orchardServices = orchardServices;
            _sessionFactoryHolder = sessionFactoryHolder;
            Logger = NullLogger.Instance;
            T = NullLocalizer.Instance;
        }

        public ILogger Logger { get; set; }
        public Localizer T { get; set; }

        public IEnumerable<TagRecord> GetTags() {
            return _tagRepository.Table.ToList();
        }

        public IEnumerable<TagRecord> GetTagsByNameSnippet(string snippet, int maxCount = 10) {
            if (String.IsNullOrEmpty(snippet)) return null; // Otherwise would return the whole dataset
            return _tagRepository.Fetch(tag => tag.TagName.StartsWith(snippet)).Take(maxCount);
        }

        public TagRecord GetTag(int tagId) {
            return _tagRepository.Get(x => x.Id == tagId);
        }

        public TagRecord GetTagByName(string tagName) {
            return _tagRepository.Get(x => x.TagName == tagName);
        }

        public TagRecord CreateTag(string tagName) {
            var result = _tagRepository.Get(x => x.TagName == tagName);
            if (result == null) {
                result = new TagRecord { TagName = tagName };
                _tagRepository.Create(result);
            }
            return result;
        }<commit_msg>[Fixes #7192] Orchard.Tags - Manage Tags Admin Page Not Alpha Sorted (#7198)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Core.Common.Models;
using Orchard.Data;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.ContentManagement;
using Orchard.Security;
using Orchard.Tags.Models;
using Orchard.UI.Notify;

namespace Orchard.Tags.Services {
    public class TagService : ITagService {
        private readonly IRepository<TagRecord> _tagRepository;
        private readonly IRepository<ContentTagRecord> _contentTagRepository;
        private readonly INotifier _notifier;
        private readonly IAuthorizationService _authorizationService;
        private readonly IOrchardServices _orchardServices;
        private readonly ISessionFactoryHolder _sessionFactoryHolder;

        public TagService(IRepository<TagRecord> tagRepository,
                          IRepository<ContentTagRecord> contentTagRepository,
                          INotifier notifier,
                          IAuthorizationService authorizationService,
                          IOrchardServices orchardServices,
                          ISessionFactoryHolder sessionFactoryHolder) {
            _tagRepository = tagRepository;
            _contentTagRepository = contentTagRepository;
            _notifier = notifier;
            _authorizationService = authorizationService;
            _orchardServices = orchardServices;
            _sessionFactoryHolder = sessionFactoryHolder;
            Logger = NullLogger.Instance;
            T = NullLocalizer.Instance;
        }

        public ILogger Logger { get; set; }
        public Localizer T { get; set; }

        public IEnumerable<TagRecord> GetTags() {
            return _tagRepository.Table.OrderBy(x => x.TagName).ToList();
        }

        public IEnumerable<TagRecord> GetTagsByNameSnippet(string snippet, int maxCount = 10) {
            if (String.IsNullOrEmpty(snippet)) return null; // Otherwise would return the whole dataset
            return _tagRepository.Fetch(tag => tag.TagName.StartsWith(snippet)).Take(maxCount);
        }

        public TagRecord GetTag(int tagId) {
            return _tagRepository.Get(x => x.Id == tagId);
        }

        public TagRecord GetTagByName(string tagName) {
            return _tagRepository.Get(x => x.TagName == tagName);
        }

        public TagRecord CreateTag(string tagName) {
            var result = _tagRepository.Get(x => x.TagName == tagName);
            if (result == null) {
                result = new TagRecord { TagName = tagName };
                _tagRepository.Create(result);
            }
            return result;
        }
{'fraction_non_alphanumeric': 0.06994576397980176, 'fraction_numerical': 0.0022442491116513932, 'mean_word_length': 2.4414414414414414, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2558557', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1302, 'n_words': 315}