From ca5954e7d2b9c2040f6f421816778d8203161563 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 May 2017 10:04:12 -0400 Subject: Create group page --- Project-Unite/Controllers/GroupsController.cs | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'Project-Unite/Controllers/GroupsController.cs') 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) { -- cgit v1.2.3