Skip to content

Commit 21e7b16

Browse files
committed
Fix major issues for employees and cafes in the frontend
1 parent 7563635 commit 21e7b16

File tree

28 files changed

+834
-179
lines changed

28 files changed

+834
-179
lines changed

backend/README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
2+
# Backend
33

44
## Installation
55

@@ -39,12 +39,61 @@ Use Prisma CLI to apply migrations and generate the Prisma client:
3939
```bash
4040
npx prisma migrate dev
4141
npx prisma generate
42+
```
43+
44+
### Seed the Cafes and Employees (add dummy data)
45+
46+
47+
While the docker container/db server is running run the following:
48+
```bash
49+
npx prisma db seed
4250

51+
or
52+
53+
pnpm run seed
54+
```
55+
56+
Then just to be sure run again...
57+
58+
```prisma
59+
npx prisma migrate dev
4360
```
4461

62+
Afterwards, seed data is in db already.
63+
64+
4565
## API
4666

47-
Default service api url
67+
68+
69+
### Schema (Entity Relationship)
70+
```prisma
71+
model Cafes {
72+
id Int @id @default(autoincrement())
73+
name String @unique
74+
createdAt DateTime @default(now())
75+
updatedAt DateTime @updatedAt
76+
description String
77+
logo String
78+
location String
79+
employees Employees[]
80+
}
81+
82+
model Employees {
83+
id Int @id @default(autoincrement())
84+
createdAt DateTime @default(now())
85+
updatedAt DateTime @updatedAt
86+
name String
87+
email_address String
88+
phone_number String
89+
days_worked Int
90+
cafe Cafes @relation(fields: [cafesId], references: [id])
91+
cafesId Int
92+
}
93+
94+
```
95+
96+
### Default service api url
4897
http://location:8888
4998

5099
> CRUD Operations:

backend/docker-compose.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ services:
77
ports:
88
- '3306:3306'
99
environment:
10-
MYSQL_ROOT_PASSWORD: password
11-
MYSQL_DATABASE: cedb
10+
- MYSQL_ROOT_PASSWORD=password
11+
- MYSQL_DATABASE=cedb
1212

1313

1414
backend:
@@ -20,5 +20,6 @@ services:
2020
volumes:
2121
- .:/app
2222
environment:
23-
DATABASE_URL: mysql://root:password@db:3306/cedb
23+
- DATABASE_URL=mysql://root:password@db:3306/cedb
24+
- NODE_DOCKER_PORT=8888
2425

