Esempio n. 1
0
        public IdeaModule(IIdeaRepository ideas, IUserRepository users, ISettingsRepository settings, IImageRepository imageRepository)
            : base("/idea")
        {
            _ideas = ideas;
            _settings = settings;
            _users = users;

            Get["/{id}"] = parameters =>
            {
                int id = parameters.id;
                var idea = _ideas.Get(id);
                if (idea == null)
                    return View["404"];

                User user = Context.GetCurrentUser(_users);
                if (user != null)
                {
                    if (idea.Votes.Any(u => u.UserId == user.Id))
                        idea.UserHasVoted = true;

                }

                var viewModel = new IdeaViewModel(idea);
                var model = Context.Model(string.Format("{0} - {1}", idea.Title, _settings.Title));
                model.Idea = viewModel;
                return View["Idea/Index", model];
            };

            Get["/image/{id}"] = parameters =>
            {
                var id = (string)parameters.id;
                if (id.Contains("."))
                {
                    id = id.Substring(0, id.IndexOf(".")); //string .jpg in case it was send in
                }
                var image = imageRepository.Get(int.Parse(id));
                // TODO: format should be adaptive based on backing source?
                return Response.FromStream(new MemoryStream(image.ImageBits), "image/jpeg");
            };

            Get[@"/imagethumb/{id}/{width}"] = parameters =>
            {
                var image = (Image)imageRepository.Get(parameters.id);
                using (var memoryStream = new MemoryStream(image.ImageBits))
                {
                    var drawingImage = System.Drawing.Image.FromStream(memoryStream);
                    var thumb = drawingImage.ToThumbnail((int)parameters.width);
                    using (var thumbnailStream = new MemoryStream())
                    {
                        // TODO: format should be adaptive based on backing source?
                        thumb.Save(thumbnailStream, ImageFormat.Jpeg);
                        return Response.FromStream(new MemoryStream(thumbnailStream.GetBuffer()), "image/jpeg"); //massive WTF? If I just use thumnailStream, it doesn't work...
                    }
                }
            };
        }
Esempio n. 2
0
        public IdeaModule(IIdeaRepository ideas, IUserRepository users, Settings settings, IImageRepository imageRepository)
            : base("/idea")
        {
            _ideas = ideas;
            _settings = settings;
            _users = users;

            Get["/{id}"] = parameters =>
            {
                int id = parameters.id;
                var idea = _ideas.Get(id);
                if (idea == null)
                    return View["404"];

                User user = Context.GetCurrentUser(_users);
                if (user != null)
                {
                    if (idea.Votes.Any(u => u.UserId == user.Id))
                        idea.UserHasVoted = true;

                }

                var viewModel = new IdeaViewModel(idea);
                var model = Context.Model(string.Format("{0} - {1}", idea.Title, (string)_settings.SiteTitle));
                model.Idea = viewModel;
                return View["Idea/Index", model];
            };

            Get["/{id}/activity"] = parameters =>
            {
                int id = parameters.id;
                var idea = _ideas.Get(id);
                if (idea == null)
                    return Response.AsJson(new { Status = "error" });

                var results = idea.Activities.OrderBy(a => a.Time).Select(MapToViewModel);

                return Response.AsJson(new
                {
                    Status = "success",
                    Items = results
                });
            };

            Get["/image/{id}"] = parameters =>
            {
                var id = (string)parameters.id;
                if (id.Contains("."))
                {
                    id = id.Substring(0, id.IndexOf(".")); //string .jpg in case it was send in
                }
                var image = imageRepository.Get(int.Parse(id));
                // TODO: format should be adaptive based on backing source?
                return Response.FromStream(new MemoryStream(image.ImageBits), "image/jpeg");
            };

            Get[@"/imagethumb/{id}/{width}"] = parameters =>
            {
                var image = (Image)imageRepository.Get(parameters.id);
                using (var memoryStream = new MemoryStream(image.ImageBits))
                {
                    var drawingImage = System.Drawing.Image.FromStream(memoryStream);
                    int thumbWidth = (int)parameters.width;
                    if (thumbWidth > int.Parse(_settings.MaxThumbnailWidth))
                    {
                        thumbWidth = int.Parse(_settings.MaxThumbnailWidth);
                    }
                    var thumb = drawingImage.ToThumbnail(thumbWidth);
                    using (var thumbnailStream = new MemoryStream())
                    {
                        // TODO: format should be adaptive based on backing source?
                        thumb.Save(thumbnailStream, ImageFormat.Jpeg);
                        return Response.FromStream(new MemoryStream(thumbnailStream.GetBuffer()), "image/jpeg"); //massive WTF? If I just use thumnailStream, it doesn't work...
                    }
                }
            };

            Get["/imagesforidea/{id}"] = parameters =>
                                             {
                                                 Idea idea = _ideas.Get((int)parameters.id);
                                                 return Response.AsJson(idea.Images.Select(i => new ImageFileStatus(i.Id, i.ImageBits.Length, i.Name))
                                                                                            .ToArray()
                                                                            ).WithHeader("Vary", "Accept");
                                             };
        }
