using System.Collections.Generic; using System.Linq; using Comal.Classes; using InABox.Clients; using InABox.Core; namespace PRSServer { /// /// Provides an interface for interacting with the database's WebTemplates and WebStyles /// public class WebDatabaseInterface { /// /// Gets a WebTemplate class by its slug and associated data model /// /// The datamodel associated /// The slug of the URL /// A WebTemplate if it exists, null otherwise public static WebTemplate? GetWebTemplateByDataModelAndSlug(string dataModel, string slug) { var htmlTable = new Client().Query( new Filter(x => x.Slug).IsEqualTo(slug).And(x => x.DataModel).IsEqualTo(dataModel), Columns.None().Add(x => x.ID).Add(x => x.Template) ); if (htmlTable.Rows.Count > 1) { Logger.Send(LogType.Error, ClientFactory.UserID, "Ambiguous request; multiple template pages with the given data model '" + dataModel + "' and slug '" + slug + "'"); return null; } if (htmlTable.Rows.Count == 0) return null; return htmlTable.Rows[0].ToObject(); } /// /// Gets a WebTemplate class by its slug, where the DataModel is empty /// /// The slug of the URL /// A WebTemplate if it exists, null otherwise public static WebTemplate? GetWebTemplateBySlug(string slug) { var htmlTable = new Client().Query( new Filter(x => x.Slug).IsEqualTo(slug).And(x => x.DataModel).IsEqualTo(""), Columns.None().Add(x => x.ID).Add(x => x.Template) ); if (htmlTable.Rows.Count > 1) { Logger.Send(LogType.Error, ClientFactory.UserID, "Ambiguous request; multiple template pages with the slug '" + slug + "'"); return null; } if (htmlTable.Rows.Count == 0) return null; return htmlTable.Rows[0].ToObject(); } /// /// Accesses the stylesheets associated with the codes 'stylesheetCodes' /// /// A list of stylesheet codes /// A dictionary with 'Code => Style' entries public static Dictionary GetStylesheets(params string[] stylesheetCodes) { var stylesTable = new Client().Query( new Filter(x => x.Code).InList(stylesheetCodes.ToArray()), Columns.None().Add(x => x.ID).Add(x => x.Code).Add(x => x.Style) ); var stylesheets = new Dictionary(); foreach (var row in stylesTable.Rows) { var webStyle = row.ToObject(); stylesheets[webStyle.Code] = webStyle; } return stylesheets; } /// /// If getting multiple stylesheets, do not use this function; use GetStylesheets() instead - it is more efficient /// /// The code associated with the requested stylesheet /// The stylesheet associated with the code if it exists, null otherwise public static WebStyle? GetStylesheet(string stylesheetCode) { var stylesTable = new Client().Query( new Filter(x => x.Code).IsEqualTo(stylesheetCode), Columns.None().Add(x => x.ID).Add(x => x.Code).Add(x => x.Style) ); return stylesTable.Rows.ElementAtOrDefault(0)?.ToObject(); } } }