private async Task SaveImages(HtmlSanitizer html, TUser author)
        {
            foreach (var pair in html.Images)
            {
                var photo = session.SelectAllFrom <Photo <TUser> >().Where(p => p.FileName == pair.Key).SingleOrDefault();
                if (photo == null)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        pair.Value.Save(memoryStream);
                        ArraySegment <byte> buffer;
                        if (memoryStream.TryGetBuffer(out buffer))
                        {
                            var url =
                                await imageStore.SaveAsync(buffer.Array, memoryStream.Length, pair.Key);

                            photo = new Photo <TUser>
                            {
                                FileName     = pair.Key,
                                Url          = url,
                                CreationTime = DateTime.Now,
                                Width        = pair.Value.Width,
                                Height       = pair.Value.Height,
                                Length       = memoryStream.Length,
                                Uploader     = author
                            };
                            await session.SaveAsync(photo);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public async Task <IEnumerable <string> > Complete([FromQuery] string query)
        {
            var pattern = query.Replace("%", "") + "%";

            return((await session.SelectAllFrom <Data.Tags.Tag>().Where(n => n.Text.Like(pattern)).OrderBy(n => n.Text).Asc().Limit(0, 10).ToListAsync())
                   .Select(n => n.Text));
        }
        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))));
            }
        }
Beispiel #4
0
        public async Task <IHttpActionResult <IEnumerable <PollView <TUserView> > > > Get()
        {
            var polls = await session.SelectAllFrom <Poll <TUser> >()
                        .Where(x => !x.Deleted)
                        .OrderBy(x => x.OpenDate).Desc()
                        .Limit(0, 20)
                        .ToListAsync();

            return(Ok(polls.Select(x => pollDataMapping.ToPollDto(x))));
        }
Beispiel #5
0
        public async Task <IEnumerable <CommentView <TUserView> > > GetComments(TUser account, int newsId, bool descending = false)
        {
            var news = await session.LoadAsync <TThread>(newsId);

            var comments = (await session.SelectAllFrom <TCommentInThread>(x => x.Comment, x => x.Comment.Author).Where(c => c.Commentable == news).ToListAsync()).Select(x => x.Comment);

            if (descending)
            {
                return(comments.OrderByDescending(c => c.CreationDate).Select(c => commentMapping.ToCommentView(c)));
            }

            return(comments.OrderBy(c => c.CreationDate).Select(c => commentMapping.ToCommentView(c)));
        }
        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)));
            }
        }
        public void UpdateManyToMany_TwoExistingElement_AddOneElement()
        {
            using (var transaction = connection.BeginTransaction())
            {
                parent = new ParentClass();
                connection.Save(parent);

                var newChildren = new[] { new ChildDto {
                                              Test = "First"
                                          }, new ChildDto {
                                              Test = "Second"
                                          } };
                connection.UpdateManyToMany(parent, parent.Children, newChildren, mapper);

                connection.Cache.Clear();

                var first  = connection.SelectAllFrom <ChildClass>().Where(c => c.Test == "First").Single();
                var second = connection.SelectAllFrom <ChildClass>().Where(c => c.Test == "Second").Single();

                var newParent        = connection.Load <ParentClass>(parent.Id);
                var modifiedChildren = new[] { new ChildDto {
                                                   Test = "First", Id = first.Id
                                               }, new ChildDto {
                                                   Test = "Second", Id = second.Id
                                               }, new ChildDto {
                                                   Test = "Third"
                                               } };
                connection.UpdateManyToMany(newParent, newParent.Children, modifiedChildren, mapper);

                connection.Cache.Clear();

                var final = connection.Load <ParentClass>(parent.Id);
                Assert.Equal(3, final.Children.Count);
                Assert.True(final.Children.Any(c => c.Child.Test == "First"));
                Assert.True(final.Children.Any(c => c.Child.Test == "Second"));
                Assert.True(final.Children.Any(c => c.Child.Test == "Third"));
                transaction.Rollback();
            }
        }
