Skip to content

Commit 7443169

Browse files
committed
[Feature] Added website URL storing API
1 parent c17c272 commit 7443169

File tree

7 files changed

+280
-1
lines changed

7 files changed

+280
-1
lines changed

api/controllers/note/delete.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* ElementController
3+
*
4+
* @description :: Server-side actions for handling incoming requests.
5+
* @help :: See https://sailsjs.com/docs/concepts/actions
6+
*/
7+
8+
module.exports = {
9+
friendlyName: 'Delete',
10+
11+
inputs: {
12+
},
13+
14+
exits: {
15+
success: {
16+
description: 'Note deleted.'
17+
},
18+
},
19+
20+
fn: async function (inputs, exits) {
21+
let id = this.req.param('id', 0);
22+
23+
let result = await Note.destroy({
24+
id: id,
25+
user_id: this.req.me.id
26+
});
27+
28+
return exits.success({
29+
deleted: true
30+
});
31+
}
32+
};
33+

api/controllers/site/delete.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* ElementController
3+
*
4+
* @description :: Server-side actions for handling incoming requests.
5+
* @help :: See https://sailsjs.com/docs/concepts/actions
6+
*/
7+
8+
module.exports = {
9+
friendlyName: 'Delete',
10+
11+
inputs: {
12+
},
13+
14+
exits: {
15+
success: {
16+
description: 'Site deleted.'
17+
},
18+
},
19+
20+
fn: async function (inputs, exits) {
21+
let id = this.req.param('id', 0);
22+
23+
let result = await Site.destroy({
24+
id: id,
25+
user_id: this.req.me.id
26+
});
27+
28+
return exits.success({
29+
deleted: true
30+
});
31+
}
32+
};
33+

api/controllers/site/store.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* NoteController
3+
*
4+
* @description :: Server-side actions for handling incoming requests.
5+
* @help :: See https://sailsjs.com/docs/concepts/actions
6+
*/
7+
8+
module.exports = {
9+
friendlyName: 'Store',
10+
11+
inputs: {
12+
url: {
13+
type: 'string',
14+
defaultsTo: '',
15+
description: 'URL to store'
16+
},
17+
18+
tags: {
19+
type: 'string',
20+
description: 'Tags for the URL.',
21+
defaultsTo: '',
22+
maxLength: 2000
23+
},
24+
25+
status: {
26+
type: 'number',
27+
defaultsTo: 1,
28+
description: 'Status of the Site'
29+
}
30+
},
31+
32+
exits: {
33+
success: {
34+
description: 'Site saved.'
35+
},
36+
},
37+
38+
fn: async function (inputs, exits) {
39+
let id = this.req.param('id', 0);
40+
let element = null;
41+
42+
if (!id)
43+
{
44+
element = await Site.create(Object.assign({
45+
user_id: this.req.me.id,
46+
url: inputs.url,
47+
tags: inputs.content,
48+
status: inputs.status
49+
})).fetch();
50+
51+
sails.log.info('New Site ' + inputs.url + ' for user ' + this.req.me.id + ' has been added');
52+
} else {
53+
element = await Site.update({
54+
id: id,
55+
user_id: this.req.me.id
56+
}, {
57+
// Should be checked before
58+
url: inputs.url,
59+
tags: inputs.content,
60+
status: inputs.status
61+
}).fetch();
62+
}
63+
64+
return exits.success({
65+
element: element
66+
});
67+
}
68+
};
69+

api/controllers/site/view-all.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* ElementController
3+
*
4+
* @description :: Server-side actions for handling incoming requests.
5+
* @help :: See https://sailsjs.com/docs/concepts/actions
6+
*/
7+
8+
module.exports = {
9+
friendlyName: 'View all Sites for this user',
10+
11+
exits: {
12+
success: {
13+
description: 'Get the sites.'
14+
},
15+
},
16+
17+
/**
18+
* TODO refactor
19+
* @param inputs
20+
* @param exits
21+
* @returns {Promise<*|{description}|{description, outputVariableName, outputType}|{description, outputType}|{description, outputVariableName, outputExample}|{description, outputExample}>}
22+
*/
23+
fn: async function (inputs, exits) {
24+
let offset = this.req.param('offset', 0);
25+
let limit = this.req.param('limit', 1000);
26+
27+
// Searches all Columns (kind, title, url, comment, username etc.)
28+
let filterSearch = this.req.param('search', '');
29+
30+
// Ordering (Defaults to newest items first (performance))
31+
let orderBy = this.req.param('order_by', 'id DESC');
32+
33+
let allowedOrderBy = ['id ASC', 'id DESC', 'createdAt ASC', 'createdAt DESC',
34+
'url ASC', 'url DESC', 'modifiedAt ASC', 'modified DESC'];
35+
36+
// Check if it's a valid ordering
37+
if (allowedOrderBy.indexOf(orderBy) === -1) {
38+
sails.log.warn('Error unknown order by ' + orderBy);
39+
orderBy = 'id DESC';
40+
}
41+
42+
// TODO Check values
43+
if (limit > 1000) {
44+
limit = 1000;
45+
}
46+
47+
// Build where
48+
let where = {
49+
user_id: this.req.me.id,
50+
};
51+
52+
if (filterSearch) {
53+
where.or = [];
54+
55+
where.or.push(
56+
{url: {'contains': filterSearch}},
57+
{tags: {'contains': filterSearch}},
58+
);
59+
}
60+
61+
let total = await Site.count({
62+
where: where
63+
});
64+
65+
let sites = await Site.find({
66+
where: where,
67+
skip: offset,
68+
limit: limit,
69+
sort: orderBy
70+
});
71+
72+
sails.log.info('Query for Site by ' + this.req.me.id + '.');
73+
74+
return exits.success({
75+
sites: sites,
76+
total: total
77+
});
78+
},
79+
80+
};
81+

