Exemple #1
0
        public async Task <CommentView <TUserView> > PostComment(TUser account, int newsId, CommentView <TUserView> value)
        {
            if (account == null)
            {
                return(null);
            }
            using (var transaction = session.BeginTransaction())
            {
                var news = await session.LoadAsync <TThread>(newsId);

                var comment = new Comment <TUser> {
                    Author = account, Text = value.Text, CreationDate = DateTime.UtcNow
                };
                await session.SaveAsync(comment);

                news.NumberOfComments++;
                await session.UpdateAsync(news);

                var commentInNews = new TCommentInThread {
                    Comment = comment, Commentable = news
                };
                await session.SaveAsync(commentInNews);

                transaction.Commit();
                return(commentMapping.ToCommentView(comment));
            }
        }
Exemple #2
0
        public async Task <IEnumerable <ChatView <TUserView> > > GetAll([FromQuery] int offset = 0)
        {
            var account = await userService.GetCurrentUserAsync();

            if (account != null && offset == 0)
            {
                using (var transaction = session.BeginTransaction())
                {
                    var myLastChat = await queryLastChatViewByAccount.Build(session, account).FirstOrDefaultAsync();

                    var lastChat = await queryLastChats.Build(session).FirstOrDefaultAsync();

                    if (myLastChat == null)
                    {
                        myLastChat = new LastChatView <TUser> {
                            Account = account, Chat = lastChat
                        };
                        await session.SaveAsync(myLastChat);

                        transaction.Commit();
                    }
                    else if (lastChat != myLastChat.Chat)
                    {
                        myLastChat.Chat = lastChat;
                        await session.UpdateAsync(myLastChat);

                        transaction.Commit();
                    }
                }
            }
            return((await queryChats.Build(session, offset).ToListAsync()).Select(c => chatDataMapping.ToChatDto(c)));
        }
        public async Task <IHttpActionResult <IEnumerable <ThreadView <TUserView> > > > GetFromForum(int forumId, [FromQuery] int offset = 0, [FromQuery] int limit = 10)
        {
            var forum = await session.LoadAsync <Data.Forums.Forum>(forumId);

            var account = await accountService.GetCurrentUserAsync();

            if (forum.ReadRole != null && !await accountService.HasRole(account, forum.ReadRole))
            {
                return(Unauthorized <IEnumerable <ThreadView <TUserView> > >());
            }

            if (account != null)
            {
                using (var transaction = session.BeginTransaction())
                {
                    var lastViewed = await session.SelectAllFrom <LastForumView <TUser> >().Where(l => l.Forum == forum && l.Account.Equals(account)).FirstOrDefaultAsync();

                    if (lastViewed == null)
                    {
                        lastViewed = new LastForumView <TUser> {
                            Account = account, Forum = forum, LastView = DateTime.UtcNow
                        };
                        await session.SaveAsync(lastViewed);
                    }
                    else
                    {
                        lastViewed.LastView = DateTime.UtcNow;
                        await session.UpdateAsync(lastViewed);
                    }
                    transaction.Commit();
                }

                var query = session.Select <ThreadBean>().All(x => x.Thread).All(x => x.LastViewed).All(x => x.Thread.Author).From(x => x.Thread).LeftJoin(x => x.LastViewed).On(x => x.Thread == x.LastViewed.Thread)
                            .AndOn(t => t.LastViewed.Account.Equals(account))
                            .LeftJoinOnId(x => x.Thread.Author)
                            .Where(t => t.Thread.Forum == forum).OrderBy(t => t.Thread.Sticky).Desc().OrderBy(t => t.Thread.CreationDate).Desc().Limit(offset, limit);

                var results = await query.ToListAsync();

                return(Ok(results.Select(b => forumsDataMapping.ToThreadView(b.Thread, b.LastViewed))));
            }
            else
            {
                var query = session.SelectAllFrom <Thread <TUser> >(x => x.Author).Where(t => t.Forum == forum).OrderBy(t => t.Sticky).Desc().OrderBy(t => t.CreationDate).Desc().Limit(offset, limit);
                return(Ok((await query.ToListAsync()).Select(t => forumsDataMapping.ToThreadView(t, null))));
            }
        }
