|
1 | | -using DemoNStack.Extensions; |
2 | | -using DemoNStack.Models; |
3 | | -using DemoNStack.Models.ViewModels; |
4 | | -using Microsoft.AspNetCore.Mvc; |
5 | | -using Microsoft.Extensions.Caching.Memory; |
6 | | -using NStack.SDK.Models; |
7 | | -using NStack.SDK.Services; |
8 | | -using System; |
9 | | -using System.Threading.Tasks; |
| 1 | +namespace DemoNStack.Controllers; |
10 | 2 |
|
11 | | -namespace DemoNStack.Controllers |
| 3 | +public class TermsController : NStackController |
12 | 4 | { |
13 | | - public class TermsController : NStackController |
14 | | - { |
15 | | - private INStackTermsService TermsService { get; } |
16 | | - private IMemoryCache Cache { get; } |
17 | | - |
18 | | - public TermsController(INStackAppService appService, IMemoryCache cache, INStackTermsService termsService) : base(appService) |
19 | | - { |
20 | | - Cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
21 | | - TermsService = termsService ?? throw new ArgumentNullException(nameof(termsService)); |
22 | | - } |
| 5 | + private INStackTermsService TermsService { get; } |
| 6 | + private IMemoryCache Cache { get; } |
23 | 7 |
|
24 | | - public async Task<IActionResult> Index() |
25 | | - { |
26 | | - Guid userId = GetUserId(); |
27 | | - |
28 | | - TermsWithContent newestTerms = await GetNewestTerms(userId, Request.GetCurrentLanguage()); |
| 8 | + public TermsController(INStackAppService appService, IMemoryCache cache, INStackTermsService termsService) : base(appService) |
| 9 | + { |
| 10 | + Cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
| 11 | + TermsService = termsService ?? throw new ArgumentNullException(nameof(termsService)); |
| 12 | + } |
29 | 13 |
|
30 | | - DataMetaWrapper<Translation> translations = await GetTranslations(); |
| 14 | + public async Task<IActionResult> Index() |
| 15 | + { |
| 16 | + Guid userId = GetUserId(); |
31 | 17 |
|
32 | | - var viewModel = new TermsViewModel |
33 | | - { |
34 | | - Content = newestTerms.Content.Data, |
35 | | - HasApproved = newestTerms.HasViewed, |
36 | | - Headline = translations.Data.Terms.Headline, |
37 | | - ApproveButton = translations.Data.Terms.Approve, |
38 | | - HasApprovedString = translations.Data.Terms.HasApproved |
39 | | - }; |
| 18 | + TermsWithContent newestTerms = await GetNewestTerms(userId, Request.GetCurrentLanguage()); |
40 | 19 |
|
41 | | - return View(viewModel); |
42 | | - } |
| 20 | + DataMetaWrapper<Translation> translations = await GetTranslations(); |
43 | 21 |
|
44 | | - [HttpPost] |
45 | | - [ValidateAntiForgeryToken] |
46 | | - public async Task<IActionResult> AcceptTerms() |
| 22 | + var viewModel = new TermsViewModel |
47 | 23 | { |
48 | | - Guid userId = GetUserId(); |
49 | | - string language = Request.GetCurrentLanguage(); |
| 24 | + Content = newestTerms.Content.Data, |
| 25 | + HasApproved = newestTerms.HasViewed, |
| 26 | + Headline = translations.Data.Terms.Headline, |
| 27 | + ApproveButton = translations.Data.Terms.Approve, |
| 28 | + HasApprovedString = translations.Data.Terms.HasApproved |
| 29 | + }; |
| 30 | + |
| 31 | + return View(viewModel); |
| 32 | + } |
50 | 33 |
|
51 | | - TermsWithContent newestTerms = await GetNewestTerms(userId, language); |
| 34 | + [HttpPost] |
| 35 | + [ValidateAntiForgeryToken] |
| 36 | + public async Task<IActionResult> AcceptTerms() |
| 37 | + { |
| 38 | + Guid userId = GetUserId(); |
| 39 | + string language = Request.GetCurrentLanguage(); |
52 | 40 |
|
53 | | - await TermsService.MarkReadAsync(newestTerms.Id, userId.ToString(), language); |
| 41 | + TermsWithContent newestTerms = await GetNewestTerms(userId, language); |
54 | 42 |
|
55 | | - //Clear cache so we get the updated boolean that the user has read the terms |
56 | | - Cache.Remove($"terms-{language}-{userId}"); |
| 43 | + await TermsService.MarkReadAsync(newestTerms.Id, userId.ToString(), language); |
57 | 44 |
|
58 | | - return RedirectToAction("index", new { lang = language }); |
59 | | - } |
| 45 | + //Clear cache so we get the updated boolean that the user has read the terms |
| 46 | + Cache.Remove($"terms-{language}-{userId}"); |
60 | 47 |
|
61 | | - protected async Task<TermsWithContent> GetNewestTerms(Guid userId, string language) |
62 | | - { |
63 | | - return (await Cache.GetOrCreateAsync($"terms-{language}-{userId}", async e => { |
64 | | - e.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60); |
| 48 | + return RedirectToAction("index", new { lang = language }); |
| 49 | + } |
| 50 | + |
| 51 | + protected async Task<TermsWithContent> GetNewestTerms(Guid userId, string language) |
| 52 | + { |
| 53 | + return (await Cache.GetOrCreateAsync($"terms-{language}-{userId}", async e => { |
| 54 | + e.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60); |
65 | 55 |
|
66 | | - return await TermsService.GetNewestTermsAsync("terms", userId.ToString(), language); |
67 | | - })).Data; |
68 | | - } |
| 56 | + return await TermsService.GetNewestTermsAsync("terms", userId.ToString(), language); |
| 57 | + })).Data; |
69 | 58 | } |
70 | 59 | } |
0 commit comments