70 lines
1.7 KiB
JavaScript
70 lines
1.7 KiB
JavaScript
var express = require('express')
|
|
var router = express.Router()
|
|
|
|
const DB = require("../mongoDB.js");
|
|
const Profile = require("../def/profile.js");
|
|
|
|
DB.getDB.then((DB)=>{
|
|
|
|
const getUserId = function(req){
|
|
const user_sid = req.cookies.user_sid || req.query.user_sid || req.body.user_sid;
|
|
return DB.ObjectID(user_sid);
|
|
}
|
|
|
|
router.get("/new", async (req, res) => {
|
|
let profile = {
|
|
userid: getUserId(req),
|
|
... req.query.content
|
|
};
|
|
let profileObj = new Profile(profile);
|
|
let r = await DB.newProfile(profileObj);
|
|
return res.json({
|
|
status: "ok",
|
|
... profileObj.toObj()
|
|
});
|
|
});
|
|
|
|
router.get("/groups", async (req, res) => {
|
|
let groups = await DB.getGroups();
|
|
return res.json({
|
|
status: "ok",
|
|
groups
|
|
});
|
|
});
|
|
|
|
router.post("/groups", async (req, res) => {
|
|
let profile = {
|
|
userid: getUserId(req),
|
|
isGroup: true,
|
|
... req.body
|
|
};
|
|
console.log("newGroup", profile)
|
|
let profileObj = new Profile(profile);
|
|
DB.newProfile(profileObj)
|
|
return res.json({
|
|
status: "ok",
|
|
... profileObj.toObj()
|
|
});
|
|
});
|
|
|
|
router.get("/groups/:id", async (req, res) => {
|
|
const groupid = req.params.id;
|
|
let groups = await DB.getGroup(groupid);
|
|
return res.json({
|
|
status: "ok",
|
|
groups
|
|
});
|
|
});
|
|
|
|
router.get("/:id", async (req, res) => {
|
|
let profileId = req.params.id;
|
|
let profile = await DB.getProfile(profileId);
|
|
return res.json({
|
|
status: "ok",
|
|
... profile
|
|
});
|
|
});
|
|
|
|
});
|
|
|
|
module.exports = router |