From e37b3223dbcd7957bbe113c5cb9a5698205fb722 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 20 May 2017 20:06:42 -0400 Subject: [PATCH] create contest page --- .../Controllers/ContestsController.cs | 53 +++++++++++++++++++ Project-Unite/Models/ContestModels.cs | 49 +++++++++++++++++ Project-Unite/Project-Unite.csproj | 1 + .../Views/Contests/CreateContest.cshtml | 38 +++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 Project-Unite/Views/Contests/CreateContest.cshtml diff --git a/Project-Unite/Controllers/ContestsController.cs b/Project-Unite/Controllers/ContestsController.cs index 4b41f91..143f885 100644 --- a/Project-Unite/Controllers/ContestsController.cs +++ b/Project-Unite/Controllers/ContestsController.cs @@ -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 }); + } } } \ No newline at end of file diff --git a/Project-Unite/Models/ContestModels.cs b/Project-Unite/Models/ContestModels.cs index 80533c1..6af7aca 100644 --- a/Project-Unite/Models/ContestModels.cs +++ b/Project-Unite/Models/ContestModels.cs @@ -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 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!")] + 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; } diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index e9c5b83..11608fd 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -969,6 +969,7 @@ + diff --git a/Project-Unite/Views/Contests/CreateContest.cshtml b/Project-Unite/Views/Contests/CreateContest.cshtml new file mode 100644 index 0000000..e134312 --- /dev/null +++ b/Project-Unite/Views/Contests/CreateContest.cshtml @@ -0,0 +1,38 @@ +@model Project_Unite.Models.CreateContestViewModel + +@{ + ViewBag.Title = "Create contest"; +} + +

Create contest

+ +

This page will let you create a ShiftOS community contest.

+ + + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
+
Contest name:
+
@Html.TextBoxFor(Model=>Model.Name, new { @class = "form-control" })
+
Contest description
+
@Html.TextAreaFor(Model => Model.Description, new { @class = "form-control" })
+
Video ID:
+
If you have posted a YouTube video for this contest, paste its ID here. @Html.TextBoxFor(Model => Model.VideoId, new { @class = "form-control" })
+
Gold reward:
+
@Html.EditorFor(Model => Model.GoldReward, new { @class = "form-control" })
+
Silver reward:
+
@Html.EditorFor(Model => Model.SilverReward, new { @class = "form-control" })
+
Bronze reward:
+
@Html.EditorFor(Model => Model.BronzeReward, new { @class = "form-control" })
+
+ +
+ +} \ No newline at end of file