Source

CodeSmackdown / CodeSmackdown.Site / Controllers / SnippetsController.cs

Full commit
OJ Reeves 55e2b1f 
OJ Reeves b92e1db 
OJ Reeves 55e2b1f 
OJ Reeves 80c5c33 
OJ Reeves 55e2b1f 



OJ Reeves b92e1db 
OJ Reeves 55e2b1f 
OJ Reeves b3e1d1b 

OJ Reeves 55e2b1f 

OJ Reeves 1b2fcb2 

OJ Reeves 55e2b1f 



OJ Reeves 80c5c33 
OJ Reeves 55e2b1f 

OJ Reeves 80c5c33 
OJ Reeves 3a62f57 

secretgeek bfff11a 

OJ Reeves 80c5c33 
OJ Reeves 3a62f57 

OJ Reeves 80c5c33 
OJ Reeves b3e1d1b 
OJ Reeves 80c5c33 







OJ Reeves 55e2b1f 

secretgeek c19c319 

OJ Reeves 3a62f57 



OJ Reeves 55e2b1f 

OJ Reeves 3a62f57 
OJ Reeves 55e2b1f 
OJ Reeves 80c5c33 












OJ Reeves b3e1d1b 
OJ Reeves 80c5c33 

secretgeek bfff11a 
OJ Reeves 29891c8 
secretgeek bfff11a 

OJ Reeves 29891c8 
secretgeek 742b083 

OJ Reeves 29891c8 

secretgeek 742b083 
OJ Reeves 29891c8 
secretgeek c19c319 
secretgeek 742b083 

OJ Reeves 29891c8 




secretgeek c19c319 
OJ Reeves 29891c8 






secretgeek c19c319 
secretgeek 742b083 

OJ Reeves 55e2b1f 
using System.Web.Mvc;
using CodeSmackdown.Domain.Models;
using CodeSmackdown.Domain.Services;
using CodeSmackdown.Site.Extensions;
using CodeSmackdown.Site.Models;

namespace CodeSmackdown.Site.Controllers
{
    public class SnippetsController : CodeSmackdownController
    {
        public const string SnippetRedirectKey = "RedirectSnippet";

        private readonly ISnippetService _snippetService;

        public SnippetsController(IUserService userService, ISnippetService snippetService)
            : base(userService)
        {
            _snippetService = snippetService;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            return ViewSnippet(_snippetService.GetRandomSnippet());
        }


        //@OJ: any reason why get is specified? i've always left off get specifications, and only put in posts --lb
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult View(int id)
        {
            // attempt to see if we were redirected here first
            var snippet = TempData.Read<Snippet>(SnippetRedirectKey);

            // if we were and the snippet matches, then use it,
            // otherwise load it from the back-end
            if (snippet == null || snippet.SnippetId != id)
            {
                snippet = _snippetService.GetSnippet(id);
            }

            var data = new SnippetData
            {
                //TODO: Get vote information here instead.
                CurrentUserVoted = true,
                Snippet = snippet,
                Snippet1Votes = 5,
                Snippet2Votes = 8,
                TotalVotes = 13
            };

            return View("View", data);
        }

        public ActionResult Next(int id)
        {
            return ViewSnippet(_snippetService.GetNextSnippet(id));
        }

        public ActionResult Previous(int id)
        {
            return ViewSnippet(_snippetService.GetPreviousSnippet(id));
        }

        private ActionResult ViewSnippet(Snippet snippet)
        {
            TempData.Store(SnippetRedirectKey, snippet);
            return RedirectToAction("View", new { id = snippet.SnippetId });
        }

        [Authorize]
        public ActionResult Create()
        {
            return View(new Snippet());
        }

        [Authorize]
        public ActionResult VoteSnippet1(int snippetId)
        {
            _snippetService.Vote(snippetId, VoteValue.Left, CurrentUser.UserId);
            return RedirectToAction("View", new { id = snippetId });
        }

        [Authorize]
        public ActionResult VoteSnippet2(int snippetId)
        {
            _snippetService.Vote(snippetId, VoteValue.Right, CurrentUser.UserId);

            return RedirectToAction("View", new { id = snippetId });
        }

        [Authorize]
        public ActionResult VoteNeither(int snippetId)
        {
            _snippetService.Vote(snippetId, VoteValue.Neither, CurrentUser.UserId);

            return RedirectToAction("View", new { id = snippetId });
            
        }
    }
}