create contest page

This commit is contained in:
Michael 2017-05-20 20:06:42 -04:00
parent b0c200196e
commit e37b3223db
4 changed files with 141 additions and 0 deletions

View file

@ -26,5 +26,58 @@ public ActionResult ViewContest(string id)
return new HttpStatusCodeResult(404);
return View(c);
}
[RequiresAdmin]
public ActionResult CloseContest(string id)
{
var db = new ApplicationDbContext();
var c = db.Contests.FirstOrDefault(x => x.Id == id);
if (c == null)
return new HttpStatusCodeResult(404);
c.EndsAt = DateTime.Now;
db.SaveChanges();
return RedirectToAction("Index");
}
[RequiresAdmin]
public ActionResult CreateContest()
{
var model = new CreateContestViewModel();
return View(model);
}
[RequiresAdmin]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateContest(CreateContestViewModel model)
{
if (!ModelState.IsValid)
return View(model);
var db = new ApplicationDbContext();
string allowed = "abcdefghijklmnopqrstuvwxyz_0123456789";
var c = new Contest();
c.Name = model.Name;
c.Description = model.Description;
c.StartedAt = DateTime.Now;
c.EndsAt = model.EndDate;
c.VideoId = model.VideoId;
string id = c.Name.ToLower() + "_" + db.Contests.Count();
foreach (char ch in id.ToCharArray())
if (!allowed.Contains(ch))
id = id.Replace(ch, '_');
c.Id = id;
c.CodepointReward1st = model.GoldReward;
c.CodepointReward2nd = model.SilverReward;
c.CodepointReward3rd = model.BronzeReward;
db.Contests.Add(c);
db.SaveChanges();
return RedirectToAction("ViewContest", new { id = c.Id });
}
}
}

View file

@ -2,9 +2,58 @@
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Project_Unite.Models
{
public class CreateContestViewModel
{
[Required(AllowEmptyStrings =false, ErrorMessage ="You must name your contest!")]
[MinLength(5, ErrorMessage ="Your contest name must contain at least 5 characters.")]
[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!")]
public string Description { get; set; }
[Required(ErrorMessage ="Please set an end date for the contest.")]
public DateTime EndDate { get; set; }
[Required(ErrorMessage ="Please specify a Codepoint reward for the gold winner.")]
public long GoldReward { get; set; }
[Required(ErrorMessage = "Please specify a Codepoint reward for the silver winner.")]
public long SilverReward { get; set; }
[Required(ErrorMessage = "Please specify a Codepoint reward for the bronze winner.")]
public long BronzeReward { get; set; }
public string VideoId { get; set; }
}
public class Contest
{
public string Id { get; set; }

View file

@ -969,6 +969,7 @@
<Content Include="Views\Home\SendFeedback.cshtml" />
<Content Include="Views\Contests\Index.cshtml" />
<Content Include="Views\Contests\ViewContest.cshtml" />
<Content Include="Views\Contests\CreateContest.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

View file

@ -0,0 +1,38 @@
@model Project_Unite.Models.CreateContestViewModel
@{
ViewBag.Title = "Create contest";
}
<h2>Create contest</h2>
<p>This page will let you create a ShiftOS community contest.</p>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"
integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E="
crossorigin="anonymous"></script>
<script>
$('#enddate').datepicker();
</script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<dl>
<dt>Contest name:</dt>
<dd>@Html.TextBoxFor(Model=>Model.Name, new { @class = "form-control" })</dd>
<dt>Contest description</dt>
<dd>@Html.TextAreaFor(Model => Model.Description, new { @class = "form-control" })</dd>
<dt>Video ID:</dt>
<dd>If you have posted a YouTube video for this contest, paste its ID here. @Html.TextBoxFor(Model => Model.VideoId, new { @class = "form-control" })</dd>
<dt>Gold reward:</dt>
<dd>@Html.EditorFor(Model => Model.GoldReward, new { @class = "form-control" })</dd>
<dt>Silver reward:</dt>
<dd>@Html.EditorFor(Model => Model.SilverReward, new { @class = "form-control" })</dd>
<dt>Bronze reward:</dt>
<dd>@Html.EditorFor(Model => Model.BronzeReward, new { @class = "form-control" })</dd>
</dl>
<hr/>
<input type="submit" class="btn btn-primary" value="Open the contest!"/>
}