Exemple #1
0
 public override Task <Reply> IncreaseTag(IncDecTagRequest request, ServerCallContext context)
 {
     return(IncreaseOrDecreaseTag(request, 1));
 }
Exemple #2
0
        private async Task <Reply> IncreaseOrDecreaseTag(IncDecTagRequest request, int inc)
        {
            var reply = new Reply();

            if (!Guid.TryParse(request.TagId, out var tagId) || !Guid.TryParse(request.FileId, out var fileId))
            {
                reply.Error = Error.InvalidArguments;
                return(reply);
            }

            var tag = await _tagsService.All().FirstOrDefaultAsync(t => t.Id == tagId);

            if (tag == null)
            {
                reply.Error = Error.NoSuchEntity;
                return(reply);
            }

            var file = await _service.All().Include(f => f.FileTags).FirstOrDefaultAsync(f => f.Id == fileId);

            if (file == null)
            {
                reply.Error = Error.NoSuchEntity;
                return(reply);
            }

            var editableTags = await _keyValueService.All()
                               .FirstOrDefaultAsync(kv => kv.Id == ServerKeys.IncreaseTags.Key);

            if (editableTags == null || string.IsNullOrWhiteSpace(editableTags.Value))
            {
                reply.Error = Error.InvalidOperation;
                return(reply);
            }

            var canEdit = editableTags.Value.Split(' ').Any(s => s.Trim() == tag.Name);

            if (!canEdit)
            {
                reply.Error = Error.InvalidOperation;
                return(reply);
            }

            file.FileTags ??= new List <Domain.Models.FileTag>();

            var fileTag = file.FileTags.FirstOrDefault(t => t.TagId == tagId);

            if (fileTag == null)
            {
                fileTag = new Domain.Models.FileTag
                {
                    TagId  = tagId,
                    FileId = fileId,
                    Value  = Math.Max(inc, 0).ToString()
                };
                file.FileTags.Add(fileTag);
            }
            else
            {
                if (!int.TryParse(fileTag.Value, out var cur))
                {
                    cur = 0;
                }

                fileTag.Value = Math.Max(cur + inc, 0).ToString();
            }

            await _service.Update(file);

            return(reply);
        }