Skip to content

Commit

Permalink
Merge pull request #43 from Romantic-Yeojido/develop
Browse files Browse the repository at this point in the history
feature/#39-deploy
  • Loading branch information
bulee5328 authored Dec 20, 2024
2 parents 306b859 + e1bbc1b commit 8462425
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
run: |
ssh romantic-yeojido '
echo "[Unit]
Description=UMC 7th Project
Description=romantic-yeojido
After=network.target
[Service]
Expand Down
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dotenv": "^16.4.7",
"express": "^5.0.1",
"express-session": "^1.18.1",
"http-status-code": "^2.1.0",
"http-status-codes": "^2.3.0",
"multer": "^1.4.5-lts.1",
"multer-s3": "^3.0.1",
Expand Down
115 changes: 67 additions & 48 deletions src/controllers/memo.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StatusCodes } from "http-status-codes";
import { responseFromMemories } from "../dtos/memo.dto.js";
import { postMemories , updateMemory ,deleteMemory } from "../services/memo.service.js";
import { responseFromMemories , responseFromGetMemories} from "../dtos/memo.dto.js";
import { postMemories, updateMemory, deleteMemory, getMemory } from "../services/memo.service.js";


export const handleMemories = async (req, res, next) => {
Expand Down Expand Up @@ -37,57 +37,76 @@ export const handleMemories = async (req, res, next) => {
user_id: userId,
location_id: locationId,
});

const memories = await postMemories(memoriesData);
console.log(memories);

res.status(StatusCodes.OK).json({ result: memories });
} catch (error) {
res.status(StatusCodes.NOT_FOUND).json({
success: false,
message: error.message,
});
}
} catch (error) {
res.status(StatusCodes.NOT_FOUND).json({
success: false,
message: error.message,
});
}
};

export const handleUpdateMemory = async (req, res, next) => {
try {
const memoryId = parseInt(req.params.memoryId);
const userId = parseInt(req.body.userId); // 사용자 ID
console.log('Controller received:', { memoryId, userId, body: req.body }); // 로그 추가
// 수정된 필드만 포함된 데이터 전달
const updatedMemory = await updateMemory(memoryId,userId, req.body);

res.status(StatusCodes.OK).json({
success: true,
result: updatedMemory
});
} catch (error) {
console.error('Memory update error:', error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({
success: false,
message: error.message
});
}
try {
const memoryId = parseInt(req.params.memoryId);
const userId = parseInt(req.body.userId); // 사용자 ID
console.log('Controller received:', { memoryId, userId, body: req.body }); // 로그 추가
// 수정된 필드만 포함된 데이터 전달
const updatedMemory = await updateMemory(memoryId, userId, req.body);

res.status(StatusCodes.OK).json({
success: true,
result: updatedMemory
});
} catch (error) {
console.error('Memory update error:', error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({
success: false,
message: error.message
});
}
};
export const handleDeleteMemory = async (req, res) => {
try {
const memoryId = parseInt(req.params.memoryId);
const userId = parseInt(req.body.userId);

await deleteMemory(memoryId, userId);

res.status(StatusCodes.OK).json({
success: true,
message: "추억이 삭제되었습니다."
});
} catch (error) {
console.error('Memory delete error:', error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({
success: false,
message: error.message
});
}
};
try {
const memoryId = parseInt(req.params.memoryId);
const userId = parseInt(req.body.userId);

await deleteMemory(memoryId, userId);

res.status(StatusCodes.OK).json({
success: true,
message: "추억이 삭제되었습니다."
});
} catch (error) {
console.error('Memory delete error:', error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({
success: false,
message: error.message
});
}
};

export const handleGetMemory = async (req, res) => {
try {
const userId = parseInt(req.params.userId);
const locationId = parseInt(req.params.locationId);

const memory = await getMemory(userId, locationId);
console.log(memory);

res.status(StatusCodes.OK).json({ result: memory });
} catch (error) {
console.error('Memory get error:', error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({
success: false,
message: error.message
});
}
};
36 changes: 36 additions & 0 deletions src/controllers/user.home.controller.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// controllers/user.home.controller.js
import { UserHomeService } from '../services/user.home.service.js';
import { RandomMemoryResponseDTO } from '../dtos/user.home.dto.js';
import { StatusCodes } from 'http-status-codes'; // StatusCodes 임포트 추가

export class UserHomeController {
constructor() {
this.userHomeService = new UserHomeService();
this.getRandomMemory = this.getRandomMemory.bind(this);
}

async getTodayMemory(req, res) {
Expand All @@ -25,4 +28,37 @@ export class UserHomeController {
});
}
}

async getRandomMemory(req,res) {
try {
const userId = parseInt(req.params.userId);
const memory = await this.userHomeService.getRandomCompleteMemory(userId);

if (!memory) {
return res.status(StatusCodes.NOT_FOUND).json({
success: false,
message: "보관함에 추억이 없습니다."
});
}
const responseData = new RandomMemoryResponseDTO(memory);


res.status(StatusCodes.OK).json({
success: true,
data: responseData
});

} catch (error) {
console.error('Random memory fetch error:', error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
success: false,
message: "서버 에러가 발생했습니다"
});
}

}




}
19 changes: 18 additions & 1 deletion src/dtos/memo.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ export const responseFromMemories = (body) => {
friends: body.friends || "",
content: body.content || "",
summary: body.summary || "",
updated_at : body.updated_at
}
}

