summaryrefslogtreecommitdiff
path: root/Project-Unite
diff options
context:
space:
mode:
Diffstat (limited to 'Project-Unite')
-rw-r--r--Project-Unite/Controllers/ContestsController.cs66
-rw-r--r--Project-Unite/Models/ContestModels.cs59
-rw-r--r--Project-Unite/Project-Unite.csproj1
-rw-r--r--Project-Unite/Views/Contests/SubmitEntry.cshtml27
-rw-r--r--Project-Unite/Views/Contests/ViewContest.cshtml17
5 files changed, 146 insertions, 24 deletions
diff --git a/Project-Unite/Controllers/ContestsController.cs b/Project-Unite/Controllers/ContestsController.cs
index 143f885..1d403f2 100644
--- a/Project-Unite/Controllers/ContestsController.cs
+++ b/Project-Unite/Controllers/ContestsController.cs
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
+using Microsoft.AspNet.Identity;
using Project_Unite.Models;
namespace Project_Unite.Controllers
@@ -40,10 +42,74 @@ namespace Project_Unite.Controllers
}
+ public ActionResult SubmitEntry(string id)
+ {
+ var db = new ApplicationDbContext();
+ var contest = db.Contests.FirstOrDefault(x => x.Id == id);
+ if (contest == null)
+ return new HttpStatusCodeResult(404);
+ if (contest.UserSubmitted(User.Identity.GetUserId()))
+ return new HttpStatusCodeResult(403);
+
+ var model = new SubmitContestEntryViewModel();
+ model.ContestId = contest.Id;
+ return View(model);
+ }
+
+ [HttpPost]
+ [ValidateAntiForgeryToken]
+ public ActionResult SubmitEntry(SubmitContestEntryViewModel model)
+ {
+ if(model.Download != null)
+ {
+ if (!model.Download.FileName.ToLower().EndsWith(".zip"))
+ ModelState.AddModelError("Download", new Exception("The uploaded file is not a .zip file."));
+ }
+
+ if (!ModelState.IsValid)
+ return View(model);
+
+ var db = new ApplicationDbContext();
+ var entry = new ContestEntry();
+ entry.Name = model.Name;
+ entry.Description = model.Description;
+ entry.PostedAt = DateTime.Now;
+ entry.Disqualified = false;
+ entry.AuthorId = User.Identity.GetUserId();
+ entry.ContestId = model.ContestId;
+ entry.VideoId = model.VideoID;
+ string allowed = "abcdefghijklmnopqrstuvwxyz1234567890_";
+ entry.Id = entry.Name.ToLower();
+ foreach (var ch in entry.Id.ToCharArray())
+ if (!allowed.Contains(ch))
+ entry.Id = entry.Id.Replace(ch, '_');
+ if (model.Download != null)
+ {
+ string fname = model.Download.FileName.ToLower().Replace(".zip", "");
+ foreach (var ch in fname.ToCharArray())
+ if (!allowed.Contains(ch))
+ fname = fname.Replace(ch, '_');
+ fname += ".zip";
+ string serverpath = "~/Uploads/" + ACL.UserNameRaw(User.Identity.GetUserId()) + "/ContestSubmissions/" + model.ContestId;
+ string mapped = Server.MapPath(serverpath);
+ string mappedwithfilename = Path.Combine(mapped, fname);
+ string dbpath = serverpath.Remove(0, 1) + "/" + fname;
+ if (!Directory.Exists(mapped))
+ Directory.CreateDirectory(mapped);
+ model.Download.SaveAs(mappedwithfilename);
+ entry.DownloadURL = dbpath;
+ }
+
+ db.ContestEntries.Add(entry);
+ db.SaveChanges();
+ return RedirectToAction("ViewSubmission", "Contests", new { id = entry.Id });
+ }
+
[RequiresAdmin]
public ActionResult CreateContest()
{
var model = new CreateContestViewModel();
+ model.EndDate = DateTime.Now;
return View(model);
}
diff --git a/Project-Unite/Models/ContestModels.cs b/Project-Unite/Models/ContestModels.cs
index 9d8203e..42fe8f1 100644
--- a/Project-Unite/Models/ContestModels.cs
+++ b/Project-Unite/Models/ContestModels.cs
@@ -14,25 +14,6 @@ namespace Project_Unite.Models
[MaxLength(35, ErrorMessage ="Your contest's name must not have more than 35 characters!")]
public string Name { get; set; }
- public string ContestId { get; set; }
-
- public List<SelectListItem> Contests
- {
- get
- {
- var db = new ApplicationDbContext();
- var list = new List<SelectListItem>();
- foreach (var c in db.Contests.Where(x => x.IsEnded == false).OrderByDescending(x => x.StartedAt).ToArray())
- {
- list.Add(new SelectListItem
- {
- Value = c.Id,
- Text = c.Name
- });
- }
- return list;
- }
- }
[AllowHtml]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please describe your contest!")]
@@ -66,6 +47,10 @@ namespace Project_Unite.Models
public long CodepointReward2nd { get; set; }
public long CodepointReward3rd { get; set; }
+ public bool UserSubmitted(string uid)
+ {
+ return Entries.FirstOrDefault(x => x.AuthorId == uid) != null;
+ }
public bool IsEnded
{
@@ -118,4 +103,40 @@ namespace Project_Unite.Models
}
}
}
+
+ public class SubmitContestEntryViewModel
+ {
+ public string ContestId { get; set; }
+
+ public List<SelectListItem> Contests
+ {
+ get
+ {
+ var db = new ApplicationDbContext();
+ var list = new List<SelectListItem>();
+ foreach (var c in db.Contests.Where(x => x.IsEnded == false).OrderByDescending(x => x.StartedAt).ToArray())
+ {
+ list.Add(new SelectListItem
+ {
+ Value = c.Id,
+ Text = c.Name
+ });
+ }
+ return list;
+ }
+ }
+
+ [Required(AllowEmptyStrings =false, ErrorMessage ="Please name your submission!")]
+ [MaxLength(55, ErrorMessage ="Your submission's name must have less than 55 characters in it.")]
+ [MinLength(5, ErrorMessage ="Your submission's name must be at least 5 characters long.")]
+ public string Name { get; set; }
+
+ [Required(AllowEmptyStrings =false, ErrorMessage ="Please describe your submission!")]
+ [AllowHtml]
+ public string Description { get; set; }
+
+ public HttpPostedFileBase Download { get; set; }
+
+ public string VideoID { get; set; }
+ }
} \ No newline at end of file
diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj
index bb11f6e..3ecbf96 100644
--- a/Project-Unite/Project-Unite.csproj
+++ b/Project-Unite/Project-Unite.csproj
@@ -972,6 +972,7 @@
<Content Include="Views\Contests\Index.cshtml" />
<Content Include="Views\Contests\ViewContest.cshtml" />
<Content Include="Views\Contests\CreateContest.cshtml" />
+ <Content Include="Views\Contests\SubmitEntry.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
diff --git a/Project-Unite/Views/Contests/SubmitEntry.cshtml b/Project-Unite/Views/Contests/SubmitEntry.cshtml
new file mode 100644
index 0000000..b80b3fb
--- /dev/null
+++ b/Project-Unite/Views/Contests/SubmitEntry.cshtml
@@ -0,0 +1,27 @@
+@model Project_Unite.Models.SubmitContestEntryViewModel
+@{
+ ViewBag.Title = "Submit contest entry";
+}
+
+<h2>Submit contest entry</h2>
+
+<p>So you want to enter into a community contest? Alrighty, just fill out this form and we'll enter you in.</p>
+
+@using (Html.BeginForm())
+{
+ @Html.ValidationSummary()
+ @Html.AntiForgeryToken()
+
+ <dl>
+ <dt>Submission name:</dt>
+ <dd>@Html.TextBoxFor(Model=>Model.Name, new { @class = "form-control" })</dd>
+ <dt>Description:</dt>
+ <dd>@Html.TextAreaFor(Model => Model.Description, new { @class = "form-control" })</dd>
+ <dt>What contest is this for?</dt>
+ <dd>@Html.DropDownListFor(Model => Model.ContestId, Model.Contests, new { @class = "form-control" })</dd>
+ <dt>YouTube video ID:</dt>
+ <dd>If you have a YouTube video for this submission, paste its ID here! @Html.TextBoxFor(Model => Model.VideoID, new { @class = "form-control" })</dd>
+ <dt>Attachment:</dt>
+ <dd>If your submission has a download associated with it, upload it here. Please note: Only .zip files are allowed. @Html.TextBoxFor(Model => Model.Download, new { type="file", @class = "form-control" })</dd>
+ </dl>
+} \ No newline at end of file
diff --git a/Project-Unite/Views/Contests/ViewContest.cshtml b/Project-Unite/Views/Contests/ViewContest.cshtml
index a460840..886d5f4 100644
--- a/Project-Unite/Views/Contests/ViewContest.cshtml
+++ b/Project-Unite/Views/Contests/ViewContest.cshtml
@@ -1,4 +1,5 @@
@model Project_Unite.Models.Contest
+@using Microsoft.AspNet.Identity
@{
ViewBag.Title = "Contest: " + Model.Name;
}
@@ -91,16 +92,22 @@
</div>
<div class="col-xs-3">
<h4>Want to win this contest?</h4>
-
- @if (Model.IsEnded)
+ @if (!Model.UserSubmitted(User.Identity.GetUserId()))
{
+ if (Model.IsEnded)
+ {
<p>Unfortunately, this contest has ended and you cannot submit an entry. Perhaps there's another contest still going?</p>
+ }
+ else
+ {
+ <p>Good news! This contest is still open. Hurry and submit your entry!</p>
+
+ <a href="@Url.Action("SubmitEntry", "Contests", new { id = Model.Id })" class="btn-primary btn"><span class="glyphicon glyphicon-arrow-right"></span> Go, go, go! Submit an entry!</a>
+ }
}
else
{
- <p>Good news! This contest is still open. Hurry and submit your entry!</p>
-
- <a href="@Url.Action("SubmitEntry", "Contests", new {id=Model.Id})" class="btn-primary btn"><span class="glyphicon glyphicon-arrow-right"></span> Go, go, go! Submit an entry!</a>
+ <p>You have already submitted an entry for this contest.</p>
}
<h4>Current winners:</h4>