Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
0
 public async Task <Snippet> GetSnippet(Guid id)
 {
     return(await _repository.GetById(id));
 }
Пример #4
0
        public Snippet GetSnippet(int id)
        {
            var snippet = snippetsRepository.GetById(id);

            return(snippet);
        }