summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/GroupsController.cs
blob: 55b23b69e061823799095a4bc38c8ff2f1f707c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Project_Unite.Models;

namespace Project_Unite.Controllers
{
    public class GroupsController : Controller
    {
        // GET: Groups
        public ActionResult Index()
        {
            var db = new ApplicationDbContext();
            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)
        {
            var db = new ApplicationDbContext();
            string UserId = User.Identity.GetUserId();
            var user = db.Users.FirstOrDefault(x => x.Id == UserId);
            var group = db.Groups.FirstOrDefault(x => x.Id == id);
            if (group == null)
                return new HttpStatusCodeResult(404);
            user.GroupId = id;
            db.SaveChanges();
            return RedirectToAction("ViewGroup", "Groups", new { id = id });
        }

        [Authorize]
        public ActionResult LeaveGroup()
        {
            var db = new ApplicationDbContext();
            string UserId = User.Identity.GetUserId();
            var user = db.Users.FirstOrDefault(x => x.Id == UserId);
            var group = db.Groups.FirstOrDefault(x => x.Id == user.GroupId);
            if (group == null)
                return new HttpStatusCodeResult(404);
            user.GroupId = "";
            db.SaveChanges();
            return RedirectToAction("ViewGroup", "Groups", new { id = group.Id });

        }

        [Authorize]
        public ActionResult ViewGroup(string id)
        {
            var db = new ApplicationDbContext();
            var group = db.Groups.FirstOrDefault(x => x.Id == id);
            if (group == null)
                return new HttpStatusCodeResult(404);

            return View(group);
        }
    }
}