Commits

OJ Reeves  committed 8a32a5a

Did a bit of work with the views and models to tidy things up a little. Added the SnippetCreateData and started work on getting the create functionality working.

  • Participants
  • Parent commits 29891c8

Comments (0)

Files changed (12)

File CodeSmackdown.DB/CodeSmackdown.sql

   `SnippetId` int(10) NOT NULL AUTO_INCREMENT,
   `UserId` int(10) NOT NULL,
   `Title` varchar(255) NOT NULL,
-  `Snippet1` varchar(8000) NOT NULL,
-  `Snippet2` varchar(8000) NOT NULL,
+  `Left` varchar(8000) NOT NULL,
+  `Right` varchar(8000) NOT NULL,
   `Created` datetime NOT NULL,
   PRIMARY KEY (`SnippetId`),
   KEY `UserId` (`UserId`),

File CodeSmackdown.Domain/Maps/SnippetMap.cs

         {
             Id(s => s.SnippetId)
                 .Not.Nullable()
-                .Column("snippetid");
+                .Column("SnippetId");
             Map(s => s.UserId)
                 .Not.Nullable()
-                .Column("userid");
+                .Column("UserId");
             Map(s => s.Title)
                 .Length(255)
                 .Not.Nullable()
-                .Column("title");
-            Map(s => s.Snippet1)
+                .Column("Title");
+            Map(s => s.Left)
                 .Length(8000)
                 .Not.Nullable()
-                .Column("snippet1");
-            Map(s => s.Snippet2)
+                .Column("Left");
+            Map(s => s.Right)
                 .Length(8000)
                 .Not.Nullable()
-                .Column("snippet2");
+                .Column("Right");
             Map(s => s.Created)
-                .Column("created");
+                .Column("Created");
         }
     }
 }

File CodeSmackdown.Domain/Models/Snippet.cs

         public virtual int SnippetId { get; set; }
         public virtual int UserId { get; set; }
         public virtual string Title { get; set; }
-        public virtual string Snippet1 { get; set; }
-        public virtual string Snippet2 { get; set; }
+        public virtual string Left { get; set; }
+        public virtual string Right { get; set; }
         public virtual DateTime Created { get; set; }
     }
 }

File CodeSmackdown.Site/CodeSmackdown.Site.csproj

     <Compile Include="Global.asax.cs">
       <DependentUpon>Global.asax</DependentUpon>
     </Compile>
-    <Compile Include="Models\SnippetData.cs" />
+    <Compile Include="Models\SnippetCreateData.cs" />
+    <Compile Include="Models\SnippetViewData.cs" />
     <Compile Include="Models\UserData.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
     <Content Include="Scripts\MicrosoftMvcValidation.js" />
     <Content Include="Scripts\MicrosoftMvcValidation.debug.js" />
     <None Include="Views\Layouts\Application.spark" />
-    <None Include="Views\Shared\_Snippet.spark" />
-    <None Include="Views\Snippets\Create.spark" />
+    <None Include="Views\Shared\_Snippet.spark">
+      <SubType>Designer</SubType>
+    </None>
+    <None Include="Views\Snippets\Create.spark">
+      <SubType>Designer</SubType>
+    </None>
     <None Include="Views\Snippets\View.spark" />
   </ItemGroup>
   <ItemGroup />

File CodeSmackdown.Site/Controllers/SnippetsController.cs

-using System.Web.Mvc;
+using System.Collections.Generic;
+using System.Web.Mvc;
 using CodeSmackdown.Domain.Models;
 using CodeSmackdown.Domain.Services;
 using CodeSmackdown.Site.Extensions;
             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)
         {
                 snippet = _snippetService.GetSnippet(id);
             }
 
-            var data = new SnippetData
+            var data = new SnippetViewData
             {
-                CurrentUserVoted = false,
+                CurrentUserVote = null,
                 Snippet = snippet,
-                Snippet1Votes = 5,
-                Snippet2Votes = 8,
-                TotalVotes = 13
+                Votes = new Dictionary<VoteValue, int>
+                {
+                    { VoteValue.Left, 5 },
+                    { VoteValue.Right, 7 },
+                    { VoteValue.Neither, 3 },
+                }
             };
 
             return View("View", data);
         [Authorize]
         public ActionResult Create()
         {
-            return View(new Snippet());
+            return View(new SnippetCreateData());
         }
 
         [Authorize]
-        public ActionResult VoteSnippet1(int snippetId)
+        public ActionResult Create(SnippetCreateData createData)
+        {
+            return RedirectToRoute("Index");
+        }
+
+        [Authorize]
+        public ActionResult VoteLeft(int snippetId)
         {
             _snippetService.Vote(snippetId, VoteValue.Left, CurrentUser.UserId);
             return RedirectToAction("Results", new { id = snippetId });
         }
 
         [Authorize]
