Skip to content

Commit

Permalink
Merge pull request #151 from boostcampwm-2024/feature/be/#49-#50-add-โ€ฆ
Browse files Browse the repository at this point in the history
โ€ฆchannel

[BE][Feat] #50 : ๊ฒฝ๋กœ๋ฐฉ ๋ณ„ ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ post api
  • Loading branch information
happyhyep authored Nov 12, 2024
2 parents 21a29ee + b197081 commit f1ef1c9
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 21 deletions.
29 changes: 28 additions & 1 deletion backend/src/controllers/channelController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createChannelService } from '../services/channelService.js';
import { addGuestService, createChannelService } from '../services/channelService.js';

export const createChannelController = async (req, res) => {
try {
Expand All @@ -19,3 +19,30 @@ export const createChannelController = async (req, res) => {
});
}
};

export const addGuestController = async (req, res) => {
try {
const { channelId } = req.params;
const { guests } = req.body;

const updatedChannel = await addGuestService(channelId, guests);

if (!updatedChannel) {
return res.status(404).json({
success: false,
message: 'Channel not found',
});
}

return res.status(200).json({
success: true,
message: 'Guests added successfully',
});
} catch (err) {
console.error(err);
return res.status(500).json({
success: false,
message: 'Server error',
});
}
};
16 changes: 16 additions & 0 deletions backend/src/repositories/channelRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ export const createChannelInDB = async (name, host_id) => {

return result.rows[0];
};

export const getChannelById = async channelId => {
const query = `
SELECT *
FROM "main"."channel"
WHERE id = $1;
`;
const values = [channelId];
const result = await pool.query(query, values);

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

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

export const addGuestInDB = async (
export const addGuestToChannel = async (
channel_id,
name,
start_location,
Expand Down
95 changes: 94 additions & 1 deletion backend/src/routes/channelRouter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express';
import { body } from 'express-validator';
import { createChannelController } from '../controllers/channelController.js';
import { addGuestController, createChannelController } from '../controllers/channelController.js';
import { validationMiddleware } from '../middleware/validationMiddleware.js';

export const channelRouter = express.Router();
Expand Down Expand Up @@ -40,3 +40,96 @@ channelRouter.post(
validationMiddleware,
createChannelController,
);

/**
* @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:
* 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'
* 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'
*/
channelRouter.post(
'/:channelId/guests',
[body('guests').isArray().withMessage('Guests must be an array')],
validationMiddleware,
addGuestController,
);
13 changes: 0 additions & 13 deletions backend/src/routes/example.js

This file was deleted.

30 changes: 25 additions & 5 deletions backend/src/services/channelService.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { createChannelInDB } from '../repositories/channelRepository.js';
import { addGuestInDB } from '../repositories/guestRepository.js';
import { createChannelInDB, getChannelById } from '../repositories/channelRepository.js';
import { addGuestToChannel } 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(
return addGuestToChannel(
channel.id,
name,
start_location,
Expand All @@ -23,3 +21,25 @@ export const createChannelService = async (name, host_id, guests) => {

return channel;
};

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

const guestPromises = guests.map(guest => {
const { name, start_location, end_location, path, marker_style } = guest;
return addGuestToChannel(
channelId,
name,
start_location,
end_location,
path,
marker_style,
channel.host_id,
);
});

await Promise.all(guestPromises);

return channel;
};
125 changes: 125 additions & 0 deletions backend/swaggerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,92 @@ const swaggerDefinition = {
},
},
},
// ๊ฒŒ์ŠคํŠธ ์ถ”๊ฐ€ ์š”์ฒญ ์Šคํ‚ค๋งˆ
AddGuestRequest: {
type: 'object',
properties: {
channel_id: {
type: 'string',
description: '์ฑ„๋„ ID (UUID ํ˜•ํƒœ)',
},
guest: {
type: 'object',
properties: {
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: '๋„์ฐฉ์ง€ ๋งˆ์ปค์˜ ๊ฒฝ๋„',
},
},
},
path: {
type: 'array',
items: {
type: 'object',
properties: {
lat: {
type: 'number',
description: '๊ฒฝ๋กœ n๋ฒˆ์งธ ์ง€์ ์˜ ์œ„๋„',
},
lng: {
type: 'number',
description: '๊ฒฝ๋กœ n๋ฒˆ์งธ ์ง€์ ์˜ ๊ฒฝ๋„',
},
},
},
description: '๊ฒŒ์ŠคํŠธ์˜ ๊ฒฝ๋กœ (์œ„๋„, ๊ฒฝ๋„)๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด',
},
marker_style: {
type: 'object',
properties: {
color: {
type: 'string',
description: '๊ฒŒ์ŠคํŠธ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์ƒ‰์ƒ ์Šคํƒ€์ผ',
},
},
},
},
},
},
},

// ๊ฒŒ์ŠคํŠธ ์ถ”๊ฐ€ ์‘๋‹ต ์Šคํ‚ค๋งˆ
AddGuestResponse: {
type: 'object',
properties: {
success: {
type: 'boolean',
description: '์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์—ˆ๋Š”์ง€ ์—ฌ๋ถ€',
},
message: {
type: 'string',
description: '์‘๋‹ต ๋ฉ”์‹œ์ง€',
},
},
},
},
},
paths: {
Expand Down Expand Up @@ -241,6 +327,45 @@ const swaggerDefinition = {
},
},
},
'/channel/guests': {
post: {
summary: '์ฑ„๋„์— ๊ฒŒ์ŠคํŠธ ์ถ”๊ฐ€ API',
description: '์ฃผ์–ด์ง„ ์ฑ„๋„์— ์ƒˆ๋กœ์šด ๊ฒŒ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.',
operationId: 'addGuest',
requestBody: {
description: '์ฑ„๋„์— ๊ฒŒ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/AddGuestRequest',
},
},
},
required: true,
},
responses: {
200: {
description: '๊ฒŒ์ŠคํŠธ ์ถ”๊ฐ€ ์„ฑ๊ณต',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/AddGuestResponse',
},
},
},
},
400: {
description: '์ž˜๋ชป๋œ ์š”์ฒญ, ์ฑ„๋„ ID๋‚˜ ๊ฒŒ์ŠคํŠธ ์ •๋ณด๊ฐ€ ์ž˜๋ชป๋จ',
},
404: {
description: '์ฑ„๋„์„ ์ฐพ์„ ์ˆ˜ ์—†์Œ',
},
500: {
description: '์„œ๋ฒ„ ์—๋Ÿฌ',
},
},
},
},
},
};

Expand Down

0 comments on commit f1ef1c9

Please sign in to comment.