GET liked status

This commit is contained in:
Tom Russell 2019-01-22 17:02:03 +00:00
parent c067f87ad2
commit 4f4d81ce65
2 changed files with 66 additions and 24 deletions

View File

@ -83,6 +83,16 @@ function getBuildingById(id) {
});
}
function getBuildingLikeById(building_id, user_id) {
return db.one(
"SELECT true as like FROM building_user_likes WHERE building_id = $1 and user_id = $2 LIMIT 1",
[building_id, user_id]
).catch(function(error){
console.error(error);
return undefined;
});
}
function getBuildingUPRNsById(id) {
return db.any(
"SELECT uprn, parent_uprn FROM building_properties WHERE building_id = $1",
@ -278,5 +288,12 @@ function compare(old_obj, new_obj, whitelist){
return [forward_patch, reverse_patch]
}
export { queryBuildingsAtPoint, queryBuildingsByReference, getBuildingById,
getBuildingUPRNsById, saveBuilding, likeBuilding };
export {
queryBuildingsAtPoint,
queryBuildingsByReference,
getBuildingById,
getBuildingLikeById,
getBuildingUPRNsById,
saveBuilding,
likeBuilding
};

View File

@ -17,8 +17,15 @@ import pgConnect from 'connect-pg-simple';
import App from './frontend/app';
import db from './db';
import { authUser, createUser, getUserById, authAPIUser, getNewUserAPIKey } from './user';
import { queryBuildingsAtPoint, queryBuildingsByReference, getBuildingById,
getBuildingUPRNsById, saveBuilding, likeBuilding } from './building';
import {
queryBuildingsAtPoint,
queryBuildingsByReference,
getBuildingById,
getBuildingLikeById,
getBuildingUPRNsById,
saveBuilding,
likeBuilding
} from './building';
import tileserver from './tileserver';
import { parseBuildingURL } from './parse';
@ -238,27 +245,45 @@ server.get('/building/:building_id/uprns.json', function (req, res) {
})
})
// POST like building
server.post('/building/like/:building_id', function(req, res){
if (!req.session.user_id) {
res.send({error: 'Must be logged in'});
return
}
const { building_id } = req.params;
likeBuilding(building_id, req.session.user_id).then(building => {
if (building.error) {
// GET/POST like building
server.route('/building/:building_id/like.json')
.get(function(req, res){
if (!req.session.user_id) {
res.send({like: false}); // not logged in, so cannot have liked
return
}
const { building_id } = req.params;
getBuildingLikeById(building_id, req.session.user_id).then(like => {
if (typeof(like) === "undefined") {
res.send({like: false})
return
}
// any value returned means like
res.send({like: true})
}).catch(
() => res.send({error:'Database error'})
)
})
.post(function(req, res){
if (!req.session.user_id) {
res.send({error: 'Must be logged in'});
return
}
const { building_id } = req.params;
likeBuilding(building_id, req.session.user_id).then(building => {
if (building.error) {
res.send(building)
return
}
if (typeof(building) === "undefined") {
res.send({error:'Database error'})
return
}
res.send(building)
return
}
if (typeof(building) === "undefined") {
res.send({error:'Database error'})
return
}
res.send(building)
}).catch(
() => res.send({error:'Database error'})
)
});
}).catch(
() => res.send({error:'Database error'})
)
})
// POST new user
server.post('/users', function(req, res){