summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/APIController.cs
blob: e8512ffe87ecea0787392ef22e744d380b37a009 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Microsoft.AspNet.Identity;
using Project_Unite.Models;

namespace Project_Unite.Controllers
{
    public class APIController : Controller
    {
        public ActionResult GetCodepoints()
        {
            try
            {
                string token = Request.Headers["Authentication"].Remove(0, 6);
                var user = ACL.GetUserFromToken(token);
                if (user == null)
                    return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
                return Content(user.Codepoints.ToString());
            }
            catch
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        }

        public ActionResult SetCodepoints(long id)
        {
            try
            {
                string token = Request.Headers["Authentication"].Remove(0, 6);

                var db = new ApplicationDbContext();
                var t = db.OAuthTokens.FirstOrDefault(x => x.Id == token);
                var user = db.Users.FirstOrDefault(x => x.Id == t.UserId);
                user.Codepoints = id;
                db.SaveChanges();
                return new HttpStatusCodeResult(200);
            }
            catch
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        }


        public JavaScriptSerializer Serializer
        {
            get; set;
        }
        //I'll put those curly braces on their own line just to piss a certain smiley off.

        public APIController()
        {
            Serializer = new JavaScriptSerializer();
        }

        // GET: API
        public ActionResult Index()
        {
            return Content("Please use a valid API endpoint.");
        }

        public ActionResult Releases(bool showUnstable = false, bool showObsolete = false)
        {
            var db = new ApplicationDbContext();

            var releases = db.Downloads.ToArray();
            if (!showUnstable)
                releases = releases.Where(x => x.IsStable == true).ToArray();
            if (!showObsolete)
                releases = releases.Where(x => x.Obsolete == false).ToArray();

            return Content(Serializer.Serialize(releases));
        }

        public ActionResult Skins()
        {
            var db = new ApplicationDbContext();
            return Content(Serializer.Serialize(db.Skins.ToArray()));
        }

        public ActionResult TestNotification()
        {
            NotificationDaemon.NotifyEveryone(User.Identity.GetUserId(), "Test notification", "This is a test of the real-time notification system.", "#");
            return Content("Sent.");
        }

        [Authorize]
        public ActionResult GetNotificationCount()
        {
            return Content(ACL.NotificationCountRaw(User.Identity.Name).ToString());
        }
    }
}