api/models/Site.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Note.js
3+
*
4+
* @description :: A model definition. Represents a database table/collection/etc.
5+
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
6+
*/
7+
8+
module.exports = {
9+
10+
attributes: {
11+
12+
// ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗
13+
// ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
14+
// ╩ ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝
15+
16+
// (id, kind, user_id, title, url, username, password, form_data, comment, created, modified, status)
17+
18+
user_id: {
19+
type: 'number',
20+
description: 'User ID',
21+
protect: true,
22+
required: true,
23+
example: 42
24+
},
25+
26+
url: {
27+
type: 'string',
28+
description: 'URL of the site',
29+
maxLength: 2000,
30+
columnType: 'varchar(2000)'
31+
},
32+
33+
tags: {
34+
type: 'string',
35+
defaultsTo: '',
36+
description: 'Optional Tags',
37+
maxLength: 2000
38+
},
39+
40+
status: {
41+
type: 'number',
42+
defaultsTo: 1,
43+
description: 'Status of the Element'
44+
}
45+
46+
47+
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
48+
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
49+
// ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝
50+
51+
52+
// ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
53+
// ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗
54+
// ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
55+
56+
},
57+
};

assets/js/cloud.setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Cloud.setup({
1414

1515
/* eslint-disable */
16-
methods: {"confirmEmail":{"verb":"GET","url":"/email/confirm","args":["token"]},"logout":{"verb":"GET","url":"/api/v1/account/logout","args":[]},"updatePassword":{"verb":"PUT","url":"/api/v1/account/update-password","args":["password"]},"updateProfile":{"verb":"PUT","url":"/api/v1/account/update-profile","args":["fullName","emailAddress"]},"updateBillingCard":{"verb":"PUT","url":"/api/v1/account/update-billing-card","args":["stripeToken","billingCardLast4","billingCardBrand","billingCardExpMonth","billingCardExpYear"]},"login":{"verb":"PUT","url":"/api/v1/entrance/login","args":["emailAddress","password","rememberMe"]},"signup":{"verb":"POST","url":"/api/v1/entrance/signup","args":["emailAddress","password","fullName"]},"sendPasswordRecoveryEmail":{"verb":"POST","url":"/api/v1/entrance/send-password-recovery-email","args":["emailAddress"]},"updatePasswordAndLogin":{"verb":"POST","url":"/api/v1/entrance/update-password-and-login","args":["password","token"]},"deliverContactFormMessage":{"verb":"POST","url":"/api/v1/deliver-contact-form-message","args":["emailAddress","topic","fullName","message"]},"grantCsrfToken":{"verb":"GET","url":"/csrf-token"},"view":{"verb":"GET","url":"/api/v1/elements/:id","args":[]},"store":{"verb":"POST","url":"/api/v1/notes","args":["group","title","url","content","status"]},"delete":{"verb":"DELETE","url":"/api/v1/elements/:id","args":[]}}
16+
methods: {"confirmEmail":{"verb":"GET","url":"/email/confirm","args":["token"]},"logout":{"verb":"GET","url":"/api/v1/account/logout","args":[]},"updatePassword":{"verb":"PUT","url":"/api/v1/account/update-password","args":["password"]},"updateProfile":{"verb":"PUT","url":"/api/v1/account/update-profile","args":["fullName","emailAddress"]},"updateBillingCard":{"verb":"PUT","url":"/api/v1/account/update-billing-card","args":["stripeToken","billingCardLast4","billingCardBrand","billingCardExpMonth","billingCardExpYear"]},"login":{"verb":"PUT","url":"/api/v1/entrance/login","args":["emailAddress","password","rememberMe"]},"signup":{"verb":"POST","url":"/api/v1/entrance/signup","args":["emailAddress","password","fullName"]},"sendPasswordRecoveryEmail":{"verb":"POST","url":"/api/v1/entrance/send-password-recovery-email","args":["emailAddress"]},"updatePasswordAndLogin":{"verb":"POST","url":"/api/v1/entrance/update-password-and-login","args":["password","token"]},"deliverContactFormMessage":{"verb":"POST","url":"/api/v1/deliver-contact-form-message","args":["emailAddress","topic","fullName","message"]},"grantCsrfToken":{"verb":"GET","url":"/csrf-token"},"view":{"verb":"GET","url":"/api/v1/elements/:id","args":[]},"store":{"verb":"POST","url":"/api/v1/sites","args":["url","tags","status"]},"delete":{"verb":"DELETE","url":"/api/v1/sites/:id","args":[]}}
1717
/* eslint-enable */
1818

1919
});

config/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ module.exports.routes = {
6565
'GET /api/v1/notes': {action: 'note/view-all'},
6666
'POST /api/v1/notes': {action: 'note/store'},
6767
'PATCH /api/v1/notes/:id': {action: 'note/store'},
68+
'DELETE /api/v1/notes/:id': {action: 'note/delete'},
6869

70+
// Sites
71+
'GET /api/v1/sites': {action: 'site/view-all'},
72+
'POST /api/v1/sites': {action: 'site/store'},
73+
'PATCH /api/v1/sites/:id': {action: 'site/store'},
74+
'DELETE /api/v1/sites/:id': {action: 'site/delete'},
6975

7076
// ╦ ╦╔═╗╔╗ ╦ ╦╔═╗╔═╗╦╔═╔═╗
7177
// ║║║║╣ ╠╩╗╠═╣║ ║║ ║╠╩╗╚═╗

0 commit comments

Comments
 (0)