user route for getting friendships
This commit is contained in:
@@ -43,4 +43,14 @@ export default class UserCtl {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async getFriends(id: string) {
|
||||
try {
|
||||
const result = await UserInstance.getFriends(id);
|
||||
if (!result) throw createError(404, "You have no friends");
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
server/db/sql/friendships.sql
Normal file
13
server/db/sql/friendships.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
SELECT
|
||||
recipin.cmp_userfriendships.id,
|
||||
recipin.cmp_userfriendships.datecreated,
|
||||
recipin.appusers.id,
|
||||
recipin.appusers.firstname,
|
||||
recipin.appusers.lastname,
|
||||
recipin.appusers.handle,
|
||||
recipin.appusers.email
|
||||
FROM recipin.cmp_userfriendships
|
||||
INNER JOIN recipin.appusers
|
||||
ON recipin.appusers.id = recipin.cmp_userfriendships.seconduserid
|
||||
WHERE firstuserid = $1
|
||||
AND cmp_userfriendships.active = true;
|
||||
@@ -1,4 +1,5 @@
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
import cors from 'cors';
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
@@ -9,7 +10,10 @@ const port = 8080;
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
|
||||
export const appRoot = path.resolve(__dirname);
|
||||
|
||||
(async function() {
|
||||
|
||||
const app = express();
|
||||
await loaders(app);
|
||||
app.listen(port, () => {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { IUser } from "../schemas";
|
||||
import fs from "fs";
|
||||
import pgPromise from "pg-promise";
|
||||
import pool from '../db';
|
||||
import now from "../util/now";
|
||||
import { appRoot } from "..";
|
||||
|
||||
const pgp = pgPromise({ capSQL: true });
|
||||
export class User {
|
||||
@@ -91,4 +93,15 @@ export class User {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async getFriends(id: string) {
|
||||
try {
|
||||
const sql = fs.readFileSync(appRoot + '/db/sql/friendships.sql').toString();
|
||||
const result = await pool.query(sql, [id]);
|
||||
if (result.rows.length) return result.rows;
|
||||
return null;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export const userRoute = (app: Express) => {
|
||||
res.status(200).send(data);
|
||||
})
|
||||
|
||||
// get, put by id
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
@@ -33,13 +34,21 @@ export const userRoute = (app: Express) => {
|
||||
}
|
||||
})
|
||||
|
||||
// create user
|
||||
router.post('/', async (req, res) => {
|
||||
const data = req.body;
|
||||
const response = userCtl.post(data);
|
||||
res.status(200).send(response);
|
||||
})
|
||||
|
||||
router.get('/hidden-thing', (req, res) => {
|
||||
res.send('does this route actually work?');
|
||||
// get friendships by requester ID
|
||||
router.get('/friends/:id', async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
const result = await userCtl.getFriends(id);
|
||||
res.status(200).send(result);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user