@@ -20,24 +20,70 @@ async function routes (app, options) {
2020 return { hello : 'world' }
2121 } )
2222
23+ /**
24+ * CREATE SITE
25+ */
2326 app . post ( '/' , {
27+ config : {
28+ // permissions: ['create:sites', 'manage:sites']
29+ } ,
2430 schema : {
2531 summary : 'Create a new site' ,
2632 tags : [ 'Sites' ] ,
2733 body : {
2834 type : 'object' ,
29- required : [ 'name ' , 'hostname ' ] ,
35+ required : [ 'hostname ' , 'title ' ] ,
3036 properties : {
31- name : { type : 'string' } ,
32- hostname : { type : 'string' }
37+ hostname : {
38+ type : 'string' ,
39+ minLength : 1 ,
40+ maxLength : 255 ,
41+ pattern : '^(\\*|[a-z0-9.-]+)$'
42+ } ,
43+ title : {
44+ type : 'string' ,
45+ minLength : 1 ,
46+ maxLength : 255
47+ }
48+ } ,
49+ examples : [
50+ {
51+ hostname : 'wiki.example.org' ,
52+ title : 'My Wiki Site'
53+ }
54+ ]
55+ } ,
56+ response : {
57+ 200 : {
58+ description : 'Site created successfully' ,
59+ type : 'object' ,
60+ properties : {
61+ message : {
62+ type : 'string'
63+ } ,
64+ id : {
65+ type : 'string' ,
66+ format : 'uuid'
67+ }
68+ }
3369 }
3470 }
3571 }
3672 } , async ( req , reply ) => {
37- return { hello : 'world' }
73+ const result = await WIKI . models . sites . createSite ( req . body . hostname , { title : req . body . title } )
74+ return {
75+ message : 'Site created successfully.' ,
76+ id : result . id
77+ }
3878 } )
3979
80+ /**
81+ * UPDATE SITE
82+ */
4083 app . put ( '/:siteId' , {
84+ config : {
85+ permissions : [ 'manage:sites' ]
86+ } ,
4187 schema : {
4288 summary : 'Update a site' ,
4389 tags : [ 'Sites' ]
@@ -50,6 +96,9 @@ async function routes (app, options) {
5096 * DELETE SITE
5197 */
5298 app . delete ( '/:siteId' , {
99+ config : {
100+ permissions : [ 'manage:sites' ]
101+ } ,
53102 schema : {
54103 summary : 'Delete a site' ,
55104 tags : [ 'Sites' ] ,
@@ -71,7 +120,9 @@ async function routes (app, options) {
71120 }
72121 } , async ( req , reply ) => {
73122 try {
74- if ( await WIKI . models . sites . deleteSite ( req . params . siteId ) ) {
123+ if ( await WIKI . models . sites . countSites ( ) <= 1 ) {
124+ reply . conflict ( 'Cannot delete the last site. At least 1 site must exist at all times.' )
125+ } else if ( await WIKI . models . sites . deleteSite ( req . params . siteId ) ) {
75126 reply . code ( 204 )
76127 } else {
77128 reply . badRequest ( 'Site does not exist.' )
0 commit comments