Skip to content

Commit

Permalink
[Feature] Added website URL storing API
Browse files Browse the repository at this point in the history
  • Loading branch information
yvesh committed Apr 26, 2018
1 parent c17c272 commit 7443169
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 1 deletion.
33 changes: 33 additions & 0 deletions api/controllers/note/delete.js
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
});
}
};

33 changes: 33 additions & 0 deletions api/controllers/site/delete.js
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
});
}
};

69 changes: 69 additions & 0 deletions api/controllers/site/store.js
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
});
}
};

81 changes: 81 additions & 0 deletions api/controllers/site/view-all.js
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
});
},

};

57 changes: 57 additions & 0 deletions api/models/Site.js
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'
}


// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
// ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝


// ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
// ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗
// ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝

},
};
2 changes: 1 addition & 1 deletion assets/js/cloud.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Cloud.setup({

/* eslint-disable */
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":[]}}
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":[]}}
/* eslint-enable */

});
6 changes: 6 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ module.exports.routes = {
'GET /api/v1/notes': {action: 'note/view-all'},
'POST /api/v1/notes': {action: 'note/store'},
'PATCH /api/v1/notes/:id': {action: 'note/store'},
'DELETE /api/v1/notes/:id': {action: 'note/delete'},

// Sites
'GET /api/v1/sites': {action: 'site/view-all'},
'POST /api/v1/sites': {action: 'site/store'},
'PATCH /api/v1/sites/:id': {action: 'site/store'},
'DELETE /api/v1/sites/:id': {action: 'site/delete'},

// ╦ ╦╔═╗╔╗ ╦ ╦╔═╗╔═╗╦╔═╔═╗
// ║║║║╣ ╠╩╗╠═╣║ ║║ ║╠╩╗╚═╗
Expand Down

0 comments on commit 7443169

Please sign in to comment.