网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
namespace Dpz.Core.Service.V4.Implements;

public class PictureRecordService : IPictureRecordService
{
    private readonly IHybridCachingProvider _cachingProvider;
    private readonly IRepository<PictureRecord> _repository;
    private readonly IMapper _mapper;

    public PictureRecordService(IHybridCachingProvider cachingProvider, IRepository<PictureRecord> repository,
        IMapper mapper)
    {
        _cachingProvider = cachingProvider;
        _repository = repository;
        _mapper = mapper;
    }

    public async Task<ICollection<string>> GetTagsAsync()
    {
        var tags = await _repository.MongodbQueryable
            //.Select(x => x.Tags)
            .SelectMany(x => x.Tags)
            .GroupBy(x => x)
            .Select(x => x.Key)
            .ToListAsync();
        return tags;
    }

    public async Task SetBannerAsync(string id)
    {
        var entity = await _repository.TryGetAsync(id);
        if (entity != null)
        {
            var tags = new List<string>(entity.Tags);
            if (entity.Tags.Contains("banner"))
                tags = entity.Tags.Where(x => x != "banner").ToList();
            else
                tags.Add("banner");
            var update = Builders<PictureRecord>.Update
                .Set(x => x.Tags, tags);
            await _repository.UpdateAsync(x => x.Id == entity.Id, update);
        }
    }

    public async Task<ICollection<VmPictureRecord>> GetBannerAsync()
    {
        var pagedList = await _repository
            .SearchFor(x => x.Tags.Any(y => y == "banner"))
            .OrderByDescending(x => x.UploadTime).ToListAsync<PictureRecord, VmPictureRecord>();
        pagedList.ForEach(x => x.AccessUrl += "!banner");
        return pagedList;
    }

    public async Task<IPagedList<VmPictureRecord>> GetPagesAsync(ICollection<string>? tags, string? description,
        int pageIndex = 1, int pageSize = 20)
    {
        var filterEmpty = Builders<PictureRecord>.Filter.Empty;
        var filters = new List<FilterDefinition<PictureRecord>>();

        var clearTags = tags?.Where(x => !string.IsNullOrEmpty(x))
            .Select(x => x)
            .ToList() ?? new List<string>();
        if (clearTags.Any())
        {
            var filter = Builders<PictureRecord>.Filter.AnyIn(x => x.Tags, clearTags);
            filters.Add(filter);
        }

        if (!string.IsNullOrEmpty(description))
        {
            var filter =
                Builders<PictureRecord>.Filter.Regex(x => x.Description, new BsonRegularExpression(description, "i"));
            filters.Add(filter);
        }

        var filterResult = filters.Any() ? Builders<PictureRecord>.Filter.And(filters) : filterEmpty;

        return await _repository
            .SearchFor(filterResult)
            .SortByDescending(x => x.Id)
            .ToPagedListAsync<PictureRecord, VmPictureRecord>(pageIndex, pageSize);
    }

    public async Task<VmPictureRecord?> GetPictureRecordAsync(string id)
    {
        var entity = await _repository.TryGetAsync(id);
        return entity == null ? null : _mapper.Map<VmPictureRecord>(entity);
    }

    public async Task<VmPictureRecord> CreateRecordAsync(VmPictureRecord pictureRecord)
    {
        var entity = _mapper.Map<PictureRecord>(pictureRecord);
        await _repository.InsertAsync(entity);
        if (entity.Tags.Any(x => x == "banner"))
        {
            await _cachingProvider.SetAsync(CacheKey.BannerKey, await GetBannerAsync(), TimeSpan.FromHours(12));
        }

        return _mapper.Map<VmPictureRecord>(entity);
    }

    public async Task<VmPictureRecord?> ChangeInformationAsync(VmPictureRecordChange change)
    {
        if (!ObjectId.TryParse(change.Id, out var oid))
            return null;

        var update = Builders<PictureRecord>.Update
            .Set(x => x.Category, change.Category)
            .Set(x => x.Description, change.Description)
            .Set(x => x.Tags, change.Tags);

        await _repository.UpdateAsync(x => x.Id == oid, update);

        if (change.Tags.Any(x => x == "banner"))
        {
            await _cachingProvider.SetAsync(CacheKey.BannerKey, await GetBannerAsync(), TimeSpan.FromHours(12));
        }

        var entity = await _repository.FindAsync(oid);
        return entity == null ? null : _mapper.Map<VmPictureRecord>(entity);
    }

    public async Task DeleteAsync(string id)
    {
        var entity = await _repository.TryGetAsync(id);
        if (entity == null) return;

        if (entity.Tags.Any(x => x == "banner"))
        {
            await _cachingProvider.SetAsync(CacheKey.BannerKey, await GetBannerAsync(), TimeSpan.FromHours(12));
        }

        await _repository.DeleteAsync(entity.Id);
    }
}
loading