mirror of
https://github.com/detleph/server.git
synced 2026-09-04 23:45:00 +02:00
Added Event Endpoints; Dotenv; npm Scripts
+ GET /events/ + POST /events/
This commit is contained in:
@@ -1 +1,13 @@
|
||||
# server
|
||||
|
||||
## SETUP
|
||||
|
||||
Before Runningthis on you local machine some things have to be setup
|
||||
|
||||
1. Run npm install
|
||||
2. Add .env file with following variables:
|
||||
|
||||
```
|
||||
DATABASE_URL: ADDRESS TO YOUR DATABASE
|
||||
PORT: PORT WHERE THE API SHOULD RUN
|
||||
```
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
// ... you will write your Prisma Client queries here
|
||||
console.log("Hello world!!");
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
throw e;
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Generated
+5
@@ -245,6 +245,11 @@
|
||||
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
|
||||
"dev": true
|
||||
},
|
||||
"dotenv": {
|
||||
"version": "10.0.0",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz",
|
||||
"integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q=="
|
||||
},
|
||||
"ee-first": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
|
||||
|
||||
+4
-1
@@ -4,7 +4,9 @@
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "ts-node src/app.ts",
|
||||
"dev": "nodemon src/app.ts"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -18,6 +20,7 @@
|
||||
"homepage": "https://github.com/detleph/server#readme",
|
||||
"dependencies": {
|
||||
"@prisma/client": "^3.2.1",
|
||||
"dotenv": "^10.0.0",
|
||||
"express": "^4.17.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../lib/prisma";
|
||||
|
||||
const getAllEvents = async (req: Request, res: Response) => {
|
||||
const events = await prisma.event.findMany({
|
||||
select: {
|
||||
name: true,
|
||||
date: true,
|
||||
pid: true,
|
||||
id: false,
|
||||
},
|
||||
});
|
||||
if (events.length > 0) res.status(200).send(events);
|
||||
else res.status(204).send();
|
||||
};
|
||||
|
||||
const addEvent = async (req: Request, res: Response) => {
|
||||
// TODO: Add checks if user has admin permissions
|
||||
|
||||
if (req.body.name == null || undefined || req.body.date == null || undefined) {
|
||||
res.status(400).send("Malformed request");
|
||||
return;
|
||||
}
|
||||
|
||||
const event = await prisma.event.create({
|
||||
data: {
|
||||
name: req.body.name,
|
||||
date: req.body.date,
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
date: true,
|
||||
pid: true,
|
||||
id: false,
|
||||
},
|
||||
});
|
||||
res.status(201).send(event);
|
||||
};
|
||||
|
||||
export { getAllEvents, addEvent };
|
||||
@@ -0,0 +1,9 @@
|
||||
import Express from "express";
|
||||
import { addEvent, getAllEvents } from "../Controllers/event.controller";
|
||||
const router = Express.Router();
|
||||
|
||||
router.get("/", getAllEvents);
|
||||
|
||||
router.post("/", addEvent);
|
||||
|
||||
export default router;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import express from "express";
|
||||
import prisma from "./lib/prisma";
|
||||
import eventRouter from "./Routes/event.routes";
|
||||
|
||||
require("dotenv").config(); // Load dotenv config
|
||||
|
||||
const app = express();
|
||||
|
||||
async function main() {
|
||||
// Todo: Everything
|
||||
|
||||
// Bodyparser and urlencoded to parse post request bodies
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
|
||||
app.use("/api/events", eventRouter);
|
||||
|
||||
app.listen(process.env.PORT, () => {
|
||||
console.log(`Listening on Port: ${process.env.PORT}`);
|
||||
});
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
throw e;
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
// See here: https://github.com/prisma/prisma-client-js/issues/228#issuecomment-618433162
|
||||
let prisma: PrismaClient;
|
||||
|
||||
prisma = new PrismaClient();
|
||||
|
||||
export default prisma;
|
||||
Reference in New Issue
Block a user