1 /**
2 * Copyright © Webd 2018
3 * License: MIT (https://github.com/DiamondMVC/Webd/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module webd.dal.webpages;
7 
8 import diamond.database;
9 
10 import webd.models.database;
11 
12 /**
13 * Gets an web page from its name.
14 * Params:
15 *   name = The name of the web page.
16 * Returns:
17 *   The web page's db representation.
18 */
19 auto getWebPage(string name)
20 {
21   static const sql = "SELECT * FROM @table WHERE `name` = @name AND `published` = '1' AND `deleted` = '0' LIMIT 1";
22 
23   auto params = getParams();
24   params["name"] = name;
25 
26   return MySql.readSingle!WebdWebPage(sql, params);
27 }
28 
29 /**
30 * Gets an web page from its route.
31 * Params:
32 *   route = The route of the web page.
33 * Returns:
34 *   The web page's db representation.
35 */
36 auto getWebPageFromRoute(string route)
37 {
38   static const sql = "SELECT * FROM @table WHERE `route` = @route AND `published` = '1' AND `deleted` = '0' LIMIT 1";
39 
40   auto params = getParams();
41   params["route"] = route;
42 
43   return MySql.readSingle!WebdWebPage(sql, params);
44 }
45 
46 /**
47 * Gets an web page from its id.
48 * Params:
49 *   id = The id of the web page.
50 * Returns:
51 *   The web page's db representation.
52 */
53 auto getWebPage(ulong id)
54 {
55   static const sql = "SELECT * FROM @table WHERE `id` = @id AND `published` = '1' AND `deleted` = '0' LIMIT 1";
56 
57   auto params = getParams();
58   params["id"] = id;
59 
60   return MySql.readSingle!WebdWebPage(sql, params);
61 }
62 
63 /**
64 * Gets a range of all web pages.
65 * Returns:
66 *   A range of the web pages' db representation.
67 */
68 auto getAllWebPages()
69 {
70   static const sql = "SELECT * FROM @table WHERE `published` = '1' AND `deleted` = '0' ORDER BY `id` DESC";
71 
72   return MySql.readMany!WebdWebPage(sql, null);
73 }
74 
75 /**
76 * Gets an array of all sub web pages.
77 * Params:
78 *   parentPageId = The id of the parent page.
79 * Returns:
80 *   An array of the sub web pages' db representation.
81 */
82 WebdWebPage[] getAllSubWebPages(ulong parentPageId)
83 {
84   static const sql = "SELECT * FROM @table WHERE `parentPage` = @parentPage AND `published` = '1' AND `deleted` = '0' ORDER BY `id` DESC";
85   auto params = getParams();
86   params["parentPage"] = parentPageId;
87 
88   import std.array : array;
89 
90   auto result = MySql.readMany!WebdWebPage(sql, params).array;
91 
92   foreach (page; result)
93   {
94     result ~= getAllSubWebPages(page.id);
95   }
96 
97   return result;
98 }
99 
100 /**
101 * Gets a range of all deleted web pages.
102 * Returns:
103 *   The range of the deleted web pages' db representation.
104 */
105 auto getAllDeletedWebPages()
106 {
107   static const sql = "SELECT * FROM @table WHERE `deleted` = '1' ORDER BY `id` DESC";
108 
109   return MySql.readMany!WebdWebPage(sql, null);
110 }