Skip to content

Commit

Permalink
[BE][Feat] #46 : guest id로 guest 정보 get API 구현
Browse files Browse the repository at this point in the history
- guest id로 guest 정보 get API에 대한 swagger 문서 추가
- guest id로 guest 정보 get API 구현
- swagger 문서 세팅 수정
  • Loading branch information
happyhyep committed Nov 13, 2024
1 parent 5f37574 commit 5d1ec49
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 268 deletions.
19 changes: 19 additions & 0 deletions backend/src/controllers/channelController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
addGuestService,
createChannelService,
getChannelByIdService,
getChannelGuestInfoService,
} from '../services/channelService.js';

export const createChannelController = async (req, res) => {
Expand Down Expand Up @@ -69,3 +70,21 @@ export const getChannelInfoController = async (req, res) => {
return res.status(500).json({ success: false, message: 'Server error' });
}
};

export const getChannelGuestInfoController = async (req, res) => {
const { channelId, guestId } = req.params;
try {
const result = await getChannelGuestInfoService(channelId, guestId);
if (result) {
res.status(200).json({
success: true,
message: 'Get guest data successfully',
data: result,
});
} else {
res.status(404).json({ message: 'Channel or guest not found' });
}
} catch (error) {
res.status(500).json({ message: 'Server error', error });
}
};
53 changes: 53 additions & 0 deletions backend/src/repositories/channelRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,63 @@ export const getChannelWithGuestsByIdFromDB = async id => {
lat: guest.end_location.lat,
lng: guest.end_location.lng,
},
path: guest.path,
marker_style: guest.marker_style,
})),
};
} catch (error) {
console.error('Database error:', error);
throw error;
}
};

export const getGuestByChannelAndGuestIdFromDB = async (channelId, guestId) => {
try {
const channelQuery = `
SELECT *
FROM "main"."channel"
WHERE id = $1;
`;
const channelResult = await pool.query(channelQuery, [channelId]);
if (channelResult.rows.length === 0) {
return null;
}

const guestQuery = `
SELECT *
FROM "main"."guest"
WHERE channel_id = $1
AND id = $2;
`;
const guestResult = await pool.query(guestQuery, [channelId, guestId]);
if (guestResult.rows.length === 0) {
return null;
}

const channel = channelResult.rows[0];
const guest = guestResult.rows[0];

return {
id: channel.id,
name: channel.name,
host_id: channel.host_id,
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,
},
path: guest.path,
marker_style: guest.marker_style,
},
};
} catch (error) {
console.error('Database error:', error);
throw error;
}
};
15 changes: 9 additions & 6 deletions backend/src/routes/authRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@ export const authRouter = express.Router();

