Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE][Feat] : #49 : 경로방 생성 api #148

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"pg": "^8.13.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"uuid": "^11.0.3",
"ws": "^8.11.0"
}
}
21 changes: 21 additions & 0 deletions backend/src/controllers/channelController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createChannelService } from '../services/channelService.js';

export const createChannelController = async (req, res) => {
try {
const { name, host_id, guests } = req.body;

const channel = await createChannelService(name, host_id, guests);

return res.status(201).json({
success: true,
message: 'Channel created successfully',
data: channel,
});
} catch (err) {
console.error(err);
return res.status(500).json({
success: false,
message: 'Server error',
});
}
};
2 changes: 2 additions & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { pool } from './db/db.js';
import { PORT } from './constants/constants.js';
import { initializeWebSocketServer } from './websocketServer.js';
import { authRouter } from './routes/authRouter.js';
import { channelRouter } from './routes/channelRouter.js';

const app = express();
app.use(express.json());

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));

app.use('/api/auth', authRouter);
app.use('/api/channel', channelRouter);

// TODO: 데이터베이스에서 데이터 가져오기 예시
app.get('/guests', async (req, res) => {
Expand Down
15 changes: 15 additions & 0 deletions backend/src/repositories/channelRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { v4 as uuidv4 } from 'uuid';
import { pool } from '../db/db.js';

export const createChannelInDB = async (name, host_id) => {
const id = uuidv4();
const query = `
INSERT INTO "main"."channel" (id, name, host_id)
VALUES ($1, $2, $3)
RETURNING *;
`;
const values = [id, name, host_id];
const result = await pool.query(query, values);

return result.rows[0];
};
32 changes: 32 additions & 0 deletions backend/src/repositories/guestRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { v4 as uuidv4 } from 'uuid';
import { pool } from '../db/db.js';

export const addGuestInDB = async (
channel_id,
name,
start_location,
end_location,
path,
marker_style,
host_id,
) => {
const guest_id = uuidv4();
const query = `
INSERT INTO "main"."guest" (id, channel_id, name, start_location, end_location, path, marker_style, host_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;
`;
const values = [
guest_id,
channel_id,
name,
start_location,
end_location,
path,
marker_style,
host_id,
];
const result = await pool.query(query, values);

return result.rows[0];
};
16 changes: 16 additions & 0 deletions backend/src/routes/channelRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import express from 'express';
import { body } from 'express-validator';
import { createChannelController } from '../controllers/channelController.js';
import { validationMiddleware } from '../middleware/validationMiddleware.js';

export const channelRouter = express.Router();

channelRouter.post(
'/',
[
body('name').notEmpty().withMessage('Name is required'),
body('host_id').notEmpty().withMessage('Host ID is required'),
],
validationMiddleware,
createChannelController,
);
25 changes: 25 additions & 0 deletions backend/src/services/channelService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createChannelInDB } from '../repositories/channelRepository.js';
import { addGuestInDB } from '../repositories/guestRepository.js';

export const createChannelService = async (name, host_id, guests) => {
// 채널 생성
const channel = await createChannelInDB(name, host_id);

// 게스트 추가
const guestPromises = guests.map(guest => {
const { name, start_location, end_location, path, marker_style } = guest;
return addGuestInDB(
channel.id,
name,
start_location,
end_location,
path,
marker_style,
host_id,
);
});

await Promise.all(guestPromises);

return channel;
};
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export default [
'no-console': 'off',
'consistent-return': 'off',
'import/extensions': 'off',
camelcase: 'off',
'no-shadow': 'off',
},
},

Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading