public async Task AddCountAsync(string tag, int count)
    {
        var bkTag = await _tagsRepo.GetAsync(tag);

        if (bkTag != null)
        {
            bkTag.ClickedCount += count;
            bkTag.LastClickTime = _clock.UtcNow;
            await _tagsRepo.UpsertAsync(bkTag);
        }
    }
Example #2
0
    public async Task <string> CreateAfCodeAsync(string url, AfCodeType codeType)
    {
        var bk = await _bkRepository.GetAsync(url);

        if (bk == null)
        {
            return(string.Empty);
        }

        var code = new AfCode
        {
            Title = bk.Title,
            Url   = bk.Url,
            Tags  = bk.Tags?.ToArray() ?? Array.Empty <string>(),
        };

        switch (codeType)
        {
        case AfCodeType.JsonBase64:
        {
            var json         = JsonSerializer.Serialize(code);
            var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
            return(CreateCode(base64String));
        }

        case AfCodeType.CompressionJsonBase64:
        {
            var json           = JsonSerializer.Serialize(code);
            var input          = Encoding.UTF8.GetBytes(json).AsMemory();
            var readOnlyMemory = Compress(input);
            var base64String   = Convert.ToBase64String(readOnlyMemory.Span);
            return(CreateCode(base64String));
        }

        case AfCodeType.Cloud:
            throw new NotImplementedException();

        case AfCodeType.PlainText:
        {
            var json = JsonSerializer.Serialize(code);
            return(CreateCode(json));
        }

        default:
            throw new ArgumentOutOfRangeException(nameof(codeType), codeType, null);
        }

        string CreateCode(string payload)
        {
            return($"{Consts.AfCodeSchemaPrefix}{codeType:D}{payload}");
        }
    }
Example #3
0
    public async Task <T> GetOrDefaultAsync <T>() where T : ISimpleData, new()
    {
        var id     = GetId(typeof(T));
        var entity = await _repo.GetAsync(id);

        if (entity == null)
        {
            return(new T());
        }

        var re = await JsonHelper.DeserializeAsync <T>(entity.PayloadJson);

        return(re !);
    }
Example #4
0
    private async Task AppendTagsAsync(string tag)
    {
        var oldTag = await _tagsRepo.GetAsync(tag);

        if (oldTag == null)
        {
            _logger.LogInformation("A new tag {Tag} added to all collection", tag);
            oldTag = new BkTag
            {
                Tag      = tag,
                TagAlias = new()
            };
            await _tagsRepo.UpsertAsync(oldTag);
        }
    }
Example #5
0
    public async Task AddClickAsync(string url, int moreCount)
    {
        var bk = await _bkRepo.GetAsync(url);

        if (bk == null)
        {
            return;
        }

        bk.ClickedCount += moreCount;
        bk.LastClickTime = _clock.UtcNow;
        await _bkRepo.UpsertAsync(bk);
    }
Example #6
0
    public static async Task <T?> GetSingleOneAsync <T>(this IIndexedDbRepo <T, string> repo) where T : IEntity <string>
    {
        var re = await repo.GetAsync(Consts.SingleOneDataId);

        return(re);
    }