Skip to content

Commit

Permalink
Merge pull request #106 from catholichak2024/feat/#88_withdraw
Browse files Browse the repository at this point in the history
[FEAT] νšŒμ›νƒˆν‡΄
  • Loading branch information
seoyeoneel02 authored Oct 29, 2024
2 parents 1b75a40 + 3c04582 commit a08878a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/domains/mypage/mypage.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { response } from "../../response.js";
import { status } from "../../response.status.js";
import { mypageService, majorService, pwService, searchService } from "./mypage.service.js";
import { mypageService, majorService, pwService, searchService, delService } from "./mypage.service.js";

export const mypageController = async (req, res, next) => {
console.log("λ§ˆμ΄νŽ˜μ΄μ§€λ₯Ό μ‘°νšŒν•©λ‹ˆλ‹€!");
Expand All @@ -25,4 +25,10 @@ export const pwController = async (req, res, next) => {
export const searchController = async (req, res, next) => {
console.log("전곡을 κ²€μƒ‰ν•©λ‹ˆλ‹€!");
res.send(response(status.SUCCESS, await searchService()));
}

export const delController = async (req, res, next) => {
console.log("νšŒμ›νƒˆν‡΄λ₯Ό μš”μ²­ν–ˆμŠ΅λ‹ˆλ‹€!");
const userId = req.decoded.userId;
res.send(response(status.SUCCESS, await delService(userId)));
}
7 changes: 7 additions & 0 deletions src/domains/mypage/mypage.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ export const majorDTO = (major) => {

export const searchDTO = (major) => {
return {majorData: major[0]};
}

export const delDTO = (data) => {
if (data == null) {
return {markData: "νƒˆν‡΄κ°€ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€."};
}
return {markData: data[0]};
}
33 changes: 32 additions & 1 deletion src/domains/mypage/mypage.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,35 @@ export const searchRepo = async () => {
conn.release();
}
}


export const delRepo = async (userId) => {
const conn = await pool.getConnection();
try {
const existUser = await pool.query("SELECT EXISTS(SELECT 1 FROM user WHERE id = ?) as isExistUser;", userId);
if (!existUser[0][0].isExistUser) {
return 0;
}
await pool.query("DELETE FROM user_subject WHERE user_id = ?;", userId);
await pool.query("DELETE FROM user WHERE id = ?;", userId);
return userId;
} catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
} finally {
conn.release();
}
}

export const getUser = async (userId) => {
const conn = await pool.getConnection();
try {
const user = await pool.query("SELECT * FROM user WHERE id = ?;", userId);
if (user[0].length == 0) {
return null;
}
return user;
} catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
} finally {
conn.release();
}
}
13 changes: 11 additions & 2 deletions src/domains/mypage/mypage.service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BaseError } from "../../errors.js";
import { status } from "../../response.status.js";
import bcrypt from "bcrypt";
import { mypageDTO, majorDTO, searchDTO } from "./mypage.dto.js";
import { mypageRepo, majorRepo, pwRepo, searchRepo } from "./mypage.repository.js";
import { mypageDTO, majorDTO, searchDTO, delDTO } from "./mypage.dto.js";
import { mypageRepo, majorRepo, pwRepo, searchRepo, delRepo, getUser } from "./mypage.repository.js";

export const mypageService = async (userId) => {
const user = await mypageRepo(userId);
Expand Down Expand Up @@ -32,4 +32,13 @@ export const pwService = async (userId, body) => {
export const searchService = async () => {
const major = await searchRepo();
return searchDTO(major);
}

export const delService = async (userId) => {
const delId = await delRepo(userId);
if (delId == 0) {
throw new BaseError(status.MARK_NOT_EXIST);
}
const result = await getUser(delId);
return delDTO(result);
}
5 changes: 3 additions & 2 deletions src/routes/mypage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express from "express";
import asyncHandler from "express-async-handler";
import { tokenAuth } from "../middlewares/token.auth.js";
import { mypageController, majorController, pwController, searchController } from "../domains/mypage/mypage.controller.js";
import { mypageController, majorController, pwController, searchController, delController } from "../domains/mypage/mypage.controller.js";

export const mypageRoute = express.Router();

mypageRoute.get('', tokenAuth, asyncHandler(mypageController));
mypageRoute.patch('/major', tokenAuth, asyncHandler(majorController));
mypageRoute.patch('/pw', tokenAuth, asyncHandler(pwController));
mypageRoute.get('/major/search', tokenAuth, asyncHandler(searchController));
mypageRoute.get('/major/search', tokenAuth, asyncHandler(searchController));
mypageRoute.delete('', tokenAuth, asyncHandler(delController));

0 comments on commit a08878a

Please sign in to comment.