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