Skip to content

Commit

Permalink
Merge branch 'development' into frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
happyhyep authored Nov 14, 2024
2 parents 18ec212 + 6907ab5 commit 2dde3d2
Show file tree
Hide file tree
Showing 15 changed files with 1,257 additions and 75 deletions.
66 changes: 33 additions & 33 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
{
"name": "ddara-backend",
"private": true,
"workspaces": [
"frontend",
"backend"
],
"version": "0.0.0",
"type": "module",
"description": "๋”ฐ๋ผ๋”ฐ๋ผ์˜ ์„ ๋”ฐ๋ผ๊ธธ๋”ฐ๋ผ BackEnd ์ฝ”๋“œ",
"main": "index.js",
"scripts": {
"dev": "node src/index.js",
"test": "vitest",
"test:watch": "vitest --watch",
"test:coverage": "vitest run --coverage",
"lint": "pnpm lint-staged"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"express-validator": "^7.2.0",
"jsonwebtoken": "^9.0.2",
"pg": "^8.13.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"uuid": "^11.0.3",
"ws": "^8.11.0"
}
}
"name": "ddara-backend",
"private": true,
"workspaces": [
"frontend",
"backend"
],
"version": "0.0.0",
"type": "module",
"description": "๋”ฐ๋ผ๋”ฐ๋ผ์˜ ์„ ๋”ฐ๋ผ๊ธธ๋”ฐ๋ผ BackEnd ์ฝ”๋“œ",
"main": "index.js",
"scripts": {
"dev": "node src/index.js",
"test": "vitest",
"test:watch": "vitest --watch",
"test:coverage": "vitest run --coverage",
"lint": "pnpm lint-staged"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"express-validator": "^7.2.0",
"jsonwebtoken": "^9.0.2",
"pg": "^8.13.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"uuid": "^11.0.3",
"ws": "^8.11.0"
}
}
38 changes: 33 additions & 5 deletions backend/src/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import { loginUser } from '../services/authService.js';
import { loginUser, registerUser } from '../services/authService.js';

export const login = async (req, res) => {
/**
* @description ๋กœ๊ทธ์ธ ์ปจํŠธ๋กค๋Ÿฌ
*/
export const loginController = async (req, res) => {
const { id, password } = req.body;

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' });
}
};

/**
* @description ํšŒ์›๊ฐ€์ž… ์ปจํŠธ๋กค๋Ÿฌ
*/
export const registerUserController = async (req, res) => {
try {
const { id, name, password, email } = req.body;
const newUser = await registerUser(id, name, password, email);
return res.status(201).json({
success: true,
message: 'Login successfully',
data: newUser,
});
} catch (error) {
if (error.message === 'User ID already exists') {
return res.status(409).json({ error: 'User ID already exists' });
}
console.error('User registration error:', error);
res.status(500).json({ error: 'Server error' });
}
};
126 changes: 126 additions & 0 deletions backend/src/controllers/channelController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import {
addGuestService,
createChannelService,
getChannelByIdService,
getChannelGuestInfoService,
getUserChannels,
} from '../services/channelService.js';

/**
* @description ์ฑ„๋„ ์ƒ์„ฑ ์ปจํŠธ๋กค๋Ÿฌ
*/
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',
});
}
};

/**
* @description ์ฑ„๋„์— ๊ฒŒ์ŠคํŠธ ์ถ”๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ
*/
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',
});
}
};

/**
* @description ์ฑ„๋„ ์ •๋ณด ์กฐํšŒ ์ปจํŠธ๋กค๋Ÿฌ
*/
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' });
}
};

/**
* @description ์ฑ„๋„์— ํŠน์ • ๊ฒŒ์ŠคํŠธ ์ •๋ณด ์กฐํšŒ ์ปจํŠธ๋กค๋Ÿฌ
*/
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({ success: false, message: 'Channel or guest not found' });
}
} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: 'Server error' });
}
};

/**
* @description ์‚ฌ์šฉ์ž์˜ ์ฑ„๋„ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์ปจํŠธ๋กค๋Ÿฌ
*/
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' });
}
};
19 changes: 2 additions & 17 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,18 @@ import express from 'express';
import swaggerUi from 'swagger-ui-express';
import http from 'http';
import { specs } from '../swaggerConfig.js';
import { pool } from './db/db.js';
import { PORT } from './constants/constants.js';
import { initializeWebSocketServer } from './websocketServer.js';
import { authRouter } from './routes/authRouter.js';
import { channelRouter } from './routes/channelRouter.js';

const app = express();
app.use(express.json());

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));

app.use('/api/auth', authRouter);

// TODO: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ˆ์‹œ
app.get('/guests', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM guest');
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('์„œ๋ฒ„ ์˜ค๋ฅ˜');
}
});

// TODO: ์˜ˆ์ œ ๋ผ์šฐํ„ฐ (์ถ”๊ฐ€ ์˜ˆ์ •์ธ ๋ผ์šฐํ„ฐ์˜ ์ฃผ์„์„ Swagger ์ฃผ์„ ํ˜•์‹์œผ๋กœ ๋ฌธ์„œํ™”)
app.get('/example', (req, res) => {
res.send('Hello World');
});
app.use('/api/channel', channelRouter);

// HTTP ์„œ๋ฒ„ ์ƒ์„ฑ
const server = http.createServer(app);
Expand Down
Loading

0 comments on commit 2dde3d2

Please sign in to comment.