summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/ContestsController.cs
blob: a99b3428b6e508740f92732a11454b64def824da (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.Collections.Generic;
using System.IO;
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 ContestsController : Controller
    {
        [AllowAnonymous]
        // GET: Contests
        public ActionResult Index()
        {
            var model = new ApplicationDbContext().Contests.ToArray();
            return View(model);
        }

        public ActionResult ViewContest(string id)
        {
            var db = new ApplicationDbContext();
            var c = db.Contests.FirstOrDefault(x => x.Id == id);
            if (c == null)
                return new HttpStatusCodeResult(404);
            return View(c);
        }

        public ActionResult ViewSubmission(string id)
        {
            var db = new ApplicationDbContext();
            var e = db.ContestEntries.FirstOrDefault(x => x.Id == id);
            if (e == null)
                return new HttpStatusCodeResult(404);
            return View(e);
        }

        [RequiresAdmin]
        public ActionResult CloseContest(string id)
        {
            var db = new ApplicationDbContext();
            var c = db.Contests.FirstOrDefault(x => x.Id == id);
            if (c == null)
                return new HttpStatusCodeResult(404);
            c.EndsAt = DateTime.Now;
            db.SaveChanges();
            return RedirectToAction("Index");

        }

        public ActionResult SubmitEntry(string id)
        {
            var db = new ApplicationDbContext();
            var contest = db.Contests.FirstOrDefault(x => x.Id == id);
            if (contest == null)
                return new HttpStatusCodeResult(404);
            if (contest.UserSubmitted(User.Identity.GetUserId()))
                return new HttpStatusCodeResult(403);

            var model = new SubmitContestEntryViewModel();
            model.ContestId = contest.Id;
            return View(model);
        }

        public ActionResult Upvote(string id)
        {
            string uid = User.Identity.GetUserId();
            var db = new ApplicationDbContext();
            var s = db.ContestEntries.FirstOrDefault(x => x.Id == id);
            if (s == null)
                return new HttpStatusCodeResult(404);
            var like = db.Likes.FirstOrDefault(x => x.User == uid && x.Topic == id);
            if (like != null)
            {
                if (like.IsDislike == false)
                    return new HttpStatusCodeResult(403);
                else
                    like.IsDislike = false;
            }
            else
            {
                like = new Like();
                like.Id = Guid.NewGuid().ToString();
                like.IsDislike = false;
                like.User = uid;
                like.Topic = id;
            }
            db.SaveChanges();
            return RedirectToAction("ViewSubmission", new { id = id });

        }

        public ActionResult Downvote(string id)
        {
            string uid = User.Identity.GetUserId();
            var db = new ApplicationDbContext();
            var s = db.ContestEntries.FirstOrDefault(x => x.Id == id);
            if (s == null)
                return new HttpStatusCodeResult(404);
            var like = db.Likes.FirstOrDefault(x => x.User == uid && x.Topic == id);
            if (like != null)
            {
                if (like.IsDislike == true)
                    return new HttpStatusCodeResult(403);
                else
                    like.IsDislike = true;
            }
            else
            {
                like = new Like();
                like.Id = Guid.NewGuid().ToString();
                like.IsDislike = true;
                like.User = uid;
                like.Topic = id;
            }
            db.SaveChanges();
            return RedirectToAction("ViewSubmission", new { id = id });

        }

        [RequiresAdmin]
        public ActionResult Disqualify(string id)
        {
            var db = new ApplicationDbContext();
            var c = db.ContestEntries.FirstOrDefault(x => x.Id == id);
            if (c == null)
                return new HttpStatusCodeResult(404);
            var model = new DisqualifySubmissionViewModel();
            model.Entry = id;
            return View(model);
        }

        [RequiresAdmin]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Disqualify(DisqualifySubmissionViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var db = new ApplicationDbContext();
            var e = db.ContestEntries.FirstOrDefault(x => x.Id == model.Entry);
            if (e == null)
                return new HttpStatusCodeResult(404);
            e.Disqualified = true;
            e.DisqualifiedBy = User.Identity.GetUserId();
            e.DisqualifiedReason = model.Reason;
            db.SaveChanges();

            NotificationDaemon.NotifyUser(User.Identity.GetUserId(), e.AuthorId, "Submission disqualified.", $@"We have disqualified your contest submission ""{e.Name}"".

<strong>Reason:</strong> {e.DisqualifiedReason}");

            return RedirectToAction("ViewSubmission", new { id = model.Entry });
        }

        public ActionResult RemoveVote(string id)
        {
            string uid = User.Identity.GetUserId();
            var db = new ApplicationDbContext();
            var s = db.ContestEntries.FirstOrDefault(x => x.Id == id);
            if (s == null)
                return new HttpStatusCodeResult(404);
            var like = db.Likes.FirstOrDefault(x => x.User == uid && x.Topic == id);
            if (like != null)
            {
                db.Likes.Remove(like);
            }
            else
            {
                return new HttpStatusCodeResult(403);
            }
            db.SaveChanges();
            return RedirectToAction("ViewSubmission", new { id = id });

        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitEntry(SubmitContestEntryViewModel model)
        {
            if(model.Download != null)
            {
                if (!model.Download.FileName.ToLower().EndsWith(".zip"))
                    ModelState.AddModelError("Download", new Exception("The uploaded file is not a .zip file."));
            }

            if (!ModelState.IsValid)
                return View(model);

            var db = new ApplicationDbContext();
            var entry = new ContestEntry();
            entry.Name = model.Name;
            entry.Description = model.Description;
            entry.PostedAt = DateTime.Now;
            entry.Disqualified = false;
            entry.AuthorId = User.Identity.GetUserId();
            entry.ContestId = model.ContestId;
            entry.VideoId = model.VideoID;
            string allowed = "abcdefghijklmnopqrstuvwxyz1234567890_";
            entry.Id = entry.Name.ToLower();
            foreach (var ch in entry.Id.ToCharArray())
                if (!allowed.Contains(ch))
                    entry.Id = entry.Id.Replace(ch, '_');
            if (model.Download != null)
            {
                string fname = model.Download.FileName.ToLower().Replace(".zip", "");
                foreach (var ch in fname.ToCharArray())
                    if (!allowed.Contains(ch))
                        fname = fname.Replace(ch, '_');
                fname += ".zip";
                string serverpath = "~/Uploads/" + ACL.UserNameRaw(User.Identity.GetUserId()) + "/ContestSubmissions/" + model.ContestId;
                string mapped = Server.MapPath(serverpath);
                string mappedwithfilename = Path.Combine(mapped, fname);
                string dbpath = serverpath.Remove(0, 1) + "/" + fname;
                if (!Directory.Exists(mapped))
                    Directory.CreateDirectory(mapped);
                model.Download.SaveAs(mappedwithfilename);
                entry.DownloadURL = dbpath;
            }

            db.ContestEntries.Add(entry);
            db.SaveChanges();

            foreach(var user in db.Users.ToArray())
            {
                if (user.HighestRole.IsAdmin)
                {
                    NotificationDaemon.NotifyUser(entry.AuthorId, user.Id, "New contest entry!", "A new entry has been submitted to the \"" + db.Contests.FirstOrDefault(x=>x.Id==entry.ContestId).Name + "\" contest.", Url.Action("ViewSubmission", "Contests", new { id = entry.Id }));
                }
            }

            return RedirectToAction("ViewSubmission", "Contests", new { id = entry.Id });
        }

        [RequiresAdmin]
        public ActionResult CreateContest()
        {
            var model = new CreateContestViewModel();
            model.EndDate = DateTime.Now;
            return View(model);
        }

        [RequiresAdmin]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateContest(CreateContestViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            var db = new ApplicationDbContext();

            string allowed = "abcdefghijklmnopqrstuvwxyz_0123456789";

            var c = new Contest();

            c.Name = model.Name;
            c.Description = model.Description;
            c.StartedAt = DateTime.Now;
            c.EndsAt = model.EndDate;
            c.VideoId = model.VideoId;
            string id = c.Name.ToLower() + "_" + db.Contests.Count();
            foreach (char ch in id.ToCharArray())
                if (!allowed.Contains(ch))
                    id = id.Replace(ch, '_');
            c.Id = id;
            c.CodepointReward1st = model.GoldReward;
            c.CodepointReward2nd = model.SilverReward;
            c.CodepointReward3rd = model.BronzeReward;
            db.Contests.Add(c);
            db.SaveChanges();

            NotificationDaemon.NotifyEveryone(User.Identity.GetUserId(), "A contest has just started.", "A new contest has been open! Contest: " + c.Name, Url.Action("ViewContest", new { id = c.Id }));

            return RedirectToAction("ViewContest", new { id = c.Id });
        }
    }
}