Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/my info bug #66

Merged
merged 6 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/controller/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@ import { resolveModelGetter } from "sequelize-typescript";
const getMyInfoController = async (req: Request, res: Response) => {
try {
const resData = await userService.getMyInfoService(req.body.userID.id);
response.dataResponse(
res,
returnCode.OK,
"마이페이지 조회 성공.",
true,
resData
);

if (resData === constant.NON_EXISTENT_USER) {
response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"존재하지 않는 유저입니다."
);
} else {
response.dataResponse(
res,
returnCode.OK,
"마이페이지 조회 성공.",
true,
resData
);
}
} catch (err) {
slack.slackWebhook(req, err.message);
console.error(err.message);
Expand Down Expand Up @@ -69,13 +79,21 @@ const patchImgController = async (req: Request, res: Response) => {
false,
"잘못된 폼 데이터입니다."
);
} else if (resData === constant.NON_EXISTENT_USER) {
response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"존재하지 않는 유저입니다."
);
} else {
// 모두 성공시
response.basicResponse(
response.dataResponse(
res,
returnCode.OK,
"프로필 이미지 변경 성공.",
true,
"프로필 이미지 변경 완료."
resData
);
}
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions src/library/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export default {
VALUE_ALREADY_EXIST: -9, // 이미 존재하는 값일 때
VALUE_ALREADY_DELETED: -10, // 이미 삭제된 값일 때
DB_NOT_FOUND: -11, // DB 응답값이 없을 때
NON_EXISTENT_USER: -12, // 존재하지 않는 유저일 때
};
2 changes: 1 addition & 1 deletion src/router/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import authMiddleware from "../middleware/auth";

const router = express.Router();

router.get("/myInfo", authMiddleware, userController.getMyInfoController);
router.patch(
"/img",
upload.single("img"),
authMiddleware,
userController.patchImgController
);
router.get("/myInfo", authMiddleware, userController.getMyInfoController);

module.exports = router;
3 changes: 2 additions & 1 deletion src/service/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ const getNicknameService = async (nickname?: string) => {
if (nicknameExist.length > 0) {
return constant.NICKNAME_ALREADY_EXIST;
}

return constant.SUCCESS;
};

/**
Expand Down Expand Up @@ -173,7 +175,6 @@ const postSignupService = async (
isDeleted: false,
},
});

if (nicknameExist.length > 0) {
return constant.NICKNAME_ALREADY_EXIST;
}
Expand Down
3 changes: 2 additions & 1 deletion src/service/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ const getReviewService = async (userId: number, reviewId: number) => {
if (!userId || !reviewId) {
return constant.NULL_VALUE;
}

console.log(userId, reviewId);
const reviewToShow = await Review.findOne({
where: {
id: reviewId,
Expand All @@ -228,6 +228,7 @@ const getReviewService = async (userId: number, reviewId: number) => {
},
});

console.log(reviewToShow);
// 존재하지 않는 리뷰일 때
if (!reviewToShow) {
return constant.WRONG_REQUEST_VALUE;
Expand Down
53 changes: 26 additions & 27 deletions src/service/user.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// sequelize
import Sequelize, { Op } from "sequelize";

// library
// import jwt from "jsonwebtoken";
// import bcrypt from 'bcryptjs';
import constant from "../library/constant";
// import index from "../config";

// model
import { User, Review } from "../models";
Expand All @@ -14,48 +10,51 @@ import { User, Review } from "../models";
* @유저정보조회
* @route GET /user/myInfo
* @access public
* @err
* @err 1. 존재하지 않는 유저
*/
const getMyInfoService = async (userId: number) => {
// TODO: - user isDeleted 상태 확인
const user = await User.findOne({ where: { id: userId, isDeleted: false } });
const user = await User.findOne({
where: { id: userId, isDeleted: false }
});

if (!user) {
return constant.NON_EXISTENT_USER;
}

const img = user.img;
const nickname = user.nickname;
const email = user.email;

const review = await Review.findAll({
where: {
userId,
isDeleted: false,
},
const reviewCount = await Review.count({
where: {userId, isDeleted: false}
});
const reviewCount = review.length;

return { img, nickname, email, reviewCount };
};

/**
* @프로필이미지 수정
* @route PATCH /user/img
* @access public
* @err 잘못된 폼데이터
* @err 1. 잘못된 폼 데이터
* 2. 존재하지 않는 유저
*/
const patchImgService = async (userId: number, img: string) => {
// 폼데이터 맞는지 체크 -> 아니면 return statusCode.WRONG_IMG_FORM
if (!img) {
return constant.NULL_VALUE;
} else if (img === undefined) {
} else if (img === undefined) {
return constant.WRONG_IMG_FORM;
}
// TODO: - user isDeleted 상태 확인
// userId에 맞는 user의 img 업로드
await User.update(
{
img: img,
},
{
where: { id: userId, isDeleted: false },
}
);

const user = await User.findOne({ where: {id: userId, isDeleted: false}});

if (!user) {
return constant.NON_EXISTENT_USER;
}

await user.update({img: img});
await user.save();

return {img: user.img};
};

const userService = {
Expand Down