backend/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
"test:e2e": "jest --config ./test/jest-e2e.json",
2121
"docker:build": "docker build -f Dockerfile -t app .",
2222
"docker:run": "docker run -it -p 80:80 app",
23-
"docker:prune": "docker system prune -a"
23+
"docker:prune": "docker system prune -a",
24+
"seed": "npx prisma db seed"
25+
},
26+
"prisma": {
27+
"seed": "ts-node prisma/seed.ts"
2428
},
2529
"dependencies": {
2630
"@nestjs/common": "^10.0.0",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- CreateTable
2+
CREATE TABLE `Cafes` (
3+
`id` INTEGER NOT NULL AUTO_INCREMENT,
4+
`name` VARCHAR(191) NOT NULL,
5+
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
6+
`updatedAt` DATETIME(3) NOT NULL,
7+
`description` VARCHAR(191) NOT NULL,
8+
`logo` VARCHAR(191) NOT NULL,
9+
`location` VARCHAR(191) NOT NULL,
10+
11+
UNIQUE INDEX `Cafes_name_key`(`name`),
12+
PRIMARY KEY (`id`)
13+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
14+
15+
-- CreateTable
16+
CREATE TABLE `Employees` (
17+
`id` INTEGER NOT NULL AUTO_INCREMENT,
18+
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
19+
`updatedAt` DATETIME(3) NOT NULL,
20+
`name` VARCHAR(191) NOT NULL,
21+
`cafesId` INTEGER NOT NULL,
22+
`days` INTEGER NOT NULL,
23+
`email` VARCHAR(191) NOT NULL,
24+
`phone` VARCHAR(191) NOT NULL,
25+
`gender` ENUM('M', 'F') NOT NULL,
26+
27+
PRIMARY KEY (`id`)
28+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
29+
30+
-- AddForeignKey
31+
ALTER TABLE `Employees` ADD CONSTRAINT `Employees_cafesId_fkey` FOREIGN KEY (`cafesId`) REFERENCES `Cafes`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to alter the column `gender` on the `Employees` table. The data in that column could be lost. The data in that column will be cast from `Enum(EnumId(0))` to `VarChar(191)`.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE `Employees` MODIFY `gender` VARCHAR(191) NOT NULL;

backend/prisma/schema.prisma

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ datasource db {
99
}
1010

1111
model Cafes {
12-
id Int @id @default(autoincrement())
13-
name String @unique
12+
id Int @id @default(autoincrement())
13+
name String @unique
14+
createdAt DateTime @default(now())
15+
updatedAt DateTime @updatedAt
1416
description String
1517
logo String
1618
location String
17-
employees Int
19+
employees Employees[]
1820
}
1921

2022
model Employees {
21-
id Int @id @default(autoincrement())
22-
name String
23-
email_address String
24-
phone_number String
25-
days_worked Int
26-
cafe String @unique
23+
id Int @id @default(autoincrement())
24+
createdAt DateTime @default(now())
25+
updatedAt DateTime @updatedAt
26+
name String
27+
cafesId Int
28+
days Int
29+
email String
30+
phone String
31+
gender String
32+
cafe Cafes @relation(fields: [cafesId], references: [id])
2733
}

backend/prisma/seed.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { PrismaClient } from '@prisma/client';
2+
const prisma = new PrismaClient();
3+
async function main() {
4+
console.log(`Start seeding ...`);
5+
6+
const caf1 = await prisma.cafes.create({
7+
data: {
8+
id: 1,
9+
name: 'Digong Cafe',
10+
description: 'Lorem ipsum dolor sit amet',
11+
location: 'Tampines',
12+
logo: 'https://gravatar.com/avatar/ef7b90c86c01dea8b4313dbb1cd0d015?s=400&d=robohash&r=x',
13+
},
14+
});
15+
const caf2 = await prisma.cafes.create({
16+
data: {
17+
id: 2,
18+
name: 'Farm Cafe',
19+
description: 'Lorem ipsum dolor sit amet',
20+
location: 'Jurong',
21+
logo: 'https://gravatar.com/avatar/24f7432224dd89ca643bec40d21861a7?s=400&d=robohash&r=x',
22+
},
23+
});
24+
25+
const caf3 = await prisma.cafes.create({
26+
data: {
27+
id: 3,
28+
name: 'Pogo Cafe',
29+
description: 'Lorem ipsum dolor sit amet',
30+
logo: 'https://gravatar.com/avatar/1db0c9ee9a6da87ee2416ea23dfab10d?s=400&d=robohash&r=x',
31+
location: 'Raffles',
32+
},
33+
});
34+
35+
const emp1 = await prisma.employees.create({
36+
data: {
37+
email: 'alice.guo@prisma.io',
38+
name: 'Alice Guo',
39+
phone: '+6588888888',
40+
days: 7,
41+
cafesId: 1,
42+
gender: 'F',
43+
},
44+
});
45+
const emp2 = await prisma.employees.create({
46+
data: {
47+
email: 'cass.ong@prisma.io',
48+
name: 'Cassandra Ong',
49+
phone: '+6588888888',
50+
days: 7,
51+
cafesId: 1,
52+
gender: 'F',
53+
},
54+
});
55+
56+
const emp3 = await prisma.employees.create({
57+
data: {
58+
email: 'sd@prisma.io',
59+
name: 'Bratanila SD',
60+
phone: '+6588888888',
61+
days: 365,
62+
cafesId: 3,
63+
gender: 'F',
64+
},
65+
});
66+
67+
const emp4 = await prisma.employees.create({
68+
data: {
69+
email: '1piece@prisma.io',
70+
name: 'Monkey D. Luffy',
71+
phone: '+6588888888',
72+
days: 7,
73+
cafesId: 2,
74+
gender: 'M',
75+
},
76+
});
77+
78+
console.log('Creates: ', { emp1, emp2, emp3, emp4, caf1, caf2, caf3 });
79+
}
80+
main()
81+
.then(async () => {
82+
await prisma.$disconnect();
83+
})
84+
.catch(async (e) => {
85+
console.error(e);
86+
await prisma.$disconnect();
87+
process.exit(1);
88+
});

backend/src/cafes/cafes.service.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ export class CafesService {
1111
}
1212

1313
async findAll() {
14-
return this.databaseService.cafes.findMany({});
14+
return this.databaseService.cafes.findMany({
15+
include: {
16+
employees: true,
17+
},
18+
});
1519
}
1620

1721
async findOne(id: number) {
1822
return this.databaseService.cafes.findUnique({
1923
where: {
2024
id,
2125
},
26+
include: {
27+
employees: true,
28+
},
2229
});
2330
}
2431

backend/src/employees/employees.service.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ export class EmployeesService {
1111
}
1212

1313
async findAll() {
14-
return this.databaseService.employees.findMany({});
14+
return this.databaseService.employees.findMany({
15+
include: {
16+
cafe: true,
17+
},
18+
});
1519
}
1620

1721
async findOne(id: number) {
1822
return this.databaseService.employees.findUnique({
1923
where: {
2024
id,
2125
},
26+
include: {
27+
cafe: true,
28+
},
2229
});
2330
}
2431

backend/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ async function bootstrap() {
1313
};
1414

1515
app.enableCors(options);
16-
await app.listen(8888);
16+
await app.listen(port);
1717
}
1818
bootstrap();

frontend/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
"lint": "eslint . --ext js,ts,tsx",
1212
"format": "prettier --write **/*.{js,ts,tsx} && eslint . --ext js,ts,tsx --fix",
1313
"preview": "vite preview",
14-
"prepare": "husky install",
15-
"docker:build": "docker build -f Dockerfile -t frontend .",
16-
"docker:run": "docker run -it -p 80:80 frontend"
14+
"postinstall": "bash postinstall.sh",
15+
"prepare": "husky install"
1716
},
1817
"dependencies": {
1918
"@emotion/babel-plugin": "^11.11.0",
@@ -30,6 +29,7 @@
3029
"clsx": "^2.0.0",
3130
"lodash": "^4.17.21",
3231
"lucide-react": "^0.279.0",
32+
"moment": "^2.30.1",
3333
"react": "^18.2.0",
3434
"react-dom": "^18.2.0",
3535
"react-error-boundary": "^4.0.11",

frontend/pnpm-lock.yaml

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

frontend/src/api/services/cafes/methods/methods.api.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import axiosClient from '@/api/clients/axios-client.ts';
22
import { ResponseDataType, CafeDataType, CafeDataMutationType } from '@/api/services';
3+
import { omit } from 'lodash';
34

45
const baseUrl = 'cafes';
56

67
const methodsApi = {
78
getList: (): Promise<ResponseDataType> => axiosClient.get(baseUrl),
9+
810
getDetail: (id: string): Promise<{ code: number; data: CafeDataType }> =>
911
axiosClient.get(`${baseUrl}/${id}`),
12+
1013
add: (body: CafeDataMutationType): Promise<{ code: number; data: CafeDataType }> =>
11-
axiosClient.post(baseUrl, { ...body, employees: parseInt(body.employees as string) }),
14+
axiosClient.post(baseUrl, { ...omit(body, 'employees') }),
15+
1216
update: (body: { id: string; data: CafeDataMutationType }): Promise<{ code: number; data: CafeDataType }> =>
13-
axiosClient.put(baseUrl, body),
17+
axiosClient.patch(baseUrl + `/${body.id}`, {
18+
...omit(body?.data, ['employees', 'id', 'updatedAt', 'createdAt']),
19+
}),
20+
1421
delete: (id: string): Promise<{ code: number; message: string }> => axiosClient.delete(baseUrl + `/${id}`),
1522
};
1623

0 commit comments

Comments
 (0)