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

Merge branch '2-backend-annonse-opprettelse' into 'master'

Resolve "Backend: Annonse opprettelse"

Closes #2

See merge request !2
parents 24fb5d46 8aef6f31
No related branches found
No related tags found
No related merge requests found
import { Response, Request } from "express";
import example from '../services/example';
export const example1 = async (_: Request, res: Response): Promise<void> => {
try {
//res.status(200).json({"hello":"example1","database": "a"});
res.json(await example.getTest());
} catch (error) {
res.status(400).send("Bad Request");
// res.status(500).send("Internal Server Error");
}
};
export const example2 = async (_: Request, res: Response): Promise<void> => {
try {
res.status(200).json({"hello":"example2"});
} catch (error) {
res.status(400).send("Bad Request");
// res.status(500).send("Internal Server Error");
}
};
import { Response, Request } from "express";
import query from '../services/db_query';
import express from 'express';
const router = express.Router();
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 {
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");
}
});
/* ============================= 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).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
export default router;
......@@ -16,28 +16,8 @@ app.use(bodyParser.json());
// Configuring json response
app.set("json spaces", 2);
app.use("/", routes);
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}!`)
});
import { example1, example2 } from '../controllers/example';
//import { example1, example2 } from '../controllers/example';
import postController from '../controllers/postController'
import express from 'express';
const router = express.Router();
router.get("/", (_, res) => {
res.send("Hello world!");
});
router.get('/test', (req, res) => {
console.log(req,res);
res.send("test!");
});
// Endpoints
router.use("/post", postController);
// routes go brrrrr!
router.get("/example1", example1);
router.get("/example1/example2", example2);
export default router;
import db from '../services/db';
async function getTest(){
const data = await db.query('SELECT * FROM test',"");
export default async function query(query: string, placeholder: {}){
const data = await db.query(query,placeholder);
const meta = {page: 1};
return {
......@@ -10,6 +10,3 @@ async function getTest(){
}
}
export default {
getTest
}
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