Skip to content

Commit

Permalink
Merge pull request #157 from boostcampwm-2024/feature/be/#43-getUserC…
Browse files Browse the repository at this point in the history
…hannel

[BE][Feat] #43 : user id로 해당 user가 생성한 channel 정보 가져오기 API 구현
  • Loading branch information
happyhyep authored Nov 13, 2024
2 parents 496fb13 + e4b016a commit c2ab9cd
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
10 changes: 7 additions & 3 deletions backend/src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ export const login = async (req, res) => {
try {
const token = await loginUser(id, password);
if (!token) {
return res.status(401).json({ message: 'Invalid ID or password' });
return res.status(401).json({ success: false, message: 'Invalid ID or password' });
}
return res.status(200).json({ token });
return res.status(201).json({
success: true,
message: 'Login successfully',
data: token,
});
} catch (error) {
console.error('Login error:', error);
return res.status(500).json({ message: 'Server error occurred' });
return res.status(500).json({ success: false, message: 'Server error occurred' });
}
};
25 changes: 23 additions & 2 deletions backend/src/controllers/channelController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createChannelService,
getChannelByIdService,
getChannelGuestInfoService,
getUserChannels,
} from '../services/channelService.js';

export const createChannelController = async (req, res) => {
Expand Down Expand Up @@ -82,9 +83,29 @@ export const getChannelGuestInfoController = async (req, res) => {
data: result,
});
} else {
res.status(404).json({ message: 'Channel or guest not found' });
res.status(404).json({ success: false, message: 'Channel or guest not found' });
}
} catch (error) {
res.status(500).json({ message: 'Server error', error });
console.error(error);
res.status(500).json({ success: false, message: 'Server error' });
}
};

export const getUserChannelsController = async (req, res) => {
const { userId } = req.params;

try {
const channels = await getUserChannels(userId);
if (!channels.length) {
return res.status(404).json({ success: false, message: 'No channels found for this user.' });
}
res.status(200).json({
success: true,
message: 'Get channels successfully',
data: channels,
});
} catch (error) {
console.error(error);
return res.status(500).json({ success: false, message: 'Server error' });
}
};
25 changes: 25 additions & 0 deletions backend/src/repositories/channelRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,28 @@ export const getGuestByChannelAndGuestIdFromDB = async (channelId, guestId) => {
throw error;
}
};

export const getChannelsByUserIdFromDB = async userId => {
try {
const query = `
SELECT id, name, generated_at
FROM "main"."channel"
WHERE host_id = $1;
`;
const values = [userId];
const result = await pool.query(query, values);

if (result.rows.length === 0) {
return [];
}

return result.rows.map(channel => ({
id: channel.id,
name: channel.name,
generated_at: channel.generated_at,
}));
} catch (error) {
console.error('Database error:', error);
throw error;
}
};
34 changes: 34 additions & 0 deletions backend/src/routes/channelRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createChannelController,
getChannelGuestInfoController,
getChannelInfoController,
getUserChannelsController,
} from '../controllers/channelController.js';
import { validationMiddleware } from '../middleware/validationMiddleware.js';

Expand Down Expand Up @@ -146,3 +147,36 @@ channelRouter.get(
validationMiddleware,
getChannelGuestInfoController,
);

/**
* @swagger
* paths:
* /channel/user/{userId}:
* get:
* summary: '사용자가 host인 채널 목록 반환 API'
* description: 'userId를 기준으로 해당 사용자가 host인 채널 목록을 반환합니다.'
* parameters:
* - in: path
* name: userId
* required: true
* description: '사용자의 ID'
* schema:
* type: string
* responses:
* 200:
* description: '채널 목록 반환 성공'
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/GetUserChannelsResponse'
* 404:
* description: '해당 사용자가 host인 채널이 없음'
* 500:
* description: '서버 오류'
*/
channelRouter.get(
'/user/:userId',
[param('userId').notEmpty().withMessage('User ID is required')],
validationMiddleware,
getUserChannelsController,
);
9 changes: 9 additions & 0 deletions backend/src/services/channelService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createChannelInDB,
getChannelInfoByIdInDB,
getChannelsByUserIdFromDB,
getChannelWithGuestsByIdFromDB,
getGuestByChannelAndGuestIdFromDB,
} from '../repositories/channelRepository.js';
Expand Down Expand Up @@ -66,3 +67,11 @@ export const getChannelGuestInfoService = async (channelId, guestId) => {
throw error;
}
};

export const getUserChannels = async userId => {
try {
return await getChannelsByUserIdFromDB(userId);
} catch (error) {
throw new Error('Failed to fetch channels', error);
}
};
14 changes: 14 additions & 0 deletions backend/swaggerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ const swaggerDefinition = {
},
description: '특정 게스트의 정보',
},

// 사용자별 채널 가져오기 응답 스키마
GetUserChannelsResponse: {
type: 'array',
items: {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
generated_at: { type: 'string', format: 'date-time' },
},
required: ['id', 'name', 'generated_at'],
},
},
},
},
};
Expand Down

0 comments on commit c2ab9cd

Please sign in to comment.