Exemplo n.º 1
0
        /// <summary>
        /// register a new vote on an snippet
        /// </summary>
        /// <param name="newVote"></param>
        /// <returns></returns>
        public async Task <int> RegisterVote(Vote newVote)
        {
            var snippet = await _snippetRepository.GetById(newVote.ItemId);

            if (snippet == null)
            {
                return(0);  //No Snippet return zero
            }
            var existingVotes = await _voteRepository.GetByItemId(newVote.ItemId);

            if (existingVotes.Count(v => v.UserId == newVote.UserId) > 0)
            {
                return(snippet.Score);  //Already voted return current score
            }
            //create the vote
            var created = await _voteRepository.CreateVote(newVote);

            if (created.Id != Guid.Empty)
            {
                //update the score on the snippet model
                var voteCount = _voteRepository.GetByItemId(snippet.Id).Result.Sum(v => v.Value);
                if (voteCount != snippet.Score)
                {
                    snippet.Score = voteCount;
                }
                snippet = await _snippetRepository.Update(snippet);
            }
            return(snippet.Score);
        }
Exemplo n.º 2
0
        public async Task <int> FlagSnippet(Flag flag)
        {
            var getSnippet = _snippetRepository.GetById(flag.ItemId);
            var existing   = _flagRepository.GetAll().Where(f => f.ItemId == flag.ItemId && f.UserId == flag.UserId);

            if (existing.Count() == 0)
            {
                flag = _flagRepository.CreateFlag(flag);
            }
            var snippet = await getSnippet;

            snippet.Flags = _flagRepository.GetAll().Where(f => f.ItemId == flag.ItemId).Count();
            var res = await _snippetRepository.Update(snippet);

            return(res.Flags);
        }
Exemplo n.º 3
0
        public void UpdateSnippet(UpdateSnippet command, string userName)
        {
            var item = _snippetRepository.Get(command.Id);

            item.Name     = command.Name;
            item.Language = command.Language;
            item.Template = command.Template;
            item.Code     = command.Code;

            while (item.Variables.Count > command.Variables.Count)
            {
                item.Variables.RemoveAt(item.Variables.Count - 1);
            }

            while (item.Variables.Count < command.Variables.Count)
            {
                item.Variables.Add(new Variable());
            }

            for (int i = 0; i < item.Variables.Count; i++)
            {
                item.Variables[i].Name         = command.Variables[i].Name;
                item.Variables[i].RequestName  = command.Variables[i].RequestName;
                item.Variables[i].DefaultValue = command.Variables[i].DefaultValue;
            }

            _snippetRepository.Update(item);

            _publisher.PublishAsync(new SnippetUpdated(
                                        command.Id,
                                        command.Name,
                                        command.Language,
                                        command.Variables,
                                        command.Template,
                                        command.Code));
        }
Exemplo n.º 4
0
 public async Task <Snippet> UpdateSnippet(Snippet snippet)
 {
     return(await _repository.Update(snippet));
 }