summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/QuotesController.cs
blob: cce8f2460c8fd8d567de0109ebbebe510b2b9c5a (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
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
{
    [Authorize]
    public class QuotesController : Controller
    {
        // GET: Quotes
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(Models.Quote model)
        {
            if (!ModelState.IsValid)
                return View(model);

            var db = new Models.ApplicationDbContext();
            model.Id = (db.Quotes.Count() + 1).ToString();
            model.IsApproved = false;
            db.Quotes.Add(model);
            db.SaveChanges();

            var users = db.Users.ToArray();
            foreach (var user in users)
            {
                try
                {
                    if (user.HighestRole.IsAdmin)
                    {
                        NotificationDaemon.NotifyUser(User.Identity.GetUserId(), user.Id, "New quote submitted.", "Please review user-submitted quotes.", Url.Action("ReviewAll"));
                    }
                }
                catch { }
            }
            return View(model);
        }

        [RequiresModerator]
        public ActionResult ReviewAll()
        {
            var db = new ApplicationDbContext();
            return View(db.Quotes.Where(x => x.IsApproved == false));
        }

        [RequiresModerator]
        public ActionResult Deny(string id)
        {
            var db = new ApplicationDbContext();
            var quote = db.Quotes.FirstOrDefault(x => x.Id == id);
            if (quote == null)
                return new HttpStatusCodeResult(404);
            if (quote.IsApproved == true)
                return new HttpStatusCodeResult(403);
            db.Quotes.Remove(quote);
            db.SaveChanges();
            return RedirectToAction("ReviewAll");

        }

        [RequiresModerator]
        public ActionResult Approve(string id)
        {
            var db = new ApplicationDbContext();
            var quote = db.Quotes.FirstOrDefault(x => x.Id == id);
            if (quote == null)
                return new HttpStatusCodeResult(404);
            quote.IsApproved = true;
            db.SaveChanges();
            return RedirectToAction("ReviewAll");
        }
    }
}