export const responseFromGetMemories = (body) => {
const visit_date = new Date(body.visit_date);

return {
id: body.id,
user_id: body.user_id,
location_id: body.location_id,
title: body.title || "",
visit_date,
friends: body.friends || "",
content: body.content || "",
summary: body.summary || "",
is_deleted: body.is_deleted,
created_at: body.created_at,
updated_at : body.updated_at,
}
}
10 changes: 10 additions & 0 deletions src/dtos/user.home.dto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// dtos/user.home.dto.js
export class RandomMemoryResponseDTO {
constructor(memory) {
this.title = memory.title;
this.content = memory.content;
this.visit_date = memory.visit_date;
this.friends = memory.friends;
this.images = memory.images || [];
}
}
52 changes: 49 additions & 3 deletions src/repositories/memo.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export const addMemories = async (data) => {
throw new Error("존재하지 않는 위치입니다.");
}

const [existingMemory] = await conn.query(
`SELECT id FROM memories WHERE user_id = ? AND location_id = ? AND is_deleted = false`,
[data.user_id, data.location_id]
);

if (existingMemory.length > 0) {
throw new Error("해당 사용자와 위치에 대한 추억이 이미 존재합니다.");
}

const [result] = await pool.query(
`INSERT INTO memories (user_id, location_id, title, visit_date, friends, content, summary) VALUES (?, ?, ?, ?, ?, ?, ?);`,
[
Expand All @@ -45,15 +54,14 @@ export const addMemories = async (data) => {
};


// Memories 테이블 정보 얻기
export const getMemories = async (memoryId) => {
const conn = await pool.getConnection();

try {
const [memory] = await pool.query('SELECT * FROM memories WHERE id = ?', memoryId);

if (memory.length === 0) {
throw new Error(`Memory with ID ${memoryId} not found`);
throw new Error(` 존재하지 않는 추억 id입니다.`);
}

return memory[0];
Expand Down Expand Up @@ -152,4 +160,42 @@ export const softDeleteMemory = async (memoryId, userId) => {
} finally {
conn.release();
}
};
};

// Memories 테이블 정보 얻기
export const getMemoryById = async (userId, locationId) => {
const conn = await pool.getConnection();

try {
const [memory] = await pool.query(
'SELECT * FROM memories WHERE user_id = ? AND location_id = ? AND is_deleted = false', [userId, locationId]);

const [user] = await pool.query(
'SELECT * FROM memories WHERE user_id = ? AND is_deleted = false',
[userId]
);
const [location] = await pool.query(
'SELECT * FROM memories WHERE location_id = ? AND is_deleted = false',
[locationId]
);

if (user.length === 0 && location.length !== 0) {
throw new Error(`존재하지 않는 사용자입니다.`);
}

if (location.length === 0 && user.length !== 0) {
throw new Error(`존재하지 않는 위치입니다.`);
}

if (memory.length === 0) {
throw new Error(`존재하지 않는 사용자와 위치입니다.`);
}

return memory[0];
} catch (error) {
console.error(error.message);
throw error;
} finally {
conn.release();
}
};
Loading

0 comments on commit 8462425

Please sign in to comment.