WebDatabaseInterface.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Comal.Classes;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. namespace PRSServer
  7. {
  8. /// <summary>
  9. /// Provides an interface for interacting with the database's WebTemplates and WebStyles
  10. /// </summary>
  11. public class WebDatabaseInterface
  12. {
  13. /// <summary>
  14. /// Gets a WebTemplate class by its slug and associated data model
  15. /// </summary>
  16. /// <param name="dataModel">The datamodel associated</param>
  17. /// <param name="slug">The slug of the URL</param>
  18. /// <returns>A WebTemplate if it exists, null otherwise</returns>
  19. public static WebTemplate? GetWebTemplateByDataModelAndSlug(string dataModel, string slug)
  20. {
  21. var htmlTable = new Client<WebTemplate>().Query(
  22. new Filter<WebTemplate>(x => x.Slug).IsEqualTo(slug).And(x => x.DataModel).IsEqualTo(dataModel),
  23. Columns.None<WebTemplate>().Add(x => x.ID).Add(x => x.Template)
  24. );
  25. if (htmlTable.Rows.Count > 1)
  26. {
  27. Logger.Send(LogType.Error, ClientFactory.UserID,
  28. "Ambiguous request; multiple template pages with the given data model '" + dataModel + "' and slug '" + slug + "'");
  29. return null;
  30. }
  31. if (htmlTable.Rows.Count == 0) return null;
  32. return htmlTable.Rows[0].ToObject<WebTemplate>();
  33. }
  34. /// <summary>
  35. /// Gets a WebTemplate class by its slug, where the DataModel is empty
  36. /// </summary>
  37. /// <param name="slug">The slug of the URL</param>
  38. /// <returns>A WebTemplate if it exists, null otherwise</returns>
  39. public static WebTemplate? GetWebTemplateBySlug(string slug)
  40. {
  41. var htmlTable = new Client<WebTemplate>().Query(
  42. new Filter<WebTemplate>(x => x.Slug).IsEqualTo(slug).And(x => x.DataModel).IsEqualTo(""),
  43. Columns.None<WebTemplate>().Add(x => x.ID).Add(x => x.Template)
  44. );
  45. if (htmlTable.Rows.Count > 1)
  46. {
  47. Logger.Send(LogType.Error, ClientFactory.UserID,
  48. "Ambiguous request; multiple template pages with the slug '" + slug + "'");
  49. return null;
  50. }
  51. if (htmlTable.Rows.Count == 0) return null;
  52. return htmlTable.Rows[0].ToObject<WebTemplate>();
  53. }
  54. /// <summary>
  55. /// Accesses the stylesheets associated with the codes 'stylesheetCodes'
  56. /// </summary>
  57. /// <param name="stylesheetCodes">A list of stylesheet codes</param>
  58. /// <returns>A dictionary with 'Code => Style' entries</returns>
  59. public static Dictionary<string, WebStyle> GetStylesheets(params string[] stylesheetCodes)
  60. {
  61. var stylesTable = new Client<WebStyle>().Query(
  62. new Filter<WebStyle>(x => x.Code).InList(stylesheetCodes.ToArray()),
  63. Columns.None<WebStyle>().Add(x => x.ID).Add(x => x.Code).Add(x => x.Style)
  64. );
  65. var stylesheets = new Dictionary<string, WebStyle>();
  66. foreach (var row in stylesTable.Rows)
  67. {
  68. var webStyle = row.ToObject<WebStyle>();
  69. stylesheets[webStyle.Code] = webStyle;
  70. }
  71. return stylesheets;
  72. }
  73. /// <summary>
  74. /// If getting multiple stylesheets, do not use this function; use GetStylesheets() instead - it is more efficient
  75. /// </summary>
  76. /// <param name="stylesheetCode">The code associated with the requested stylesheet</param>
  77. /// <returns>The stylesheet associated with the code if it exists, null otherwise</returns>
  78. public static WebStyle? GetStylesheet(string stylesheetCode)
  79. {
  80. var stylesTable = new Client<WebStyle>().Query(
  81. new Filter<WebStyle>(x => x.Code).IsEqualTo(stylesheetCode),
  82. Columns.None<WebStyle>().Add(x => x.ID).Add(x => x.Code).Add(x => x.Style)
  83. );
  84. return stylesTable.Rows.ElementAtOrDefault(0)?.ToObject<WebStyle>();
  85. }
  86. }
  87. }