-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Added website URL storing API
- Loading branch information
Showing
7 changed files
with
280 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* ElementController | ||
* | ||
* @description :: Server-side actions for handling incoming requests. | ||
* @help :: See https://sailsjs.com/docs/concepts/actions | ||
*/ | ||
|
||
module.exports = { | ||
friendlyName: 'Delete', | ||
|
||
inputs: { | ||
}, | ||
|
||
exits: { | ||
success: { | ||
description: 'Note deleted.' | ||
}, | ||
}, | ||
|
||
fn: async function (inputs, exits) { | ||
let id = this.req.param('id', 0); | ||
|
||
let result = await Note.destroy({ | ||
id: id, | ||
user_id: this.req.me.id | ||
}); | ||
|
||
return exits.success({ | ||
deleted: true | ||
}); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* ElementController | ||
* | ||
* @description :: Server-side actions for handling incoming requests. | ||
* @help :: See https://sailsjs.com/docs/concepts/actions | ||
*/ | ||
|
||
module.exports = { | ||
friendlyName: 'Delete', | ||
|
||
inputs: { | ||
}, | ||
|
||
exits: { | ||
success: { | ||
description: 'Site deleted.' | ||
}, | ||
}, | ||
|
||
fn: async function (inputs, exits) { | ||
let id = this.req.param('id', 0); | ||
|
||
let result = await Site.destroy({ | ||
id: id, | ||
user_id: this.req.me.id | ||
}); | ||
|
||
return exits.success({ | ||
deleted: true | ||
}); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* NoteController | ||
* | ||
* @description :: Server-side actions for handling incoming requests. | ||
* @help :: See https://sailsjs.com/docs/concepts/actions | ||
*/ | ||
|
||
module.exports = { | ||
friendlyName: 'Store', | ||
|
||
inputs: { | ||
url: { | ||
type: 'string', | ||
defaultsTo: '', | ||
description: 'URL to store' | ||
}, | ||
|
||
tags: { | ||
type: 'string', | ||
description: 'Tags for the URL.', | ||
defaultsTo: '', | ||
maxLength: 2000 | ||
}, | ||
|
||
status: { | ||
type: 'number', | ||
defaultsTo: 1, | ||
description: 'Status of the Site' | ||
} | ||
}, | ||
|
||
exits: { | ||
success: { | ||
description: 'Site saved.' | ||
}, | ||
}, | ||
|
||
fn: async function (inputs, exits) { | ||
let id = this.req.param('id', 0); | ||
let element = null; | ||
|
||
if (!id) | ||
{ | ||
element = await Site.create(Object.assign({ | ||
user_id: this.req.me.id, | ||
url: inputs.url, | ||
tags: inputs.content, | ||
status: inputs.status | ||
})).fetch(); | ||
|
||
sails.log.info('New Site ' + inputs.url + ' for user ' + this.req.me.id + ' has been added'); | ||
} else { | ||
element = await Site.update({ | ||
id: id, | ||
user_id: this.req.me.id | ||
}, { | ||
// Should be checked before | ||
url: inputs.url, | ||
tags: inputs.content, | ||
status: inputs.status | ||
}).fetch(); | ||
} | ||
|
||
return exits.success({ | ||
element: element | ||
}); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* ElementController | ||
* | ||
* @description :: Server-side actions for handling incoming requests. | ||
* @help :: See https://sailsjs.com/docs/concepts/actions | ||
*/ | ||
|
||
module.exports = { | ||
friendlyName: 'View all Sites for this user', | ||
|
||
exits: { | ||
success: { | ||
description: 'Get the sites.' | ||
}, | ||
}, | ||
|
||
/** | ||
* TODO refactor | ||
* @param inputs | ||
* @param exits | ||
* @returns {Promise<*|{description}|{description, outputVariableName, outputType}|{description, outputType}|{description, outputVariableName, outputExample}|{description, outputExample}>} | ||
*/ | ||
fn: async function (inputs, exits) { | ||
let offset = this.req.param('offset', 0); | ||
let limit = this.req.param('limit', 1000); | ||
|
||
// Searches all Columns (kind, title, url, comment, username etc.) | ||
let filterSearch = this.req.param('search', ''); | ||
|
||
// Ordering (Defaults to newest items first (performance)) | ||
let orderBy = this.req.param('order_by', 'id DESC'); | ||
|
||
let allowedOrderBy = ['id ASC', 'id DESC', 'createdAt ASC', 'createdAt DESC', | ||
'url ASC', 'url DESC', 'modifiedAt ASC', 'modified DESC']; | ||
|
||
// Check if it's a valid ordering | ||
if (allowedOrderBy.indexOf(orderBy) === -1) { | ||
sails.log.warn('Error unknown order by ' + orderBy); | ||
orderBy = 'id DESC'; | ||
} | ||
|
||
// TODO Check values | ||
if (limit > 1000) { | ||
limit = 1000; | ||
} | ||
|
||
// Build where | ||
let where = { | ||
user_id: this.req.me.id, | ||
}; | ||
|
||
if (filterSearch) { | ||
where.or = []; | ||
|
||
where.or.push( | ||
{url: {'contains': filterSearch}}, | ||
{tags: {'contains': filterSearch}}, | ||
); | ||
} | ||
|
||
let total = await Site.count({ | ||
where: where | ||
}); | ||
|
||
let sites = await Site.find({ | ||
where: where, | ||
skip: offset, | ||
limit: limit, | ||
sort: orderBy | ||
}); | ||
|
||
sails.log.info('Query for Site by ' + this.req.me.id + '.'); | ||
|
||
return exits.success({ | ||
sites: sites, | ||
total: total | ||
}); | ||
}, | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Note.js | ||
* | ||
* @description :: A model definition. Represents a database table/collection/etc. | ||
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models | ||
*/ | ||
|
||
module.exports = { | ||
|
||
attributes: { | ||
|
||
// ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗ | ||
// ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗ | ||
// ╩ ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝ | ||
|
||
// (id, kind, user_id, title, url, username, password, form_data, comment, created, modified, status) | ||
|
||
user_id: { | ||
type: 'number', | ||
description: 'User ID', | ||
protect: true, | ||
required: true, | ||
example: 42 | ||
}, | ||
|
||
url: { | ||
type: 'string', | ||
description: 'URL of the site', | ||
maxLength: 2000, | ||
columnType: 'varchar(2000)' | ||
}, | ||
|
||
tags: { | ||
type: 'string', | ||
defaultsTo: '', | ||
description: 'Optional Tags', | ||
maxLength: 2000 | ||
}, | ||
|
||
status: { | ||
type: 'number', | ||
defaultsTo: 1, | ||
description: 'Status of the Element' | ||
} | ||
|
||
|
||
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗ | ||
// ║╣ ║║║╠╩╗║╣ ║║╚═╗ | ||
// ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝ | ||
|
||
|
||
// ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗ | ||
// ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗ | ||
// ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝ | ||
|
||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters