Skip to content

Commit

Permalink
Merge pull request #33 from locator-kn/trips
Browse files Browse the repository at this point in the history
New method and view for getting trips from a certain user
  • Loading branch information
timoweiss committed Jun 6, 2015
2 parents 77bbac6 + cbeb03c commit 7cffe5e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/attachment/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Attachment {
// get revision from database with HEAD
this.db.head(documentId, (err, headers, res) => {
if (res === 404 || !headers['etag']) {
return reject(this.boom.create(404, "user not found"));
return reject(this.boom.create(404, "document not found"));
}

if (err) {
Expand Down
3 changes: 3 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Database {
LIST_DATA_CITY_TRIPS: 'data/listall/cities_trips',
LIST_TRIP_ALL: 'trip/listall/trip',
LIST_TRIP_CITY: 'trip/listall/city',
LIST_TRIP_USER: 'trip/listall/tripByUserId',
LIST_MAIL_REGISTRATION: 'mail/listall/registration',
LIST_MAIL_PASSWORD_FORGOTTEN: 'mail/listall/password_forgotten',
LIST_CHAT_CONVERSATIONS: 'chat/listallByUserId/conversationsByUserId',
Expand Down Expand Up @@ -146,6 +147,7 @@ class Database {
server.expose('updateTrip', this.trip.updateTrip);
server.expose('createTrip', this.trip.createTrip);
server.expose('deleteTripById', this.trip.deleteTripById);
server.expose('getUserTrips', this.trip.getUserTrips);

// location
server.expose('getLocationsByUserId', this.location.getLocationsByUserId);
Expand Down Expand Up @@ -188,6 +190,7 @@ class Database {
server.expose('updateDocument', this.util.updateDocument);
server.expose('createView', this.util.createView);
server.expose('entryExist', this.util.entryExist);
server.expose('deleteDocument', this.util.deleteDocument);

// mail
server.expose('getRegistrationMail', this.mail.getRegistrationMail);
Expand Down
25 changes: 20 additions & 5 deletions src/trip/trip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import Util from './../util/util';

export default
class Trip {
private util:any;
private TYPE:string = 'trip';

constructor(private db:any, private LISTS:any) {
this.util = new Util(db);
}

/**
Expand Down Expand Up @@ -50,8 +56,8 @@ class Trip {
* @param trip:Trip
* @param callback
*/
updateTrip = (tripId:string, trip, callback) => {
this.db.merge(tripId, trip, callback);
updateTrip = (tripId:string, trip) => {
return this.util.updateDocument(tripId, trip, this.TYPE);
};

/**
Expand All @@ -66,12 +72,21 @@ class Trip {


/**
* Delete a particular trip by id.
* Delete a particular trip by id and returns a Promise
*
* @param tripId:string
* @param callback
*/
deleteTripById = (tripId:string, callback) => {
this.db.remove(tripId, callback);
deleteTripById = (tripId:string) => {
return this.util.removeDocument(tripId, this.TYPE);
};

/**
* Get all trips for this user id.
* @param userid
* @param callback
*/
getUserTrips = (userid:string, callback) => {
this.db.list(this.LISTS.LIST_TRIP_USER, {key: userid}, callback);
}
}
7 changes: 7 additions & 0 deletions src/util/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ var designTrip = {
emit(doc._id, doc);
}
}
},
tripByUserId: {
"map": function (doc) {
if (doc.type == 'trip') {
emit(doc.userid, doc);
}
}
}
},
lists: {
Expand Down
65 changes: 55 additions & 10 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,61 @@ class Util {
}

/**
* Update one or more field of a document and returns a promise
* Update one or more field(s) of a document and returns a promise.
* The provided type ensures that the correct document is updated
* @param documentid
* @param object
* @param type
*/
updateDocument = (documentid:string, object:any) => {
updateDocument = (documentid:string, object:any, type:string) => {
return new Promise((resolve, reject) => {
this.db.merge(documentid, object, (err, result) => {

this.db.get(documentid, (err, res) => {

if (err) {
return reject(err);
}
return resolve(result);
})

if (res.type !== type) {
return reject(this.boom.notAcceptable())
}

this.db.merge(documentid, object, (err, result) => {
if (err) {
return reject(err);
}
return resolve(result);
})
});
});
};


/**
* Deletes a document in the database. Makes sure, that the document with the right type is deleted
* @param documentid
* @param type
*/
deleteDocument = (documentid:string, type:string) => {
return new Promise((resolve, reject) => {

this.db.get(documentid, (err, res) => {

if (err) {
return reject(err);
}

if (res.type !== type) {
return reject(this.boom.notAcceptable())
}

this.db.remove(documentid, (err, result) => {
if (err) {
return reject(err);
}
return resolve(result);
})
});
});
};

Expand All @@ -48,14 +91,16 @@ class Util {
if (err) {
return callback(err)
}
var toUpdate = {};
var fieldValue = result.field;

// if field is not present create a new one
if (!fieldValue) {
return callback(this.boom.create('field in document not found', 404))
toUpdate[field] = valueToAppend;
} else {
toUpdate[field] = fieldValue.concat(valueToAppend);
}

var toUpdate = {};
toUpdate[field] = fieldValue.concat(valueToAppend);

this.db.merge(documentid, toUpdate, callback);
});
};
Expand Down Expand Up @@ -84,7 +129,7 @@ class Util {
return new Promise((resolve, reject) => {
// check if the document exist (or attachment), by sending a lightweight HEAD request
this.db.query(options, (err, data, response) => {
if (response != 200) {
if (response !== 200) {
return reject(this.boom.create(response, 'entry in database was not found'));
}
if (err) {
Expand Down

0 comments on commit 7cffe5e

Please sign in to comment.