Beispiel #8
0
        public async Task <bool> ExistsAsync(Adjective adj)
        {
            Adjective dbAdj = await _session.SelectAllFrom <Adjective>().Where(x => x.Value == adj.Value).FirstOrDefaultAsync();

            return(dbAdj == null ? false : true);
        }
 public override Task <IList <User> > Search(UserSearchFilter name, int offset, int limit, string sortColumn)
 {
     return(connection.SelectAllFrom <User>().OrderBy(x => x.UserName).Limit(offset, limit).ToListAsync());
 }
Beispiel #10
0
        public async Task <bool> ExistsAsync(Noun noun)
        {
            Noun dbNoun = await _session.SelectAllFrom <Noun>().Where(x => x.Value == noun.Value).FirstOrDefaultAsync();

            return(dbNoun == null ? false : true);
        }
Beispiel #11
0
 public IList <TChild> QueryExisting(IFolkeConnection connection, IList <int> dto)
 {
     return(dto.Any() ? connection.SelectAllFrom <TChild>().Where(c => c.Id.In(dto)).ToList() : null);
 }
Beispiel #12
0
            public IList <TChild> QueryExisting(IFolkeConnection connection, IList <TDto> dto)
            {
                var existingChildrenIds = dto.Where(c => c.Id != 0).Select(c => c.Id).ToArray();

                return(existingChildrenIds.Any() ? connection.SelectAllFrom <TChild>().Where(c => c.Id.In(existingChildrenIds)).ToList() : null);
            }
Beispiel #13
0
 public IList <Product> Search(string partNumber)
 {
     return(session.SelectAllFrom <Product>()
            .Where(x => x.PartNumber.Like("%" + partNumber + "%"))
            .ToList());
 }
Beispiel #14
0
 public async Task <IEnumerable <ExternalLinkView> > GetAll()
 {
     return((await session.SelectAllFrom <ExternalLink>().ToListAsync()).Select(l => miscDataMapping.ToExternalLinkDto(l)));
 }
Beispiel #15
0
 public IList <Data.Tags.Tag> QueryExisting(IFolkeConnection connection, IList <string> dto)
 {
     return(connection.SelectAllFrom <Data.Tags.Tag>().Where(t => t.Text.In(dto)).ToList());
 }
Beispiel #16
0
        public async Task <IActionResult> Get(int imageId, string format)
        {
            int imageWidth;
            int imageHeight;

            switch (format)
            {
            case "thumb":
                imageWidth  = 256;
                imageHeight = 256;
                break;

            default:
                return(BadRequest("Unknown format"));
            }

            using (var transaction = session.BeginTransaction())
            {
                var photo = session.SelectAllFrom <Photo <TUser> >().SingleOrDefault(x => x.Original.Id == imageId && x.Width == imageWidth && x.Height == imageHeight);
                if (photo == null)
                {
                    var original = session.Load <Photo <TUser> >(imageId);
                    if (original.Original != null)
                    {
                        return(BadRequest());
                    }

                    var fileName = original.FileName.Replace(".", "." + format + ".");
                    using (var originalStream = imageStore.Load(original.FileName))
                    {
                        var fullSizeImage = new  Image(originalStream);
                        var resizedImage  = fullSizeImage.ResizeWithRatio(imageWidth, imageHeight);
                        using (var stream = new MemoryStream())
                        {
                            resizedImage.Save(stream);
                            ArraySegment <byte> buffer;
                            if (stream.TryGetBuffer(out buffer))
                            {
                                var url = await imageStore.SaveAsync(buffer.Array, stream.Length, fileName);

                                photo = new Photo <TUser>
                                {
                                    FileName     = fileName,
                                    CreationTime = DateTime.UtcNow,
                                    Height       = imageHeight,
                                    Width        = imageWidth,
                                    Original     = original,
                                    Length       = stream.Length,
                                    Url          = url,
                                    Uploader     = original.Uploader
                                };
                                await session.SaveAsync(photo);

                                transaction.Commit();
                            }
                        }
                    }
                }

                return(Redirect(photo.Url));
            }
        }