NoticeController.cs 1.56 KB
Newer Older
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
using Coscine.Api.Notices.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Coscine.Api.Notices.Controllers
{
    /// <summary>
    /// This controller represents the actions which can be taken with notices.
    /// </summary>
    public class NoticeController : Controller
    {
        private readonly INoticeService _noticeService;

        /// <summary>
        /// NoticeController constructor
        /// </summary>
        /// <param name="noticeService">Given NoticeService implementation</param>
        public NoticeController(INoticeService noticeService)
        {
            _noticeService = noticeService;
        }

        /// <summary>
        /// Returns a notice
        /// </summary>
        /// <param name="documentSlug">Slug defining a configured notice</param>
        /// <param name="language">Language (e.g. "en" or "de")</param>
        /// <returns>Notice</returns>
        [HttpGet("[controller]/{documentSlug}")]
        [ResponseCache(Duration = 60 * 60, VaryByQueryKeys = new[] { "documentSlug", "language" })]
        public async Task<IActionResult> GetNotice(string documentSlug, [FromQuery] string language = "en")
        {
            var url = await Program.Configuration.GetStringAsync($"coscine/local/documents/{documentSlug}/{language}", null);
            if (url == null)
            {
                return BadRequest();
            }
            return Json(new {
                data = new { 
                    body = await _noticeService.GetNotice(url) 
                }
            });
        }

    }
}