This repository has been archived by the owner on Oct 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
AdminController.cs
98 lines (79 loc) · 3.03 KB
/
AdminController.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using live.asp.net.Data;
using live.asp.net.Models;
using live.asp.net.Services;
using live.asp.net.ViewModels;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Framework.Caching.Memory;
namespace live.asp.net.Controllers
{
[Route("/admin")]
public class AdminController : Controller
{
private static readonly TimeSpan _pstOffset = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time").BaseUtcOffset;
private readonly AppDbContext _db;
private readonly IMemoryCache _memoryCache;
public AdminController(AppDbContext dbContext, IMemoryCache memoryCache)
{
_db = dbContext;
_memoryCache = memoryCache;
}
[HttpGet()]
[Authorize("Admin")]
public async Task<IActionResult> Index(bool? useDesignData)
{
var model = new AdminViewModel();
var msg = Context.Request.Cookies["msg"];
Context.Response.Cookies.Delete("msg");
switch (msg)
{
case "1":
model.SuccessMessage = "Live show details saved successfully!";
break;
case "2":
model.SuccessMessage = "YouTube cache cleared successfully!";
break;
}
var liveShowDetails = await _db.LiveShowDetails.FirstOrDefaultAsync();
model.LiveShowEmbedUrl = liveShowDetails?.LiveShowEmbedUrl;
model.NextShowDate = liveShowDetails?.NextShowDate;
return View(model);
}
[HttpPost()]
[Authorize("Admin")]
public async Task<IActionResult> Save(AdminViewModel model)
{
if (ModelState.IsValid)
{
var liveShowDetails = await _db.LiveShowDetails.FirstOrDefaultAsync();
if (liveShowDetails == null)
{
liveShowDetails = new LiveShowDetails();
_db.LiveShowDetails.Add(liveShowDetails);
}
if (!string.IsNullOrEmpty(model.LiveShowEmbedUrl) && model.LiveShowEmbedUrl.StartsWith("http://"))
{
model.LiveShowEmbedUrl = "https://" + model.LiveShowEmbedUrl.Substring("http://".Length);
}
liveShowDetails.LiveShowEmbedUrl = model.LiveShowEmbedUrl;
liveShowDetails.NextShowDate = model.NextShowDate;
await _db.SaveChangesAsync();
Context.Response.Cookies.Append("msg", "1");
return RedirectToAction("Index");
}
return View("Index", model);
}
[HttpPost("clearcache")]
public IActionResult ClearCache()
{
_memoryCache.Remove(YouTubeShowsService.CacheKey);
Context.Response.Cookies.Append("msg", "2");
return RedirectToAction("Index");
}
}
}