Skip to content

Commit

Permalink
Schema function generateCleanData renamed as getAsJson. Return value …
Browse files Browse the repository at this point in the history
…changed to a Promise so that we can expand the functionaliity in the future to support db queries.
  • Loading branch information
MKHenson committed Apr 25, 2016
1 parent 39d4389 commit e1bb08a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 42 deletions.
11 changes: 6 additions & 5 deletions server/src/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ export class Controller
* Transforms an array of model instances to its data ready state that can be sent to the client
* @param {ModelInstance} instances The instances to transform
* @param {boolean} instances If true, sensitive data will not be sanitized
* @returns {Array<T>}
* @returns {Promise<Array<T>>}
*/
getSanitizedData<T>(instances: Array<ModelInstance<T>>, verbose: boolean = false): Array<T>
getSanitizedData<T>(instances: Array<ModelInstance<T>>, verbose: boolean = false): Promise<Array<T>>
{
var sanitizedData = [];
var sanitizedData : Array<Promise<T>> = [];

for (var i = 0, l = instances.length; i < l; i++)
sanitizedData.push(instances[i].schema.generateCleanData<T>(!verbose, instances[i]._id));
sanitizedData.push(instances[i].schema.getAsJson<T>(!verbose, instances[i]._id));

return sanitizedData;
return Promise.all(sanitizedData);
}
}
6 changes: 4 additions & 2 deletions server/src/controllers/page-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,16 @@ export default class PageRenderer extends Controller

}).then(function (instances)
{
var sanitizedData = that.getSanitizedData(instances, Boolean(req.query.verbose));
return that.getSanitizedData(instances, Boolean(req.query.verbose));

}).then(function(sanitizedData){

res.end(JSON.stringify(<IGetRenders>{
error: false,
count: count,
message: `Found ${count} renders`,
data: sanitizedData
}));

}).catch(function (error: Error)
{
winston.error(error.message, { process: process.pid });
Expand Down
45 changes: 27 additions & 18 deletions server/src/controllers/posts-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default class PostsController extends Controller
}

// Sort by the date created
var sort: IPost = { createdOn: sortOrder };
var sort: mp.IPost = { createdOn: sortOrder };

// Optionally sort by the last updated
if (req.query.sort)
Expand All @@ -166,15 +166,18 @@ export default class PostsController extends Controller

}).then(function (instances)
{
var sanitizedData = that.getSanitizedData(instances, Boolean(req.query.verbose));
res.end(JSON.stringify(<mp.IGetPosts>{
return that.getSanitizedData(instances, Boolean(req.query.verbose));

}).then(function(sanitizedData){

res.end(JSON.stringify(<mp.IGetPosts>{
error: false,
count: count,
message: `Found ${count} posts`,
data: sanitizedData
}));

}).catch(function (error: Error)
}).catch(function (error: Error)
{
winston.error(error.message, { process: process.pid });
res.end(JSON.stringify(<mp.IResponse>{
Expand Down Expand Up @@ -207,20 +210,15 @@ export default class PostsController extends Controller

// Only admins are allowed to see private posts
if (!instances[0].schema.getByName("public").getValue() && ( !user || users.hasPermission(user, 2) == false ) )
{
res.end(JSON.stringify(<mp.IResponse>{
error: true,
message: "That post is marked private"
}));
return Promise.reject(new Error("That post is marked private"));

return;
}
return that.getSanitizedData<mp.IPost>(instances, Boolean(req.query.verbose));

var sanitizedData = that.getSanitizedData<mp.IPost>(instances, Boolean(req.query.verbose));
}).then(function(sanitizedData){

res.end(JSON.stringify(<mp.IGetPost>{
error: false,
message: `Found ${instances.length} posts`,
message: `Found ${sanitizedData.length} posts`,
data: sanitizedData[0]
}));

Expand Down Expand Up @@ -248,11 +246,14 @@ export default class PostsController extends Controller

categories.findInstances<mp.ICategory>({}, {}, parseInt(req.query.index), parseInt(req.query.limit)).then(function (instances)
{
var sanitizedData = that.getSanitizedData(instances, Boolean(req.query.verbose));
res.end(JSON.stringify(<mp.IGetCategories>{
return that.getSanitizedData(instances, Boolean(req.query.verbose));

}).then(function(sanitizedData){

res.end(JSON.stringify(<mp.IGetCategories>{
error: false,
count: sanitizedData.length,
message: `Found ${instances.length} categories`,
message: `Found ${sanitizedData.length} categories`,
data: sanitizedData
}));

Expand Down Expand Up @@ -384,10 +385,14 @@ export default class PostsController extends Controller

posts.createInstance(token).then(function (instance)
{
return instance.schema.getAsJson(false, instance._id);

}).then(function(json){

res.end(JSON.stringify(<mp.IGetPost>{
error: false,
message: "New post created",
data: instance.schema.generateCleanData(false, instance._id)
data: json
}));

}).catch(function (error: Error)
Expand All @@ -414,10 +419,14 @@ export default class PostsController extends Controller

categories.createInstance(token).then(function (instance)
{
return instance.schema.getAsJson(true, instance._id);

}).then(function(json){

res.end(JSON.stringify(<mp.IGetCategory>{
error: false,
message: "New category created",
data: instance.schema.generateCleanData(true, instance._id)
data: json
}));

}).catch(function (error: Error)
Expand Down
1 change: 1 addition & 0 deletions server/src/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export class Model
// Complete
return resolve(instance);
}

}).catch(function(err: Error){
reject(err);
});
Expand Down
42 changes: 25 additions & 17 deletions server/src/models/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {SchemaItem} from "./schema-items/schema-item";
import {ObjectID} from "mongodb"
import {SchemaForeignKey} from "./schema-items/schema-foreign-key";
import * as mongodb from "mongodb"
import {ModelInstance} from "./model"
import {IModelEntry} from "modepress-api";

/**
Expand Down Expand Up @@ -63,7 +65,8 @@ export class Schema
}

/**
* De-serializes the schema items from the mongodb data entry
* De-serializes the schema items from the mongodb data entry.
* I.e. the data is the document from the DB and the schema item sets its values from the document
* @param {any} data
*/
public deserialize(data: any): any
Expand All @@ -88,28 +91,33 @@ export class Schema
}

/**
* Serializes the schema items into the JSON format for mongodb
* Serializes the schema items into the JSON
* @param {boolean} sanitize If true, the item has to sanitize the data before sending it
* @returns {any}
* @param {ObjectID} id The models dont store the _id property directly, and so this has to be passed for serialization
* @returns {Promise<T>}
*/
public generateCleanData<T>(sanitize: boolean, id: ObjectID): T
public getAsJson<T>( sanitize: boolean, id: mongodb.ObjectID ): Promise<T>
{
var toReturn : T = <any>{};
var items = this._items;
var that = this;

for (var i = 0, l = items.length; i < l; i++)
{
// If this data is sensitive and the request must be sanitized
// then skip the item
if ( items[i].getSensitive() && sanitize )
continue;
return new Promise<T>(function( resolve, reject ) {

toReturn[items[i].name] = items[i].getValue();
}
var toReturn : T = <any>{};
var items = that._items;

for (var i = 0, l = items.length; i < l; i++)
{
// If this data is sensitive and the request must be sanitized
// then skip the item
if ( items[i].getSensitive() && sanitize )
continue;

(<IModelEntry>toReturn)._id = id;
toReturn[items[i].name] = items[i].getValue();
}

return toReturn;
(<IModelEntry>toReturn)._id = id;
resolve(toReturn);
});
}

/**
Expand Down

0 comments on commit e1bb08a

Please sign in to comment.