Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit 93c3dcf

Browse files
authored
Merge pull request #25 from IIM-Creative-Technology/feature/22-socket-integration
Socket integration
2 parents 1bc12ef + 0f27abe commit 93c3dcf

16 files changed

Lines changed: 465 additions & 39 deletions

File tree

apps/api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"argon2": "^0.28.5",
3535
"express-validator": "^6.14.0",
3636
"jsonwebtoken": "^8.5.1",
37-
"milliparsec": "^2.2.1"
37+
"milliparsec": "^2.2.1",
38+
"socket.io": "^4.5.1"
3839
}
3940
}

apps/api/src/auth/auth-routes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { App } from '@tinyhttp/app';
22
import type { PrismaClient } from '@prisma/client';
3+
import type { Server } from 'socket.io';
34
import { body } from 'express-validator';
45
import { getHandler } from './auth-handler';
56
// import { requireAuth } from '../util/middleware.js';
67

7-
const authRoutes = (app: App, prisma: PrismaClient) => {
8+
const authRoutes = (app: App, io: Server, prisma: PrismaClient) => {
89
const handler = getHandler(prisma);
910

1011
app.post(

apps/api/src/column/column-routes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { App } from '@tinyhttp/app';
22
import type { PrismaClient } from '@prisma/client';
33
import { body } from 'express-validator';
4+
import type { Server } from 'socket.io';
45
import { getHandler } from './column-handler';
5-
// import { requireAuth } from '../util/middleware.js';
66

7-
const projectRoutes = (app: App, prisma: PrismaClient) => {
7+
const projectRoutes = (app: App, io: Server, prisma: PrismaClient) => {
88
const handler = getHandler(prisma);
99

1010
app.get('/projects', handler.getColumns);

apps/api/src/create-api.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Server } from 'socket.io';
2+
import http from 'http';
13
import { App } from '@tinyhttp/app';
24
import { cors } from '@tinyhttp/cors';
35
import { jwt } from '@tinyhttp/jwt';
@@ -9,22 +11,24 @@ import { projectRoutes } from './project/project-routes';
911
import { authRoutes } from './auth/auth-routes';
1012
import { taskRoutes } from './task/task-routes';
1113

12-
1314
const createApi = (prisma: PrismaClient) => {
1415
const app = new App();
16+
const server = http.createServer();
17+
server.on('request', app.attach);
18+
const io = new Server(server);
1519

1620
app
1721
.use(jwt({ secret: process.env.JWT_SECRET ?? 'secret', algorithm: 'HS256' }))
1822
.use(cors())
1923
.use((req, res, next) => (req.headers['content-type'] === 'application/json' ? json()(req, res, next) : next()))
2024
.use(logger());
2125

22-
authRoutes(app, prisma);
23-
userRoutes(app, prisma);
24-
projectRoutes(app, prisma);
25-
taskRoutes(app, prisma);
26+
authRoutes(app, io, prisma);
27+
userRoutes(app, io, prisma);
28+
projectRoutes(app, io, prisma);
29+
taskRoutes(app, io, prisma);
2630

27-
return app;
31+
return server;
2832
};
2933

3034
export { createApi };

apps/api/src/project/project-handler.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ const getHandler = (prisma: PrismaClient) => {
3131
const newProject = await prisma.project.create({
3232
data: {
3333
name: req.body.name,
34+
columns: {
35+
create: [
36+
{
37+
name: 'To do',
38+
},
39+
{
40+
name: 'In progress',
41+
},
42+
{
43+
name: 'Done',
44+
},
45+
],
46+
},
3447
},
3548
});
3649
res.json(newProject);

apps/api/src/project/project-routes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { App } from '@tinyhttp/app';
22
import type { PrismaClient } from '@prisma/client';
33
import { body } from 'express-validator';
4+
import type { Server } from 'socket.io';
45
import { getHandler } from './project-handler';
5-
// import { requireAuth } from '../util/middleware.js';
66

7-
const projectRoutes = (app: App, prisma: PrismaClient) => {
7+
const projectRoutes = (app: App, io: Server, prisma: PrismaClient) => {
88
const handler = getHandler(prisma);
99

1010
app.get('/projects', handler.getProjects);

apps/api/src/task/task-routes.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { App } from '@tinyhttp/app';
22
import type { PrismaClient } from '@prisma/client';
33
import { body } from 'express-validator';
4+
import type { Server, Socket } from 'socket.io';
45
import { getHandler } from './task-handler';
5-
// import { requireAuth } from '../util/middleware.js';
6+
import { getSocketHandler } from './task-sockets';
67

7-
const taskRoutes = (app: App, prisma: PrismaClient) => {
8+
const taskRoutes = (app: App, io: Server, prisma: PrismaClient) => {
89
const handler = getHandler(prisma);
10+
const socketHandler = getSocketHandler(io, prisma);
911

1012
app.get('/tasks', handler.getTasks);
1113
app.get('/tasks/:id', handler.getTask);
@@ -24,6 +26,11 @@ const taskRoutes = (app: App, prisma: PrismaClient) => {
2426
handler.updateTask,
2527
);
2628
app.delete('/tasks/:id', handler.deleteTask);
29+
30+
io.on('connection', (socket: Socket) => {
31+
socket.on('tasks:move', socketHandler.moveTask);
32+
socket.on('task:edit', socketHandler.editTask);
33+
});
2734
};
2835

2936
export { taskRoutes };

apps/api/src/task/task-sockets.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-disable no-console */
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
import type { PrismaClient } from '@prisma/client';
4+
import type { Server } from 'socket.io';
5+
6+
const getSocketHandler = (io: Server, prisma: PrismaClient) => {
7+
const moveTask = async (payload?: any) => {
8+
try {
9+
await prisma.task.update({
10+
where: {
11+
id: payload?.task?.id,
12+
},
13+
data: {
14+
column: {
15+
connect: { id: payload?.columnId },
16+
},
17+
},
18+
});
19+
io.emit('tasks:move', payload);
20+
} catch (err) {
21+
console.error(err);
22+
}
23+
};
24+
25+
const editTask = async (payload?: any) => {
26+
try {
27+
await prisma.task.update({
28+
where: {
29+
id: payload?.task?.id,
30+
},
31+
data: {
32+
name: payload?.task?.name,
33+
content: payload?.task?.content,
34+
},
35+
});
36+
io.emit('tasks:edit', payload);
37+
} catch (err) {
38+
console.error(err);
39+
}
40+
};
41+
42+
return {
43+
moveTask,
44+
editTask,
45+
};
46+
};
47+
48+
export { getSocketHandler };

apps/api/src/user/user-routes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { App } from '@tinyhttp/app';
22
import type { PrismaClient } from '@prisma/client';
33
import { body } from 'express-validator';
4+
import { Server } from 'socket.io';
45
import { getHandler } from './user-handler';
56
import { requireAuth } from '../utils/middlewares/auth';
67

7-
const userRoutes = (app: App, prisma: PrismaClient) => {
8+
const userRoutes = (app: App, io: Server, prisma: PrismaClient) => {
89
const handler = getHandler(prisma);
910

1011
app.get('/users', requireAuth, handler.getUsers);

apps/web/components/AppColumn.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import Draggable from 'vuedraggable';
33
44
const props = defineProps({
5-
title: {
6-
type: String,
5+
data: {
6+
type: Object,
77
required: true,
88
},
99
});
@@ -19,7 +19,7 @@ const tasks = ref([
1919
<div class="flex">
2020
<div class="card bg-secondary-content mx-5 min-h-screen w-72">
2121
<h2 class="m-2">
22-
{{ props.title }}
22+
{{ props.data.name }}
2323
</h2>
2424
<Draggable
2525
v-model="tasks"
@@ -28,7 +28,7 @@ const tasks = ref([
2828
group="tasks"
2929
>
3030
<template #item="{element}">
31-
<AppTask :title="element.title" />
31+
<AppTask :data="element" />
3232
</template>
3333
</Draggable>
3434
</div>

0 commit comments

Comments
 (0)