using Coscine.Api.Notices.ReturnObjects; using Coscine.Api.Notices.Services; using Coscine.Api.Notices.Utils; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Coscine.Api.Notices.Controllers { /// /// This controller represents the actions which can be taken with notices. /// public class NoticeController : Controller { private readonly INoticeService _noticeService; private readonly MaintenanceHelper _maintenanceHelper; // url to rss feed from consul variable private readonly string _rssUrl; /// /// NoticeController constructor /// /// Given NoticeService implementation public NoticeController(INoticeService noticeService) { _noticeService = noticeService; var undefinedString = "-NotDefined-"; _maintenanceHelper = new MaintenanceHelper { RelevanceList = new List { "Störung", "Teilstörung", "Unterbrechung", "eingeschränkt betriebsfähig", "Wartung", "Teilwartung", "Änderung", "Warnung", undefinedString, "Hinweis" }, UndefinedString = undefinedString, }; _rssUrl = Program.Configuration.GetStringAndWait($"coscine/global/rss/url"); } /// /// Returns a notice /// /// Slug defining a configured notice /// Language (e.g. "en" or "de") /// Notice [HttpGet("[controller]/{documentSlug}")] [ResponseCache(Duration = 60 * 60, VaryByQueryKeys = new[] { "documentSlug", "language" })] public async Task 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) } }); } /// /// Returns defined properties of the first entry of the rss feed /// /// Maintenance or 404 if no maintenance was found [HttpGet("[controller]/getMaintenance")] public ActionResult 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); } } }