Skip to content

Commit

Permalink
[BE][Feat] #45 : 채널 id로 채널 정보, guest 정보 get API 구현
Browse files Browse the repository at this point in the history
- 채널 id로 채널 정보, guest 정보 get API에 대한 swagger 문서 추가
- 채널 id로 채널 정보, guest 정보 get API 구현
  • Loading branch information
happyhyep committed Nov 12, 2024
1 parent f1ef1c9 commit 4d20551
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 7 deletions.
25 changes: 24 additions & 1 deletion backend/src/controllers/channelController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { addGuestService, createChannelService } from '../services/channelService.js';
import {
addGuestService,
createChannelService,
getChannelByIdService,
} from '../services/channelService.js';

export const createChannelController = async (req, res) => {
try {
Expand Down Expand Up @@ -46,3 +50,22 @@ export const addGuestController = async (req, res) => {
});
}
};

export const getChannelInfoController = async (req, res) => {
const { id } = req.params;

try {
const channel = await getChannelByIdService(id);
if (!channel) {
return res.status(404).json({ success: false, message: 'Channel not found' });
}
return res.status(200).json({
success: true,
message: 'Get channel successfully',
data: channel,
});
} catch (err) {
console.error(err);
return res.status(500).json({ success: false, message: 'Server error' });
}
};
43 changes: 41 additions & 2 deletions backend/src/repositories/channelRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export const createChannelInDB = async (name, host_id) => {
return result.rows[0];
};

export const getChannelById = async channelId => {
export const getChannelInfoByIdInDB = async channelId => {
const query = `
SELECT *
FROM "main"."channel"
FROM "main"."channel"
WHERE id = $1;
`;
const values = [channelId];
Expand All @@ -29,3 +29,42 @@ export const getChannelById = async channelId => {

return result.rows[0];
};

export const getChannelWithGuestsByIdFromDB = async id => {
try {
const channelQuery = 'SELECT * FROM "main"."channel" WHERE id = $1';
const guestQuery = 'SELECT * FROM "main"."guest" WHERE channel_id = $1';

const channelResult = await pool.query(channelQuery, [id]);
if (channelResult.rows.length === 0) {
return null;
}

const channel = channelResult.rows[0];

const guestResult = await pool.query(guestQuery, [id]);

const guests = guestResult.rows;

return {
id: channel.id,
name: channel.name,
host_id: channel.host_id,
guests: guests.map(guest => ({
id: guest.id,
name: guest.name,
start_location: {
lat: guest.start_location.lat,
lng: guest.start_location.lng,
},
end_location: {
lat: guest.end_location.lat,
lng: guest.end_location.lng,
},
})),
};
} catch (error) {
console.error('Database error:', error);
throw error;
}
};
42 changes: 40 additions & 2 deletions backend/src/routes/channelRouter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import express from 'express';
import { body } from 'express-validator';
import { addGuestController, createChannelController } from '../controllers/channelController.js';
import { body, param } from 'express-validator';
import {
addGuestController,
createChannelController,
getChannelInfoController,
} from '../controllers/channelController.js';
import { validationMiddleware } from '../middleware/validationMiddleware.js';

export const channelRouter = express.Router();
Expand Down Expand Up @@ -133,3 +137,37 @@ channelRouter.post(
validationMiddleware,
addGuestController,
);

/**
* @swagger
* /channels/{id}:
* get:
* summary: 'Get channel information'
* description: 'Retrieve information about a specific channel by ID.'
* operationId: 'getChannelInfo'
* parameters:
* - in: path
* name: id
* required: true
* description: 'ID of the channel to retrieve'
* schema:
* type: string
* example: '123e4567-e89b-12d3-a456-426614174000'
* responses:
* 200:
* description: 'Channel information retrieved successfully'
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ChannelInfo'
* 404:
* description: 'Channel not found'
* 500:
* description: 'Server error'
*/
channelRouter.get(
'/:id',
[param('id').notEmpty().withMessage('Channel ID is required')],
validationMiddleware,
getChannelInfoController,
);
17 changes: 15 additions & 2 deletions backend/src/services/channelService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createChannelInDB, getChannelById } from '../repositories/channelRepository.js';
import {
createChannelInDB,
getChannelInfoByIdInDB,
getChannelWithGuestsByIdFromDB,
} from '../repositories/channelRepository.js';
import { addGuestToChannel } from '../repositories/guestRepository.js';

export const createChannelService = async (name, host_id, guests) => {
Expand All @@ -23,7 +27,7 @@ export const createChannelService = async (name, host_id, guests) => {
};

export const addGuestService = async (channelId, guests) => {
const channel = await getChannelById(channelId);
const channel = await getChannelInfoByIdInDB(channelId);
if (!channel) return null;

const guestPromises = guests.map(guest => {
Expand All @@ -43,3 +47,12 @@ export const addGuestService = async (channelId, guests) => {

return channel;
};

export const getChannelByIdService = async id => {
try {
return await getChannelWithGuestsByIdFromDB(id);
} catch (error) {
console.error('Error fetching channel:', error);
throw error;
}
};
100 changes: 100 additions & 0 deletions backend/swaggerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,66 @@ const swaggerDefinition = {
},
},
},
ChannelResponse: {
type: 'object',
properties: {
id: {
type: 'string',
description: '채널 ID (UUID 형태)',
},
name: {
type: 'string',
description: '채널 이름',
},
host_id: {
type: 'string',
description: '채널의 호스트 ID',
},
guests: {
type: 'array',
items: {
type: 'object',
properties: {
id: {
type: 'string',
description: '게스트 ID (UUID 형태)',
},
name: {
type: 'string',
description: '게스트 이름',
},
start_location: {
type: 'object',
properties: {
lat: {
type: 'number',
description: '출발지 마커의 위도',
},
lng: {
type: 'number',
description: '출발지 마커의 경도',
},
},
},
end_location: {
type: 'object',
properties: {
lat: {
type: 'number',
description: '도착지 마커의 위도',
},
lng: {
type: 'number',
description: '도착지 마커의 경도',
},
},
},
},
},
description: '해당 채널의 게스트 목록',
},
},
},
},
},
paths: {
Expand Down Expand Up @@ -366,6 +426,46 @@ const swaggerDefinition = {
},
},
},
'/channel/{id}': {
get: {
summary: '채널 정보 조회 API',
description:
'채널의 ID를 통해 해당 채널에 속한 게스트들의 정보를 포함한 채널 정보를 조회합니다.',
operationId: 'getChannelInfo',
parameters: [
{
name: 'id',
in: 'path',
description: '채널의 고유 ID (UUID 형식)',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: '채널 정보 조회 성공',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/ChannelResponse',
},
},
},
},
400: {
description: '잘못된 요청, 잘못된 채널 ID 형식',
},
404: {
description: '채널을 찾을 수 없음',
},
500: {
description: '서버 에러',
},
},
},
},
},
};

Expand Down

0 comments on commit 4d20551

Please sign in to comment.