Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0853a0b

Browse files
Set up server with DB cofiguration
- set up prisma configuration - set up getAll and getOne routes
1 parent 660e935 commit 0853a0b

File tree

11 files changed

+677
-15
lines changed

11 files changed

+677
-15
lines changed

‎server/app.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,54 @@ const express = require("express");
22
const app = express();
33
const router = express.Router();
44
const cors = require("cors");
5-
const users = require("./server/users.json");
5+
// const users = require("./server/users.json");
66
const dotenv = require("dotenv");
7+
const HTTP_STATUS = require("./constants/httpStatus");
8+
const prisma = require("./config/database");
79
dotenv.config();
810
app.use(cors());
911

10-
router.get("/users/all", (req, res) => {
12+
router.get("/users/all", async(req, res) => {
1113
try {
1214
console.log(`${new Date().toISOString()} - All users request hit!`);
13-
res.set("Cache-Control", "public, max-age=31557600");
14-
return res.status(200).send({
15+
16+
const users = await prisma.user.findMany({});
17+
return res.status(HTTP_STATUS.OK).send({
1518
success: true,
1619
message: "Successfully received all users",
1720
data: users,
1821
});
1922
} catch (error) {
2023
console.log(error);
21-
return res.status(500).send({
24+
return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).send({
2225
success: false,
2326
message: "Internal server error",
2427
});
2528
}
2629
});
2730

28-
router.get(`/user/:id`, (req, res) => {
31+
router.get(`/user/:id`, async(req, res) => {
2932
try {
3033
console.log(`${new Date().toISOString()} - Single user request hit!`);
3134
const { id } = req.params;
3235

33-
res.set("Cache-Control", "public, max-age=31557600");
34-
const result = users.filter((element) => element.id === Number(id));
36+
const result = await prisma.user.findFirst({ where: { id: Number(id) } });
3537

36-
if (result.length===1) {
37-
return res.status(200).send({
38+
if (result) {
39+
return res.status(HTTP_STATUS.OK).send({
3840
success: true,
3941
message: `Successfully received user with id: ${id}`,
40-
data: result[0],
42+
data: result,
4143
});
4244
}
43-
return res.status(404).send({
45+
return res.status(HTTP_STATUS.NOT_FOUND).send({
4446
success: false,
4547
message: "Could not find user",
4648
data: null,
4749
});
4850
} catch (error) {
4951
console.log(error);
50-
return res.status(500).send({
52+
return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).send({
5153
success: false,
5254
message: "Internal server error",
5355
});

‎server/config/database.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { PrismaClient } = require("@prisma/client");
2+
3+
const prisma = new PrismaClient();
4+
5+
module.exports = prisma;

‎server/constants/httpStatus.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const HTTP_STATUS = {
2+
CONTINUE: 100,
3+
SWITCHING_PROTOCOLS: 101,
4+
PROCESSING: 102,
5+
EARLY_HINTS: 103,
6+
OK: 200,
7+
CREATED: 201,
8+
ACCEPTED: 202,
9+
NON_AUTHORITATIVE_INFORMATION: 203,
10+
NO_CONTENT: 204,
11+
RESET_CONTENT: 205,
12+
PARTIAL_CONTENT: 206,
13+
MULTI_STATUS: 207,
14+
ALREADY_REPORTED: 208,
15+
IM_USED: 226,
16+
MULTIPLE_CHOICES: 300,
17+
MOVED_PERMANENTLY: 301,
18+
FOUND: 302,
19+
SEE_OTHER: 303,
20+
NOT_MODIFIED: 304,
21+
USE_PROXY: 305,
22+
RESERVED: 306,
23+
TEMPORARY_REDIRECT: 307,
24+
PERMANENTLY_REDIRECT: 308,
25+
BAD_REQUEST: 400,
26+
UNAUTHORIZED: 401,
27+
PAYMENT_REQUIRED: 402,
28+
FORBIDDEN: 403,
29+
NOT_FOUND: 404,
30+
METHOD_NOT_ALLOWED: 405,
31+
NOT_ACCEPTABLE: 406,
32+
PROXY_AUTHENTICATION_REQUIRED: 407,
33+
REQUEST_TIMEOUT: 408,
34+
CONFLICT: 409,
35+
GONE: 410,
36+
LENGTH_REQUIRED: 411,
37+
PRECONDITION_FAILED: 412,
38+
REQUEST_ENTITY_TOO_LARGE: 413,
39+
REQUEST_URI_TOO_LONG: 414,
40+
UNSUPPORTED_MEDIA_TYPE: 415,
41+
REQUESTED_RANGE_NOT_SATISFIABLE: 416,
42+
EXPECTATION_FAILED: 417,
43+
I_AM_A_TEAPOT: 418,
44+
MISDIRECTED_REQUEST: 421,
45+
UNPROCESSABLE_ENTITY: 422,
46+
LOCKED: 423,
47+
FAILED_DEPENDENCY: 424,
48+
TOO_EARLY: 425,
49+
UPGRADE_REQUIRED: 426,
50+
PRECONDITION_REQUIRED: 428,
51+
TOO_MANY_REQUESTS: 429,
52+
REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
53+
UNAVAILABLE_FOR_LEGAL_REASONS: 451,
54+
INTERNAL_SERVER_ERROR: 500,
55+
NOT_IMPLEMENTED: 501,
56+
BAD_GATEWAY: 502,
57+
SERVICE_UNAVAILABLE: 503,
58+
GATEWAY_TIMEOUT: 504,
59+
VERSION_NOT_SUPPORTED: 505,
60+
VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL: 506,
61+
INSUFFICIENT_STORAGE: 507,
62+
LOOP_DETECTED: 508,
63+
NOT_EXTENDED: 510,
64+
NETWORK_AUTHENTICATION_REQUIRED: 511,
65+
};
66+
67+
module.exports = HTTP_STATUS;

‎server/package-lock.json

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎server/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44
"description": "",
55
"main": "app.js",
66
"dependencies": {
7+
"@prisma/client": "^5.14.0",
78
"cors": "^2.8.5",
89
"dotenv": "^16.4.5",
910
"express": "^4.18.2",
1011
"nodemon": "^2.0.20"
1112
},
13+
"prisma": {
14+
"seed": "node ./prisma/seed.js"
15+
},
1216
"scripts": {
1317
"dev": "nodemon app.js",
1418
"test": "echo \"Error: no test specified\" && exit 1"
1519
},
1620
"author": "",
17-
"license": "ISC"
18-
}
21+
"license": "ISC",
22+
"devDependencies": {
23+
"prisma": "^5.14.0"
24+
}
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- CreateEnum
2+
CREATE TYPE "Role" AS ENUM ('MALE', 'FEMALE', 'Others');
3+
4+
-- CreateTable
5+
CREATE TABLE "User" (
6+
"id" SERIAL NOT NULL,
7+
"first_name" VARCHAR(255) NOT NULL,
8+
"last_name" VARCHAR(255) NOT NULL,
9+
"email" VARCHAR(255) NOT NULL,
10+
"gender" "Role" NOT NULL,
11+
"phone" VARCHAR(100) NOT NULL,
12+
13+
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
14+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the `User` table. If the table is not empty, all the data it contains will be lost.
5+
6+
*/
7+
-- DropTable
8+
DROP TABLE "User";
9+
10+
-- CreateTable
11+
CREATE TABLE "user" (
12+
"id" SERIAL NOT NULL,
13+
"first_name" VARCHAR(255) NOT NULL,
14+
"last_name" VARCHAR(255) NOT NULL,
15+
"email" VARCHAR(255) NOT NULL,
16+
"gender" "Role" NOT NULL,
17+
"phone" VARCHAR(100) NOT NULL,
18+
19+
CONSTRAINT "user_pkey" PRIMARY KEY ("id")
20+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Warnings:
3+
4+
- Changed the type of `gender` on the `user` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
5+
6+
*/
7+
-- CreateEnum
8+
CREATE TYPE "Gender" AS ENUM ('MALE', 'FEMALE', 'OTHER');
9+
10+
-- AlterTable
11+
ALTER TABLE "user" DROP COLUMN "gender",
12+
ADD COLUMN "gender" "Gender" NOT NULL;
13+
14+
-- DropEnum
15+
DROP TYPE "Role";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "postgresql"

‎server/prisma/schema.prisma

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
generator client {
5+
provider = "prisma-client-js"
6+
}
7+
8+
datasource db {
9+
provider = "postgresql"
10+
url = env("DATABASE_URL")
11+
}
12+
13+
model User {
14+
id Int @id @default(autoincrement())
15+
first_name String @db.VarChar(255)
16+
last_name String @db.VarChar(255)
17+
email String @db.VarChar(255)
18+
gender Gender
19+
phone String @db.VarChar(100)
20+
21+
@@map("user")
22+
}
23+
24+
enum Gender {
25+
MALE
26+
FEMALE
27+
OTHER
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /