Ejemplo n.º 1
0
        private async Task <int> GetForumNewMessages(Data.Forums.Forum forum, IEnumerable <LastForumView <TUser> > lastViewed)
        {
            var last = lastViewed.Where(l => l.Forum == forum).Select(f => f.LastView).SingleOrDefault();
            var numberOfNewMessages = await session.Select <Thread <TUser> >().CountAll().From().Where(t => t.Forum == forum && t.CreationDate > last).ScalarAsync <int>();

            numberOfNewMessages += await session.Select <CommentInThread <TUser> >().CountAll().From().LeftJoinOnId(x => x.Commentable).LeftJoinOnId(x => x.Comment).Where(x => x.Comment.CreationDate > last && x.Commentable.Forum == forum).ScalarAsync <int>();

            return(numberOfNewMessages);
        }
Ejemplo n.º 2
0
        public async Task <bool> GetNewChats()
        {
            var account = await userService.GetCurrentUserAsync();

            if (account == null)
            {
                return(false);
            }
            var myLastChat = session.Select <LastChatView <TUser> >().Values(x => x.Chat).From().Where(c => c.Account.Equals(account)).Scalar <int>();
            var lastChat   = session.Select <Chat <TUser> >().Values(x => SqlFunctions.Max(x.Id)).From().Scalar <int>();

            return(lastChat > myLastChat);
        }
Ejemplo n.º 3
0
        public async Task <Adjective> ReadRandomAsync()
        {
            int count = await _session.Select <Adjective>().CountAll().From().ScalarAsync <int>();

            int       offset = HelperService.GetRandomNumber(0, count);
            Adjective adj    = await _session.SelectAllFrom <Adjective>().OrderBy(x => x.Value).Limit(offset, 1).FirstOrDefaultAsync();

            return(adj);
        }
Ejemplo n.º 4
0
        public async Task <IHttpActionResult <PollAndVoteView <TUserView> > > GetPollAndVote(int id)
        {
            var account = await accountService.GetCurrentUserAsync();

            var poll = await session.Select <PollAndVote>().All(x => x.Poll).All(x => x.Answer).From(x => x.Poll).LeftJoin(x => x.Answer).On(x => x.Poll == x.Answer.Poll).AndOn(x => x.Answer.Account.Equals(account)).Where(x => x.Poll.Id == id).SingleOrDefaultAsync();

            if (poll == null || poll.Poll.Deleted)
            {
                return(NotFound <PollAndVoteView <TUserView> >());
            }
            return(Ok(pollDataMapping.ToPollAndVoteDto(poll.Poll, poll.Answer)));
        }
Ejemplo n.º 5
0
        public async Task <Noun> ReadRandomAsync()
        {
            try
            {
                int count = await _session.Select <Noun>().CountAll().From().ScalarAsync <int>();

                int  offset = HelperService.GetRandomNumber(0, count);
                Noun noun   = await _session.SelectAllFrom <Noun>().OrderBy(x => x.Value).Limit(offset, 1).FirstOrDefaultAsync();

                return(noun);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.ToString());
            }
        }
Ejemplo n.º 6
0
        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))));
            }
        }