public void UpdateManyToMany_NoExisting_AddedElements()
        {
            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 newParent = connection.Load <ParentClass>(parent.Id);

                Assert.Equal(2, newParent.Children.Count);
                var children = newParent.Children;
                Assert.True(children.Any(c => c.Child.Test == "First"));
                Assert.True(children.Any(c => c.Child.Test == "Second"));
                transaction.Rollback();
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Delete(int id)
        {
            var poll = session.Load <Poll <TUser> >(id);
            var user = await accountService.GetCurrentUserAsync();

            if (!await accountService.IsUser(user, poll.Author))
            {
                return(Unauthorized());
            }

            using (var transaction = session.BeginTransaction())
            {
                poll.Deleted = true;
                await session.UpdateAsync(poll);

                transaction.Commit();
            }
            return(Ok());
        }
Esempio n. 3
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));
            }
        }