Esempio n. 3
0
        public IdeaModule(IIdeaRepository ideas, ISettingsRepository settings)
            : base("/idea")
        {
            _ideas = ideas;
            _settings = settings;

            Get["/new"] = _ => View["Idea/New", new
            {
                Title = string.Format("New Idea - {0}", _settings.Title),
                Ideas = _ideas.GetAll()
            }];

            Get["/{id}/edit"] = parameters =>
            {
                int id = parameters.id;
                var idea = _ideas.Get(id);

                if (idea == null)
                    return View["404"];

                return View["Idea/Edit", new
                {
                    Title = string.Format("Edit Idea: '{0}' - {1}", idea.Title, _settings.Title),
                    PopularIdeas = _ideas.GetAll(),
                    Idea = idea
                }];
            };

            Get["/{id}"] = parameters =>
                               {
                                   int id = parameters.id;
                                   var idea = _ideas.Get(id);
                                   if (idea == null)
                                       return View["404"];

                                   var viewModel = new IdeaViewModel(idea) { UserHasVoted = false };

                                   return View["Idea/Index",
                                       new
                                       {
                                           Title = string.Format("{0} - {1}", idea.Title, _settings.Title),
                                           Idea = viewModel,
                                           UserId = 2 // TODO: not hard-code these
                                       }];
                               };

            // save result of edit to database
            Post["/{id}/edit"] = parameters =>
            {
                int id = parameters.id;
                var idea = _ideas.Get(id);
                if (idea == null)
                    return View["404"];

                idea.Title = Request.Form.Title;
                idea.Description = Request.Form.Description;

                _ideas.Save();

                return Response.AsRedirect(string.Format("/idea/{0}", idea.Id));
            };

            // save result of create to database
            Post["/new"] = _ =>
            {
                var i = new Idea
                            {
                                Time = DateTime.UtcNow,
                                Title = Request.Form.Title,
                                Description = Request.Form.Description,
                            };

                ideas.Add(i);

                return Response.AsRedirect("/idea/" + i.Id);
            };

            // someone else votes for the idea
            Post["/{id}/vote/{userid}"] = parameters =>
            {
                int votes = ideas.Vote(parameters.id, parameters.userid, 1);

                return Response.AsJson(new
                                {
                                    Status = "OK",
                                    NewVotes = votes
                                });
            };

            // the user decides to repeal his vote
            Post["/{id}/unvote/{userid}"] = parameters =>
            {
                int votes = ideas.Unvote(parameters.id, parameters.userid);

                return Response.AsJson(new
                {
                    Status = "OK",
                    NewVotes = votes
                });
            };

            Post["/{id}/delete"] = parameters =>
            {
                int id = parameters.id;
                ideas.Delete(id);
                ideas.Save();

                // TODO: test
                return Response.AsJson(new
                {
                    Status = "Error"
                });
            };
        }