summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/GroupsController.cs
blob: 264b0e6ccca4666ffb5b7b0fab7b4f31b72d9a29 (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
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 JoinGroup(string id)
        {
            var db = new ApplicationDbContext();
            var user = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId());
            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();
            var user = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId());
            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);
        }
    }
}