Exemple #1
0
        public async Task <StorySummaryViewModel> Create(CreateViewModel model, string username, Guid userId)
        {
            UriBuilder uri = null;

            if (!string.IsNullOrEmpty(model.Url))
            {
                uri        = new UriBuilder(model.Url);
                uri.Scheme = "https";
                uri.Port   = uri.Port == 80 || uri.Port == 443 ? -1 : uri.Port;
            }

            var story = await StoriesDbContext.Stories.AddAsync(new Story()
            {
                Title = model.Title,
                DescriptionMarkdown = model.DescriptionMarkdown,
                Description         = CommonMarkConverter.Convert(model.DescriptionMarkdown),
                Url          = uri?.Uri.ToString(),
                UserId       = userId,
                UserIsAuthor = model.IsAuthor,
            });

            var rowCount = await StoriesDbContext.SaveChangesAsync();

            var hashIds = new Hashids(minHashLength: 5);

            VoteQueueService.QueueStoryVote(story.Entity.Id);

            return(MapToStorySummaryViewModel(story.Entity, hashIds.Encode(story.Entity.Id), userId, false));
        }
Exemple #2
0
        public async Task <bool> ToggleStoryVote(string hashId, Guid userId)
        {
            var storyId = new Hashids(minHashLength: 5).Decode(hashId)?.FirstOrDefault();

            if (storyId == 0)
            {
                return(false);
            }

            var story = await StoriesDbContext.Stories.Include(v => v.Votes).Where(o => o.Id == storyId).FirstOrDefaultAsync();

            if (story == null)
            {
                return(false);
            }

            var vote = story.Votes.FirstOrDefault(vo => vo.UserId == userId);

            if (vote != null)
            {
                story.Upvotes--;

                return(await DeleteVote(vote) > 0);
            }

            vote = new Vote
            {
                StoryId = storyId,
                UserId  = userId,
            };

            story.Upvotes++;

            VoteQueueService.QueueStoryVote(story.Id);

            return(await AddVote(vote) > 0);
        }