-        public ActionResult VoteSnippet2(int snippetId)
+        public ActionResult VoteRight(int snippetId)
         {
             _snippetService.Vote(snippetId, VoteValue.Right, CurrentUser.UserId);
 

File CodeSmackdown.Site/Models/SnippetCreateData.cs

+using System.ComponentModel.DataAnnotations;
+
+namespace CodeSmackdown.Site.Models
+{
+    public class SnippetCreateData
+    {
+        [Required]
+        public string Title { get; set; }
+        
+        [Required]
+        public string Left { get; set; }
+        
+        [Required]
+        public string Right { get; set; }
+
+        public string Tags { get; set; }
+    }
+}

File CodeSmackdown.Site/Models/SnippetData.cs

-using CodeSmackdown.Domain.Models;
-
-namespace CodeSmackdown.Site.Models
-{
-    public class SnippetData
-    {
-        public Snippet Snippet { get; set; }
-        public int TotalVotes { get; set; }
-        public int Snippet1Votes { get; set; }
-        public int Snippet2Votes { get; set; }
-        public bool CurrentUserVoted { get; set; }
-        public bool? CurrentUserVotedSnippet1 { get; set; }
-    }
-}

File CodeSmackdown.Site/Models/SnippetViewData.cs

+using System.Collections.Generic;
+using System.Linq;
+using CodeSmackdown.Domain.Models;
+using CodeSmackdown.Domain.Services;
+
+namespace CodeSmackdown.Site.Models
+{
+    public class SnippetViewData
+    {
+        public Snippet Snippet { get; set; }
+        public Dictionary<VoteValue, int> Votes { get; set; }
+        public int TotalVotes { get { return Votes.Sum(k => k.Value); } }
+        public VoteValue? CurrentUserVote { get; set; }
+    }
+}

File CodeSmackdown.Site/Views/Shared/_Snippet.spark

-<div id="submitcontainer">
+<use namespace="CodeSmackdown.Domain.Services"/>
+<div id="submitcontainer">
     <ul class="navmenu">
         <li>!{Html.ActionLink("<< Previous", "Previous", "Snippets", new { id = model.Snippet.SnippetId }, null)}</li>
         <li>!{Html.ActionLink("Random", "Index", "Snippets")}</li>
             Total of <b>!{model.TotalVotes}</b> Vote!{(model.TotalVotes == 1) ? "" : "s"}
         </td>
     </tr>
-    <tr if="model.CurrentUserVoted">
-        <if condition="model.Snippet1Votes > model.Snippet2Votes">
-            <td class="vote winner">!{model.Snippet1Votes} Votes</td>
-            <td class="vote loser" align="right">!{model.Snippet2Votes} Votes</td>
+    <tr if="model.CurrentUserVote.HasValue">
+        <if condition="model.Votes[VoteValue.Left] > model.Votes[VoteValue.Right]">
+            <td class="vote winner">!{model.Votes[VoteValue.Left]} Votes</td>
+            <td class="vote loser" align="right">!{model.Votes[VoteValue.Right]} Votes</td>
         </if>
-        <else if="model.Snippet1Votes < model.Snippet2Votes">
-            <td class="vote loser">!{model.Snippet1Votes} Votes</td>
-            <td class="vote winner" align="right">!{model.Snippet2Votes} Votes</td>
+        <else if="model.Votes[VoteValue.Left] < model.Votes[VoteValue.Right]">
+            <td class="vote loser">!{model.Votes[VoteValue.Left]} Votes</td>
+            <td class="vote winner" align="right">!{model.Votes[VoteValue.Right]} Votes</td>
         </else>
         <else>
-            <td class="vote even">!{model.Snippet1Votes} Votes</td>
-            <td class="vote even" align="right">!{model.Snippet2Votes} Votes</td>
+            <td class="vote even">!{model.Votes[VoteValue.Left]} Votes</td>
+            <td class="vote even" align="right">!{model.Votes[VoteValue.Right]} Votes</td>
         </else>
     </tr>
     <tr>
         <td class="code" width="50%">
             <div>
-                <pre><code>!{H(model.Snippet.Snippet1)}</code></pre>
+                <pre><code>!{H(model.Snippet.Left)}</code></pre>
             </div>
         </td>
         <td class="code" width="50%">
             <div>
-                <pre><code>!{H(model.Snippet.Snippet2)}</code></pre>
+                <pre><code>!{H(model.Snippet.Right)}</code></pre>
             </div>
         </td>
     </tr>
         <td>!{Html.ActionLink("Permalink", "View", "Snippets", new { id = model.Snippet.SnippetId }, null)}</td>
     </tr>
 
-    <tr if="model.CurrentUserVoted">
-        <if condition="model.CurrentUserVotedSnippet1.HasValue">
+    <tr if="model.CurrentUserVote.HasValue">
+        <if condition="model.CurrentUserVote.Value == VoteValue.Neither">
+            <td align="center" colspan="2">^^ You voted for neither of these ^^</td>
+        </if>
+        <else>
             <td>
-                <if condition="model.CurrentUserVotedSnippet1.Value">
+                <if condition="model.CurrentUserVote.Value == VoteValue.Left">
                     ^^ You voted for this snippet
                 </if>
             </td>
             <td>
-                <if condition="!model.CurrentUserVotedSnippet1.Value">
+                <if condition="model.CurrentUserVote.Value == VoteValue.Right">
                     You voted for this snippet ^^
                 </if>
             </td>
-        </if>
-        <else>
-            <td align="center" colspan="2">^^ You voted for neither of these ^^</td>
         </else>
     </tr>
     <tr elseif="Request.IsAuthenticated">
         <td colspan="2">
-        #Html.BeginForm("VoteSnippet1", "Snippets");
+        #Html.BeginForm("VoteLeft", "Snippets");
             <span style="float: left; padding-left: 120px; padding-top: 10px">
                 <input id="voteHot" name="vote" type="submit" value="^^ That is hot! " class="hotbutton" /></span>
             !{Html.Hidden("snippetId", model.Snippet.SnippetId)}
         #Html.EndForm();
-        #Html.BeginForm("VoteSnippet2", "Snippets");
+        #Html.BeginForm("VoteRight", "Snippets");
             <span style="float: right; padding-right: 120px; padding-top: 10px">
                 <input id="voteHot" name="vote" type="submit" value="No, this is hot! ^^" class="hotbutton" /></span>
             !{Html.Hidden("snippetId", model.Snippet.SnippetId)}

File CodeSmackdown.Site/Views/Snippets/Create.spark

-<viewdata model="CodeSmackdown.Domain.Models.Snippet"/>
+<viewdata model="CodeSmackdown.Site.Models.SnippetCreateData"/>
 <content name="MainContent">
-    <pre>
-    Title
-    Snippet1  Snippet2
-    
-    Tags (e.g. F#, MVC, Hadoop)
-    
-    [Save] &lt;Preview&gt;
-    </pre>
-</content>
+    <h2>create snippet</h2>
+    !{Html.ValidationSummary()}
+    #Html.BeginForm("Create", "Snippets");
+    <h3>Enter two snippets to be compared, side by side.</h3>
+    <p>The snippets should be mostly the same but differ in one important way. For example, they may show two different ways of achieving the same result in perl. They may show two functionally equivalent snippets from two different languages.</p>
+
+    <table>
+        <tr>
+            <td>
+                <h2>snippet title</h2>
+                !{Html.ValidationMessageFor(s => s.Title)}
+                !{Html.TextBoxFor(s => s.Title)}
+            </td>
+            <td>
+                <h2>tags</h2>
+                !{Html.ValidationMessageFor(s => s.Tags)}
+                !{Html.TextBoxFor(s => s.Tags)}
+            </td>
+        </tr>
+        <tr>
+            <td>
+                <h2>left</h2>   !{Html.ValidationMessageFor(s => s.Left)}
+                !{Html.TextAreaFor(s => s.Left, new { rows = 10, cols = 50 })}
+            </td>
+            <td>
+                <h2>right</h2>  !{Html.ValidationMessageFor(s => s.Right)}
+                !{Html.TextAreaFor(s => s.Right, new { rows = 10, cols = 50 })}
+            </td>
+        </tr>
+        <tr>
+
+            <td colspan="2">
+                <br />
+                <input type="submit" value="Submit Your Snippets!" class="button"/><br /> ...snippets will be syntax highlighted with <a href="http://code.google.com/p/google-code-prettify/">magic</a>.
+            </td>
+        </tr>
+    </table>
+    #Html.EndForm();
+</content>

File CodeSmackdown.Site/Views/Snippets/View.spark

-<viewdata model="CodeSmackdown.Site.Models.SnippetData"/>
+<viewdata model="CodeSmackdown.Site.Models.SnippetViewData"/>
 <content name="MainContent">
     <Snippet model="ViewData.Model"/>
 </content>

File CodeSmackdown.Tests/Site/Controllers/Snippets/Scenarios/BaseSnippetsControllerScenario.cs

             SampleSnippet = new Snippet
             {
                 Created = DateTime.Now,
-                Snippet1 = "Foo",
-                Snippet2 = "Bar",
+                Left = "Foo",
+                Right = "Bar",
                 SnippetId = 1,
                 Title = "Baz",
                 UserId = 1