Commit aa462081 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs
Browse files

Refactor and simplify (coscine/issues#1296)

parent 12786e15
......@@ -64,45 +64,28 @@ namespace Coscine.Api.Notices.Controllers
/// <summary>
/// Returns defined properties of the first entry of the rss feed
/// </summary>
/// <returns>Maintenance</returns>
/// <returns>Maintenance or 404 if no maintenance was found</returns>
[HttpGet("[controller]/getMaintenance")]
public IActionResult GetMaintenance()
public ActionResult<MaintenanceReturnObject> GetMaintenance()
{
var maintenance = _maintenanceHelper.GetMaintenance(_rssUrl);
if(maintenance == null)
if (maintenance == null)
{
return NotFound("No maintenance was found.");
}
var maintenanceReturnObjectDe = new MaintenanceReturnObject
var maintenanceReturnObject = new MaintenanceReturnObject
{
Body = maintenance.Body,
DisplayName = maintenance.DisplayName,
EndsDate = maintenance.EndsDate.ToString("dddd dd.MM.yyyy HH:mm"),
StartsDate = (maintenance.EndsDate == DateTime.MaxValue) ? "unbekannt" : maintenance.EndsDate.ToString("dddd dd.MM.yyyy HH:mm"),
Type = maintenance.GetTranslatedType(Language.de),
EndsDate = maintenance.EndsDate != DateTime.MaxValue ? maintenance.EndsDate : null,
StartsDate = maintenance.StartsDate != DateTime.MinValue ? maintenance.StartsDate : null,
Type = maintenance.Type,
Url = maintenance.Url,
};
var maintenanceReturnObjectEn = new MaintenanceReturnObject
{
Body = maintenance.Body,
DisplayName = maintenance.DisplayName,
EndsDate = maintenance.EndsDate.ToString("dddd dd.MM.yyyy HH:mm"),
StartsDate = (maintenance.EndsDate == DateTime.MaxValue) ? "unknown" : maintenance.EndsDate.ToString("dddd dd.MM.yyyy HH:mm"),
Type = maintenance.GetTranslatedType(Language.en),
Url = maintenance.Url,
};
return Json(new
{
data = new
{
de = new List<MaintenanceReturnObject> { maintenanceReturnObjectDe },
en = new List<MaintenanceReturnObject> { maintenanceReturnObjectEn },
}
});
return Ok(maintenanceReturnObject);
}
}
......
......@@ -11,25 +11,30 @@ namespace Coscine.Api.Notices.ReturnObjects
/// Maintenance title.
/// </summary>
public string DisplayName { get; set; }
/// <summary>
/// Maintenance url.
/// </summary>
public Uri Url { get; set; }
/// <summary>
/// Maintenance type.
/// </summary>
public string Type { get; set; }
/// <summary>
/// Maintenance description.
/// </summary>
public string Body { get; set; }
/// <summary>
/// Maintenance start.
/// </summary>
public string StartsDate { get; set; }
public DateTime? StartsDate { get; set; }
/// <summary>
/// Maintenance end.
/// </summary>
public string EndsDate { get; set; }
public DateTime? EndsDate { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace Coscine.Api.Notices.Utils
{
......@@ -12,53 +11,31 @@ namespace Coscine.Api.Notices.Utils
/// Maintenance title.
/// </summary>
public string DisplayName { get; set; }
/// <summary>
/// Maintenance url.
/// </summary>
public Uri Url { get; set; }
/// <summary>
/// Maintenance type.
/// </summary>
public string Type { get; set; }
/// <summary>
/// Maintenance description.
/// </summary>
public string Body { get; set; }
/// <summary>
/// Maintenance start.
/// </summary>
public DateTime StartsDate { get; set; }
/// <summary>
/// Maintenance end.
/// </summary>
public DateTime EndsDate { get; set; }
/// <summary>
/// Gets the type in the specified language.
/// If the language is not supported, the currently set version will be returned.
/// <param name="language">Language (e.g. "Language.en" or "Language.de")</param>
/// <returns>Translated type</returns>
/// </summary>
public string GetTranslatedType(Language language)
{
if (language == Language.de)
{
return Type;
}
return _translations.ContainsKey(Type) ? _translations[Type] : Type;
}
private static readonly Dictionary<string, string> _translations = new Dictionary<string, string> {
{ "Störung", "Malfunction" },
{ "Teilstörung", "Partial malfunction" },
{ "Unterbrechung", "Interruption" },
{ "eingeschränkt betriebsfähig", "Limited operability" },
{ "Wartung", "Maintenance" },
{ "Teilwartung", "Partial maintenance" },
{ "Änderung", "Change" },
{ "Warnung", "Warning" },
{ "-NotDefined-", "Not defined" },
{ "Hinweis", "Notice" }
};
}
}
......@@ -8,7 +8,7 @@ using System.Xml;
namespace Coscine.Api.Notices.Utils
{
/// <summary>
/// This class
/// This class helps to retrieve the relevant maintenance message for a certain service.
/// </summary>
public class MaintenanceHelper
{
......@@ -20,18 +20,17 @@ namespace Coscine.Api.Notices.Utils
/// <summary>
/// Order of relevance to be used.
/// Only the most relevant (first in the list) maintenace message is later displayed.
/// All non listed "Störungen" are sorted as "-NotDefined-".
/// Use this list to adjust the order.
/// Types which are not part of the list, will be set to the UndefinedString property.
/// </summary>
//Reihenfolge der Relevanz der Störung; Alle nicht aufgelisteten Störungen werden als "-NotDefined-" einsortiert;
//Es wird nur die relevanteste Störung zurückgegeben
public List<string> RelevanceList { get; set; }
/// <summary>
/// Get the most relevant maintenance from the rss feed
/// </summary>
/// <param name="feedUrl">Url of the rss feed</param>
/// <returns></returns>
/// <returns>The most relevant Maintenance</returns>
internal Maintenance GetMaintenance(string feedUrl)
{
return GetMaintenance(feedUrl, RelevanceList);
......@@ -40,9 +39,9 @@ namespace Coscine.Api.Notices.Utils
/// <summary>
/// Get the most relevant maintenance from the rss feed
/// </summary>
/// <param name="feedUrl">Url of the rss feed</param>
/// <param name="feedUrl">URL of the rss feed</param>
/// <param name="order">Order of Importance</param>
/// <returns></returns>
/// <returns>The most relevant Maintenance</returns>
internal Maintenance GetMaintenance(string feedUrl, List<string> order)
{
Maintenance result = null;
......@@ -52,99 +51,96 @@ namespace Coscine.Api.Notices.Utils
order.Add(UndefinedString);
}
int resultOrderPos = order.Count() + 1; ;
try
{
var rssfeed = SyndicationFeed.Load(XmlReader.Create(feedUrl));
var sol = rssfeed.Items.OrderByDescending(x => x.PublishDate).ToList();
int resultOrderPos = order.Count + 1;
var rssfeed = SyndicationFeed.Load(XmlReader.Create(feedUrl));
var syndicationItems = rssfeed.Items.OrderByDescending(x => x.PublishDate).ToList();
foreach (var i in sol)
foreach (var syndicationItem in syndicationItems)
{
try
{
try
if (syndicationItem != null && syndicationItem.Summary != null && !string.IsNullOrWhiteSpace(syndicationItem.Summary.Text))
{
if (i != null && i.Summary != null && !String.IsNullOrWhiteSpace(i.Summary.Text))
var current = new Maintenance
{
var current = new Maintenance
{
DisplayName = i.Title.Text,
Url = i.Links[0].Uri
};
DisplayName = syndicationItem.Title.Text,
Url = syndicationItem.Links[0].Uri
};
string summary = i.Summary.Text;
int splitter = summary.IndexOf('-');
string summary = syndicationItem.Summary.Text;
int splitter = summary.IndexOf('-');
if (splitter != -1)
{
current.Body = summary.Substring(splitter + 1, summary.Length - splitter - 1).Trim();
string firstPart = summary.Substring(0, splitter).Trim();
splitter = firstPart.IndexOf("von");
if (splitter != -1)
{
current.Body = summary.Substring(splitter + 1, summary.Length - splitter - 1).Trim();
string firstPart = summary.Substring(0, splitter).Trim();
splitter = firstPart.IndexOf("von");
current.Type = firstPart.Substring(0, splitter).Trim();
string timespan = firstPart.Substring(splitter + 3, firstPart.Length - splitter - 3).Trim();
splitter = timespan.IndexOf("bis");
if (splitter != -1)
{
current.Type = firstPart.Substring(0, splitter).Trim();
string timespan = firstPart.Substring(splitter + 3, firstPart.Length - splitter - 3).Trim();
string startsString = timespan.Substring(0, splitter).Trim();
string endsString = timespan.Substring(splitter + 3, timespan.Length - splitter - 3).Trim();
splitter = timespan.IndexOf("bis");
if (splitter != -1)
{
string startsString = timespan.Substring(0, splitter).Trim();
string endsString = timespan.Substring(splitter + 3, timespan.Length - splitter - 3).Trim();
var dayList = new List<string> { "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag" };
var dayList = new List<string> { "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag" };
foreach (var dayString in dayList)
{
startsString = startsString.Replace(dayString, "").Trim();
startsString = startsString.Replace(dayString.ToLower(), "").Trim();
}
foreach (var dayString in dayList)
{
startsString = startsString.Replace(dayString, "").Trim();
startsString = startsString.Replace(dayString.ToLower(), "").Trim();
}
foreach (var dayString in dayList)
{
endsString = endsString.Replace(dayString, "").Trim();
endsString = endsString.Replace(dayString.ToLower(), "").Trim();
}
foreach (var dayString in dayList)
{
endsString = endsString.Replace(dayString, "").Trim();
endsString = endsString.Replace(dayString.ToLower(), "").Trim();
}
if (startsString.ToLower() == "unbekannt")
{
current.StartsDate = DateTime.MinValue;
}
else
{
current.StartsDate = DateTime.ParseExact(startsString, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
}
if (startsString.ToLower() == "unbekannt")
{
current.StartsDate = DateTime.MinValue;
}
else
{
current.StartsDate = DateTime.ParseExact(startsString, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
}
if (endsString.ToLower() == "unbekannt")
{
current.EndsDate = DateTime.MaxValue;
}
else
{
current.EndsDate = DateTime.ParseExact(endsString, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
}
if (endsString.ToLower() == "unbekannt")
// still active message
if (current.EndsDate > DateTime.Now && current.StartsDate < DateTime.Now)
{
// first listed valid message
if (result == null)
{
current.EndsDate = DateTime.MaxValue;
result = current;
resultOrderPos = GetOrderPos(order, result.Type);
}
else
{
current.EndsDate = DateTime.ParseExact(endsString, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
}
//still active message
if (current.EndsDate > DateTime.Now && current.StartsDate < DateTime.Now)
{
//first listet valid message
if (result == null)
// check if the current message is higher ranged as the previosly result
int currentOrderPos = GetOrderPos(order, current.Type);
// higher rang of type
if (currentOrderPos < resultOrderPos)
{
result = current;
resultOrderPos = GetOrderPos(order, result.Type);
resultOrderPos = currentOrderPos;
// same type but longer active
}
else
else if (currentOrderPos < resultOrderPos && current.EndsDate > result.EndsDate)
{
//check if the current message is higher ranged as the previosly result
int currentOrderPos = GetOrderPos(order, current.Type);
//higher rang of type
if (currentOrderPos < resultOrderPos)
{
result = current;
resultOrderPos = currentOrderPos;
// same type but longer active
}
else if (currentOrderPos < resultOrderPos && current.EndsDate > result.EndsDate)
{
result = current;
resultOrderPos = currentOrderPos;
}
result = current;
resultOrderPos = currentOrderPos;
}
}
}
......@@ -152,18 +148,12 @@ namespace Coscine.Api.Notices.Utils
}
}
}
catch (Exception)
{
//ignore not correct parsed messages
}
}
catch (Exception)
{
// ignore not correct parsed messages
}
}
catch (Exception e)
{
//throw new ApiRequestException("RSS Feed could not be parsed", 5324);
throw e;
}
return result;
}
......@@ -178,7 +168,7 @@ namespace Coscine.Api.Notices.Utils
if (res == -1)
{
res = order.Count() + 1;
res = order.Count + 1;
}
return res;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment