submitentry form

This commit is contained in:
Michael 2017-05-21 07:42:23 -04:00
parent 699912bc5f
commit b6dd3e9ebd
5 changed files with 146 additions and 24 deletions

View file

@ -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);
}

View file

@ -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<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 @@ 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<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; }
}
}

View file

@ -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\" />

View file

@ -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>
}

View file

@ -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>