From b6dd3e9ebd3be5bbb213461b4fe1ac6fe53cd1ba Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 May 2017 07:42:23 -0400 Subject: [PATCH] submitentry form --- .../Controllers/ContestsController.cs | 66 +++++++++++++++++++ Project-Unite/Models/ContestModels.cs | 59 +++++++++++------ Project-Unite/Project-Unite.csproj | 1 + .../Views/Contests/SubmitEntry.cshtml | 27 ++++++++ .../Views/Contests/ViewContest.cshtml | 17 +++-- 5 files changed, 146 insertions(+), 24 deletions(-) create mode 100644 Project-Unite/Views/Contests/SubmitEntry.cshtml 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 @@ public ActionResult CloseContest(string id) } + 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 @@ public class CreateContestViewModel [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 Contests - { - get - { - var db = new ApplicationDbContext(); - var list = new List(); - 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 @@ public class Contest 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 @@ public Like[] Upvotes } } } + + public class SubmitContestEntryViewModel + { + public string ContestId { get; set; } + + public List Contests + { + get + { + var db = new ApplicationDbContext(); + var list = new List(); + 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 @@ + 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"; +} + +

Submit contest entry

+ +

So you want to enter into a community contest? Alrighty, just fill out this form and we'll enter you in.

+ +@using (Html.BeginForm()) +{ + @Html.ValidationSummary() + @Html.AntiForgeryToken() + +
+
Submission name:
+
@Html.TextBoxFor(Model=>Model.Name, new { @class = "form-control" })
+
Description:
+
@Html.TextAreaFor(Model => Model.Description, new { @class = "form-control" })
+
What contest is this for?
+
@Html.DropDownListFor(Model => Model.ContestId, Model.Contests, new { @class = "form-control" })
+
YouTube video ID:
+
If you have a YouTube video for this submission, paste its ID here! @Html.TextBoxFor(Model => Model.VideoID, new { @class = "form-control" })
+
Attachment:
+
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" })
+
+} \ 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 @@

Want to win this contest?

- - @if (Model.IsEnded) + @if (!Model.UserSubmitted(User.Identity.GetUserId())) { + if (Model.IsEnded) + {

Unfortunately, this contest has ended and you cannot submit an entry. Perhaps there's another contest still going?

+ } + else + { +

Good news! This contest is still open. Hurry and submit your entry!

+ + Go, go, go! Submit an entry! + } } else { -

Good news! This contest is still open. Hurry and submit your entry!

- - Go, go, go! Submit an entry! +

You have already submitted an entry for this contest.

}

Current winners: