Skip to content
Snippets Groups Projects
Commit 8aef6f31 authored by Jonny Ngo Luong's avatar Jonny Ngo Luong
Browse files

Issue: finish set up routes for post (#2)

parent 9f6a74cc
No related branches found
No related tags found
1 merge request!2Resolve "Backend: Annonse opprettelse"
......@@ -3,19 +3,75 @@ import query from '../services/db_query';
import express from 'express';
const router = express.Router();
router.route('/sendPost').get(async (request: Request, response: Response) => {
interface IPost {
title: string;
description: string;
timestamp: number;
owner: string;
category: string;
imageUrl: string;
}
/* ============================= CREATE ============================= */
// Create posts `/api/post/`
//'{"title":"test3","description":"test3","timestamp":123123,"owner":"test3","category":"test3","imageUrl":"test3"}'
router.route('/').post(async (request: Request, response: Response) => {
const {title, description, timestamp, owner, category, imageUrl} = request.body;
try {
response.status(200).send("a");
// response.status(200).json(await query("SELECT * FROM test;"));
const post: IPost = {
"title": title,
"description": description,
"timestamp": timestamp,
"owner": owner,
"category": category,
"imageUrl": imageUrl
};
if (Object.values(post).filter(p => p == undefined).length > 0) return response.status(500).send("Error");
const input = (`INSERT INTO post(title, description, timestamp, owner, category, imageUrl) VALUES (?,?,?,?,?,?)`)
return response.status(200).json(
await query(input,Object.values(post))
);
} catch (error) {
return response.status(400).send("Bad Request");
}
});
/* ============================= READ ============================= */
// Get all posts `/api/post/`
router.route('/').get(async (_: Request, response: Response) => {
try {
response.status(200).json(await query("SELECT * FROM post;",""));
} catch (error) {
response.status(400).send("Bad Request");
}
});
// Get post with id `/api/post/:id`
router.route('/:id').get(async (request: Request, response: Response) => {
const postId = request.params.id;
try {
response.status(200).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
/* ============================= UPDATE ============================= */
// Edit post with id `/api/post/:id`
router.route('/:id').put(async (request: Request, response: Response) => {
const postId = request.params.id;
try {
response.status(200).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
router.route('/editPost').get(async (request: Request, response: Response) => {
/* ============================= DELETE ============================= */
// Remove post with id `/api/post/:id`
router.route('/:id').delete(async (request: Request, response: Response) => {
const postId = request.params.id;
try {
response.status(200).send("a");
//response.status(200).json(await example.getTest());
response.status(200).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
......
......@@ -18,26 +18,6 @@ app.set("json spaces", 2);
app.use("/api", routes);
/*
// create the connection to database
const connection = mysql.createConnection({
host: 'mysql.stud.ntnu.no',
user: 'jonnynl_tdt4140',
password: 'jonny123',
database: 'jonnynl_tdt4140',
connectTimeout: 10000
});
// simple query
connection.query(
'SELECT * from test',
function(err , results, fields) {
console.log(results); // results contains rows returned by server
// console.log(fields); // fields contains extra meta data about results, if available
// console.log(err);
}
);
*/
app.listen(port, () => {
console.log(`Listening on port ${port}!`)
});
......@@ -4,10 +4,6 @@ import express from 'express';
const router = express.Router();
// routes go brrrrr!
//router.get("/example1", example1);
//router.get("/example1/example2", example2);
// Endpoints
router.use("/post", postController);
......
import db from '../services/db';
export default async function query(query: string){
const data = await db.query(query,"");
export default async function query(query: string, placeholder: {}){
const data = await db.query(query,placeholder);
const meta = {page: 1};
return {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment