-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- uuid 생성을 위한 패키지 설치 - eslint 백엔드 설정 수정 - 채널 생성 api 구현 (router, controller, service, repository) - guest 추가 api 구현 - postman 테스트
- Loading branch information
Showing
9 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.