From ca5954e7d2b9c2040f6f421816778d8203161563 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 May 2017 10:04:12 -0400 Subject: [PATCH] Create group page --- Project-Unite/Controllers/GroupsController.cs | 65 ++++++++++++++++++ Project-Unite/Models/Group.cs | 66 ++++++++++++++++++- Project-Unite/Project-Unite.csproj | 1 + Project-Unite/Views/Groups/CreateGroup.cshtml | 50 ++++++++++++++ Project-Unite/Views/Groups/Index.cshtml | 4 +- 5 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 Project-Unite/Views/Groups/CreateGroup.cshtml diff --git a/Project-Unite/Controllers/GroupsController.cs b/Project-Unite/Controllers/GroupsController.cs index 264b0e6..d171d98 100644 --- a/Project-Unite/Controllers/GroupsController.cs +++ b/Project-Unite/Controllers/GroupsController.cs @@ -17,6 +17,71 @@ namespace Project_Unite.Controllers return View(db.Groups); } + [Authorize] + public ActionResult CreateGroup() + { + //NOPE. I'm not circumming to the ways of Victor Tran. CURLY BRACES ON THEIR OWN LINE. + var model = new GroupViewModel(); + return View(model); + } + + private bool ValidateHex(string hex) + { + if (!(hex.Length == 3 || hex.Length == 6)) + return false; + string hexallowed = "0123456789abcdef"; + foreach(var c in hex.ToLower().ToCharArray()) + { + if (!hexallowed.Contains(c)) + return false; + } + return true; + } + + [Authorize] + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult CreateGroup(GroupViewModel model) + { + var result = ValidateHex(model.BannerColorHex); + + if(result == false) + { + ModelState.AddModelError("BannerColorHex", new Exception("Invalid hexadecimal color code.")); + } + + if (!ModelState.IsValid) + return View(model); + + var db = new ApplicationDbContext(); + var group = new Group(); + group.Id = Guid.NewGuid().ToString(); + group.Name = model.Name; + group.ShortName = model.ShortName; + group.Description = model.Description; + switch (model.Publicity) + { + case "public": + group.Publicity = 0; + break; + case "publici": + group.Publicity = 1; + break; + case "private": + group.Publicity = 2; + break; + case "privatei": + group.Publicity = 3; + break; + } + group.RawReputation = 0.00; + group.BannerColorHex = model.BannerColorHex; + + db.Groups.Add(group); + db.SaveChanges(); + return RedirectToAction("JoinGroup", "Groups", new { id = group.Id }); + } + [Authorize] public ActionResult JoinGroup(string id) { diff --git a/Project-Unite/Models/Group.cs b/Project-Unite/Models/Group.cs index da5b277..3ecf2f3 100644 --- a/Project-Unite/Models/Group.cs +++ b/Project-Unite/Models/Group.cs @@ -1,19 +1,79 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; +using System.Web.Mvc; namespace Project_Unite.Models { - public class Group + public class GroupViewModel { - public string Id { get; set; } + public List Publicities + { + get + { + return new List + { + new SelectListItem { Value="public", Text="Public"}, + new SelectListItem { Value="publici", Text="Public (Invite Only)"}, + new SelectListItem { Value="private", Text="Private"}, + new SelectListItem { Value="privatei", Text="Private (Invite Only)" } + }; + } + } + + [Required] + [MaxLength(25, ErrorMessage = "Your group's name must have a maximum of 25 characters in it.")] + [MinLength(5, ErrorMessage = "You must set a name with at least 5 characters in it.")] public string Name { get; set; } - public int Publicity { get; set; } + + [Required] + public string Publicity { get; set; } + + [Required] + [MaxLength(6, ErrorMessage = "Hexadecimal color values can only have 6 or less digits.")] + [MinLength(3, ErrorMessage = "Hexadecimal color values must have at least 3 digits.")] public string BannerColorHex { get; set; } + + [Required] + [AllowHtml] public string Description { get; set; } + + [Required] + [MaxLength(4, ErrorMessage = "Your Short Name can only have 4 characters. Think of it like an acronym.")] public string ShortName { get; set; } + } + + + public class Group + { + [Required] + public string Id { get; set; } + + [Required] + [MaxLength(25, ErrorMessage ="Your group's name must have a maximum of 25 characters in it.")] + [MinLength(5, ErrorMessage ="You must set a name with at least 5 characters in it.")] + public string Name { get; set; } + + [Required] + public int Publicity { get; set; } + + [Required] + [MaxLength(6, ErrorMessage ="Hexadecimal color values can only have 6 or less digits.")] + [MinLength(3, ErrorMessage ="Hexadecimal color values must have at least 3 digits.")] + public string BannerColorHex { get; set; } + + [Required] + [AllowHtml] + public string Description { get; set; } + + [Required] + [MaxLength(4, ErrorMessage ="Your Short Name can only have 4 characters. Think of it like an acronym.")] + public string ShortName { get; set; } + + [Required] public double RawReputation { get; set; } public ApplicationUser[] Users diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index 24f4579..11604fa 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -591,6 +591,7 @@ + diff --git a/Project-Unite/Views/Groups/CreateGroup.cshtml b/Project-Unite/Views/Groups/CreateGroup.cshtml new file mode 100644 index 0000000..3b26f86 --- /dev/null +++ b/Project-Unite/Views/Groups/CreateGroup.cshtml @@ -0,0 +1,50 @@ +@model Project_Unite.Models.GroupViewModel +@{ + ViewBag.Title = "Create a group"; +} + +

Create a group

+ +

This page allows you to create a group. Note that when you create the group, you will leave your current group if you are in one.

+ +@using (Html.BeginForm()) +{ +
+
+ @Html.ValidationSummary() +
+
+ + + @Html.AntiForgeryToken() + + + + + + + + + + + + + + + + + + + + + + + + +
+ Group name: + @Html.TextBoxFor(Model=>Model.Name, new{@class="form-control"})
Short name: +

Your "Short Name" is simply a tag for your group. Think of a neat acronym to go with your group. Something clever that can fit in 4 characters or less.

+
@Html.TextBoxFor(Model=>Model.ShortName, new { @class = "form-control" })
Group description:@Html.TextAreaFor(Model=>Model.Description)
Banner color (hexadecimal):@Html.TextBoxFor(Model=>Model.BannerColorHex, new {@class="form-control"})
Publicity:@Html.DropDownListFor(Model=>Model.Publicity, Model.Publicities, new { @class = "form-control" }) +
+} \ No newline at end of file diff --git a/Project-Unite/Views/Groups/Index.cshtml b/Project-Unite/Views/Groups/Index.cshtml index 7364df0..f0f535f 100644 --- a/Project-Unite/Views/Groups/Index.cshtml +++ b/Project-Unite/Views/Groups/Index.cshtml @@ -10,7 +10,9 @@

You can join one of the in-game groups from your Digital Society Control Centre, or you can join one of the many user-created groups here.

- +