Exemple #4
0
        public async Task <IActionResult> Put(int id, [FromBody] PollView <TUserView> pollView)
        {
            using (var transaction = session.BeginTransaction())
            {
                // Update poll
                var poll = await session.LoadAsync <Poll <TUser> >(id);

                poll.Question = pollView.Question;

                var newAnswers = pollView.PossibleAnswers.Where(a => a.Text != null).ToList();

                // Update or add modified possible answers
                foreach (var possibleAnswerDto in newAnswers)
                {
                    var possibleAnswer = poll.PossibleAnswers.SingleOrDefault(p => p.Id == possibleAnswerDto.Id);

                    if (possibleAnswer != null)
                    {
                        possibleAnswer.Text = possibleAnswerDto.Text.Trim();

                        if (possibleAnswer.Text.Length >= 1)
                        {
                            possibleAnswer.Text = possibleAnswerDto.Text;
                            await session.UpdateAsync(possibleAnswer);
                        }
                    }
                    else
                    {
                        await session.SaveAsync(new PollPossibleAnswer <TUser>
                        {
                            Text = possibleAnswerDto.Text,
                            Poll = poll
                        });
                    }
                }

                // Delete from db possible answers no longer in the Dto (removed by user)
                foreach (var answer in poll.PossibleAnswers.Where(p => newAnswers.All(u => u.Id != p.Id)))
                {
                    await session.DeleteAsync(answer);
                }
                await session.UpdateAsync(poll);

                transaction.Commit();
            }
            return(Ok());
        }
Exemple #5
0
        public async Task <IActionResult> Put(int id, [FromBody] ExternalLinkView value)
        {
            using (var transaction = session.BeginTransaction())
            {
                var link = await session.LoadAsync <ExternalLink>(id);

                link.Title = value.Title;
                link.Url   = value.Url;
                await session.UpdateAsync(link);

                transaction.Commit();
                return(Ok());
            }
        }
Exemple #6
0
        public async Task <ActionResult> Put(int id, [FromBody] ForumView value)
        {
            using (var transaction = session.BeginTransaction())
            {
                var forum = await session.LoadAsync <Data.Forums.Forum>(id);

                forum.ReadRole  = value.ReadRole;
                forum.WriteRole = value.WriteRole;
                forum.Name      = value.Name;
                await session.UpdateAsync(forum);

                transaction.Commit();
            }
            return(Ok());
        }
        public async Task <IHttpActionResult <PollChosenAnswerView <TUserView> > > AddAnswer(int pollId, [FromBody] PollPossibleAnswerView value)
        {
            // Checks that provided Poll is visible
            var poll = await session.LoadAsync <Poll <TUser> >(pollId);

            if (poll.Deleted)
            {
                return(BadRequest <PollChosenAnswerView <TUserView> >("Deleted poll"));
            }

            var account = await accountService.GetCurrentUserAsync();

            // Checks that account hasn't voted yet
            var answers = await session.SelectAllFrom <PollChosenAnswer <TUser> >()
                          .Where(x => x.Account.Equals(account))
                          .SingleOrDefaultAsync();

            if (answers != null)
            {
                return(BadRequest <PollChosenAnswerView <TUserView> >("Already answered"));
            }

            using (var transaction = session.BeginTransaction())
            {
                var answer = await session.LoadAsync <PollPossibleAnswer <TUser> >(value.Id);

                if (answer.Poll != poll)
                {
                    return(BadRequest <PollChosenAnswerView <TUserView> >("Not a possible answer"));
                }

                var chosenAnswer = new PollChosenAnswer <TUser>
                {
                    Poll    = poll,
                    Answer  = answer,
                    Account = account
                };

                await session.SaveAsync(chosenAnswer);

                answer.Count++;
                await session.UpdateAsync(answer);

                transaction.Commit();

                return(Created("GetPollChosenAnswer", chosenAnswer.Id, pollDataMapping.ToPollChosenAnswerDto(chosenAnswer)));
            }
        }
Exemple #8
0
        public async Task Put(int id, [FromBody] CommentView <TUserView> value)
        {
            var account = await userService.GetCurrentUserAsync();

            using (var transaction = session.BeginTransaction())
            {
                var comment = await session.LoadAsync <Comment <TUser> >(id);

                if (await userService.IsUser(account, comment.Author))
                {
                    comment.Text = value.Text;
                    await session.UpdateAsync(comment);

                    transaction.Commit();
                }
            }
        }
Exemple #9
0
        public async Task <Adjective> UpdateAsync(Adjective adj)
        {
            await _session.UpdateAsync(adj);

            return(adj);
        }
Exemple #10
0
        public async Task <Noun> UpdateAsync(Noun noun)
        {
            await _session.UpdateAsync(noun);

            return(noun);
        }