NoticeController.cs 3.37 KB
Newer Older
1
using Coscine.Api.Notices.ReturnObjects;
2
using Coscine.Api.Notices.Services;
3
using Coscine.Api.Notices.Utils;
4
using Microsoft.AspNetCore.Mvc;
5
6
using System;
using System.Collections.Generic;
7
8
9
10
11
12
13
14
15
16
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;
17
18
19
        private readonly MaintenanceHelper _maintenanceHelper;
        // url to rss feed from consul variable
        private readonly string _rssUrl;
20
21
22
23
24
25
26
27

        /// <summary>
        /// NoticeController constructor
        /// </summary>
        /// <param name="noticeService">Given NoticeService implementation</param>
        public NoticeController(INoticeService noticeService)
        {
            _noticeService = noticeService;
28
29
30
31
32
33
34
35
            var undefinedString = "-NotDefined-";

            _maintenanceHelper = new MaintenanceHelper
            {
                RelevanceList = new List<string> { "Strung", "Teilstrung", "Unterbrechung", "eingeschrnkt betriebsfhig", "Wartung", "Teilwartung", "nderung", "Warnung", undefinedString, "Hinweis" },
                UndefinedString = undefinedString,
            };
            _rssUrl = Program.Configuration.GetStringAndWait($"coscine/global/rss/url");
36
37
38
39
40
41
42
43
44
45
46
47
48
        }

        /// <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);
49

50
51
52
53
            if (url == null)
            {
                return BadRequest();
            }
54
55
56
57
58

            return Json(new 
            {
                data = new
                { 
59
60
61
62
63
                    body = await _noticeService.GetNotice(url) 
                }
            });
        }

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
        /// <summary>
        /// Returns defined properties of the first entry of the rss feed
        /// </summary>
        /// <returns>Maintenance or 404 if no maintenance was found</returns>
        [HttpGet("[controller]/getMaintenance")]
        public ActionResult<MaintenanceReturnObject> GetMaintenance()
        {
            var maintenance = _maintenanceHelper.GetMaintenance(_rssUrl);

            if (maintenance == null)
            {
                return NotFound("No maintenance was found.");
            }

            var maintenanceReturnObject = new MaintenanceReturnObject
            {
                Body = maintenance.Body,
                DisplayName = maintenance.DisplayName,
                EndsDate = maintenance.EndsDate != DateTime.MaxValue ? maintenance.EndsDate : null,
                StartsDate = maintenance.StartsDate != DateTime.MinValue ? maintenance.StartsDate : null,
                Type = maintenance.Type,
                Url = maintenance.Url,
            };

            return Ok(maintenanceReturnObject);
        }

91
92
    }
}