/**
* @swagger
* /api/auth/login:
* /auth/login:
* post:
* summary: User login
* description: Logs in a user with their ID and password.
* summary: 사용자 로그인 API
* description: 사용자가 로그인할 수 있도록 ID와 비밀번호를 통해 인증 후 토큰을 반환합니다.
* requestBody:
* required: true
* description: 로그인을 위한 ID와 비밀번호를 포함한 요청 body
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/LoginRequest'
* responses:
* 200:
* description: Successfully logged in
* description: 로그인 성공, 토큰 및 사용자 정보 반환
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/LoginResponse'
* 400:
* description: Invalid input (e.g. invalid ID or password)
* description: 잘못된 요청, 필수 정보 누락 또는 형식 오류
* 401:
* description: 잘못된 ID나 비밀번호
* 500:
* description: Internal server error
* description: 서버 에러
*/
authRouter.post(
'/login',
Expand Down
221 changes: 98 additions & 123 deletions backend/src/routes/channelRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,34 @@ import { body, param } from 'express-validator';
import {
addGuestController,
createChannelController,
getChannelGuestInfoController,
getChannelInfoController,
} from '../controllers/channelController.js';
import { validationMiddleware } from '../middleware/validationMiddleware.js';

export const channelRouter = express.Router();

// 채널 생성 API 경로
/**
* @swagger
* /channels:
* post:
* summary: 'Create a new channel'
* description: 'Create a new channel with guests and their respective locations and paths.'
* operationId: 'createChannel'
* requestBody:
* description: 'Channel data to be created'
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/CreateChannelRequest'
* required: true
* responses:
* 201:
* description: 'Channel created successfully'
* paths:
* /channel:
* post:
* summary: '새로운 채널 생성 API'
* description: '채널 이름, 주인, 게스트 정보를 포함하여 채널을 생성합니다.'
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/CreateChannelResponse'
* 400:
* description: 'Invalid input data'
* 500:
* description: 'Server error'
* $ref: '#/components/schemas/CreateChannelRequest'
* responses:
* 201:
* description: '채널 생성 성공'
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/CreateChannelResponse'
*/
channelRouter.post(
'/',
Expand All @@ -45,91 +42,34 @@ channelRouter.post(
createChannelController,
);

// 게스트 추가 API 경로
/**
* @swagger
* /channels/{channelId}/guests:
* post:
* summary: 'Add guests to an existing channel'
* description: 'Add new guests to an existing channel by providing guest information.'
* operationId: 'addGuest'
* parameters:
* - in: path
* name: channelId
* required: true
* description: 'ID of the channel to which guests are being added'
* schema:
* type: string
* example: '123e4567-e89b-12d3-a456-426614174000'
* requestBody:
* description: 'Guest data to be added to the channel'
* content:
* application/json:
* paths:
* /channel/{channelId}/guests:
* post:
* summary: '게스트 추가 API'
* description: '특정 채널에 게스트를 추가합니다.'
* parameters:
* - name: 'channelId'
* in: 'path'
* required: true
* schema:
* type: object
* properties:
* guests:
* type: array
* items:
* type: object
* properties:
* name:
* type: string
* description: 'Guest\'s name'
* start_location:
* type: object
* properties:
* lat:
* type: number
* description: 'Latitude of the start location'
* lng:
* type: number
* description: 'Longitude of the start location'
* end_location:
* type: object
* properties:
* lat:
* type: number
* description: 'Latitude of the end location'
* lng:
* type: number
* description: 'Longitude of the end location'
* path:
* type: array
* items:
* type: object
* properties:
* lat:
* type: number
* description: 'Latitude of a waypoint'
* lng:
* type: number
* description: 'Longitude of a waypoint'
* marker_style:
* type: object
* properties:
* color:
* type: string
* description: 'Color for the guest marker'
* responses:
* 200:
* description: 'Guests added successfully'
* type: 'string'
* description: '채널의 고유 ID'
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* message:
* type: string
* example: 'Guests added successfully'
* 400:
* description: 'Invalid input data'
* 404:
* description: 'Channel not found'
* 500:
* description: 'Server error'
* $ref: '#/components/schemas/AddGuestRequest'
* responses:
* 200:
* description: '게스트 추가 성공'
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/AddGuestResponse'
*/
channelRouter.post(
'/:channelId/guests',
Expand All @@ -138,36 +78,71 @@ channelRouter.post(
addGuestController,
);

// 채널 정보 조회 API 경로
/**
* @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'
* paths:
* /channel/{id}:
* get:
* summary: '채널 정보 조회 API'
* description: '특정 채널의 정보를 조회합니다.'
* parameters:
* - name: 'id'
* in: 'path'
* required: true
* schema:
* type: 'string'
* description: '채널의 고유 ID'
* responses:
* 200:
* description: '채널 정보 조회 성공'
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ChannelResponse'
*/
channelRouter.get(
'/:id',
[param('id').notEmpty().withMessage('Channel ID is required')],
validationMiddleware,
getChannelInfoController,
);

// 게스트 정보 조회 API 경로
/**
* @swagger
* paths:
* /channel/{channelId}/guest/{guestId}:
* get:
* summary: '게스트 정보 조회 API'
* description: '특정 채널 내의 게스트 정보를 조회합니다.'
* parameters:
* - name: 'channelId'
* in: 'path'
* required: true
* schema:
* type: 'string'
* description: '채널의 고유 ID'
* - name: 'guestId'
* in: 'path'
* required: true
* schema:
* type: 'string'
* description: '게스트의 고유 ID'
* responses:
* 200:
* description: '게스트 정보 조회 성공'
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/GuestResponse'
*/
channelRouter.get(
'/:channelId/guest/:guestId',
[
param('channelId').notEmpty().withMessage('Channel ID is required'),
param('guestId').notEmpty().withMessage('Guest ID is required'),
],
validationMiddleware,
getChannelGuestInfoController,
);
Loading

0 comments on commit 5d1ec49

Please sign in to comment.