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

검색한 옷 등록한 유저 follow 로직 구현 #133

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
22 changes: 20 additions & 2 deletions src/domains/search/search.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { response } from "../../config/response.js";
import { status } from "../../config/response.status.js";
import { getSearch, getCloth, getSearchResult, getSearchBrand, getMyCloth, getMyWish } from "./search.provider.js";
import { addMyCloth, addMyWish, delMyWish } from "./search.service.js";
import { getSearch, getCloth, getSearchResult, getSearchBrand, getMyCloth, getMyWish, getMyFollow } from "./search.provider.js";
import { addMyCloth, addMyWish, delMyWish, addMyFollow, delMyFollow } from "./search.service.js";

export const searchPreview = async (req, res, next) => {
console.log("검색 메인화면을 조회합니다");
Expand Down Expand Up @@ -51,4 +51,22 @@ export const getWish = async (req, res, next) => {
console.log("관심 있는 옷 조회를 요청하였습니다!");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await getMyWish(userId, req.params.clothId)));
}

export const addFollow = async (req, res, next) => {
console.log("팔로우를 요청하였습니다!");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await addMyFollow(userId, req.params.clothId)));
}

export const delFollow = async (req, res, next) => {
console.log("팔로우 취소를 요청하였습니다!");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await delMyFollow(userId, req.params.clothId)));
}

export const getFollow = async (req, res, next) => {
console.log("팔로우 조회를 요청하였습니다!");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await getMyFollow(userId, req.params.clothId)));
}
54 changes: 53 additions & 1 deletion src/domains/search/search.dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { UserNicknameToClothId, UserCategoryToClothId,
brandToBrandName, userIdToNickname, userToNickname, getBrandToBrandId,
userToBrand, categoryToBrand, clothToBrand, clothCategoryToBrand,
insertCloth, insertRealSize, getCloth,
addWishSQL, delWishSQL, getWishSQL } from "./search.sql.js";
addWishSQL, delWishSQL, getWishSQL, getFollowSQL, addFollowSQL, delFollowSQL } from "./search.sql.js";

// nickname+cloth 반환
export const getNicknameToClothId = async (category) => {
Expand Down Expand Up @@ -269,4 +269,56 @@ export const getWishDAO = async (userId, clothId) => {
}catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}

// Follow 추가
export const addFollowDAO = async (userId, clothId) => {
try{
const conn = await pool.getConnection();
const to_user = await pool.query(getUserIdToClothId, clothId);
if(userId == to_user[0][0].uuid){
throw new BaseError(status.PARAMETER_IS_WRONG);
}
const is_exist = await pool.query(getFollowSQL, [userId, to_user[0][0].uuid]);
if(is_exist[0].length !== 0){
throw new BaseError(status.PARAMETER_IS_WRONG);
}
const follow = await pool.query(addFollowSQL, [userId, to_user[0][0].uuid]);
conn.release();
return follow[0].insertId;
}catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}

// Follow 취소
export const delFollowDAO = async (userId, clothId) => {
try{
const conn = await pool.getConnection();
const to_user = await pool.query(getUserIdToClothId, clothId);
const is_exist = await pool.query(getFollowSQL, [userId, to_user[0][0].uuid]);
if(is_exist[0].length == 0){
throw new BaseError(status.PARAMETER_IS_WRONG);
}
await pool.query(delFollowSQL, [userId, to_user[0][0].uuid]);
const follow = await pool.query(getFollowSQL, [userId, to_user[0][0].uuid]);
conn.release();
return follow;
}catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}

// Follow 조회
export const getFollowDAO = async (userId, clothId) => {
try{
const conn = await pool.getConnection();
const to_user = await pool.query(getUserIdToClothId, clothId);
const follow = await pool.query(getFollowSQL, [userId, to_user[0][0].uuid]);

conn.release();
return follow;
}catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}
25 changes: 25 additions & 0 deletions src/domains/search/search.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,29 @@ export const getWishDTO = (wish) => {
}

return {"wish_id": wish_id};
}

export const addFollowDTO = (follow) => {
return {"follow_id": follow};
}

export const delFollowDTO = (follow) => {
let follow_id;
if(follow[0].length == 0){
follow_id = 0;
}

return {"follow_id": follow_id};
}

export const getFollowDTO = (follow) => {

let follow_id;
if(follow[0].length == 0){
follow_id = 0;
} else if(follow[0].length == 1){
follow_id = follow[0][0].id
}

return {"follow_id": follow_id};
}
9 changes: 7 additions & 2 deletions src/domains/search/search.provider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { previewSearchResponseDTO, previewClothResponseDTO, SearchResultResponseDTO, SearchBrandResponseDTO,
previewMyClothResponseDTO, getWishDTO } from "./search.dto.js"
previewMyClothResponseDTO, getWishDTO, getFollowDTO } from "./search.dto.js"
import { getNicknameToClothId, getPreviewCloth, getUserToClothId, getNicknameToClothName, getPreviewBrand, getPreviewUser,
getBrand, getNicknameToBrand, getPreviewMyCloth, getWishDAO } from "./search.dao.js";
getBrand, getNicknameToBrand, getPreviewMyCloth, getWishDAO, getFollowDAO } from "./search.dao.js";

export const getSearch = async (query) => {
const { category } = query;
Expand Down Expand Up @@ -34,4 +34,9 @@ export const getMyCloth = async (userId, clothId) => {
export const getMyWish = async (userId, clothId) => {

return getWishDTO(await getWishDAO(userId, clothId));
}

export const getMyFollow = async (userId, clothId) => {

return getFollowDTO(await getFollowDAO(userId, clothId));
}
12 changes: 10 additions & 2 deletions src/domains/search/search.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseError } from "../../config/error.js";
import { status } from "../../config/response.status.js";
import { addClothResponseDTO, addWishDTO, delWishDTO } from "./search.dto.js";
import { clothAdd, getAddCloth, addWishDAO, delWishDAO } from "./search.dao.js";
import { addClothResponseDTO, addWishDTO, delWishDTO, addFollowDTO, delFollowDTO } from "./search.dto.js";
import { clothAdd, getAddCloth, addWishDAO, delWishDAO, addFollowDAO, delFollowDAO } from "./search.dao.js";

export const addMyCloth = async (userId, body) => {
const requiredFields = ['name', 'product_code', 'category', 'size', 'fit'];
Expand Down Expand Up @@ -54,4 +54,12 @@ export const addMyWish = async (userId, clothId) => {

export const delMyWish = async (userId, clothId) => {
return delWishDTO(await delWishDAO(userId, clothId));
}

export const addMyFollow = async (userId, clothId) => {
return addFollowDTO(await addFollowDAO(userId, clothId));
}

export const delMyFollow = async (userId, clothId) => {
return delFollowDTO(await delFollowDAO(userId, clothId));
}
8 changes: 7 additions & 1 deletion src/domains/search/search.sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,10 @@ export const addWishSQL = "INSERT INTO wish (cloth_id, wisher_uuid) VALUES (?, ?

export const delWishSQL = "DELETE FROM wish WHERE cloth_id = ? AND wisher_uuid = ? ;"

export const getWishSQL = "SELECT id FROM wish WHERE cloth_id = ? AND wisher_uuid = ? ;"
export const getWishSQL = "SELECT id FROM wish WHERE cloth_id = ? AND wisher_uuid = ? ;"

export const getFollowSQL = "SELECT id FROM follow WHERE from_uuid = ? AND to_uuid = ? ;"

export const addFollowSQL = "INSERT INTO follow (from_uuid, to_uuid) VALUES (?, ?) ;"

export const delFollowSQL = "DELETE FROM follow WHERE from_uuid = ? AND to_uuid = ? ;"
16 changes: 13 additions & 3 deletions src/routes/search.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express from "express";
import asyncHandler from 'express-async-handler';
import { LoginCheck } from "../middlewares/logincheck.js";
import { searchPreview, clothView, searchView, brandView, addClothPreview, addCloth, addWish, delWish, getWish } from "../domains/search/search.controller.js";
import { searchPreview, clothView, searchView, brandView, addClothPreview, addCloth,
addWish, delWish, getWish, addFollow, delFollow, getFollow } from "../domains/search/search.controller.js";

export const searchRouter = express.Router({mergeParams: true});

Expand Down Expand Up @@ -29,5 +30,14 @@ searchRouter.post('/:clothId/wish', LoginCheck, asyncHandler(addWish));
//검색-wish에서 삭제
searchRouter.delete('/:clothId/wish', LoginCheck, asyncHandler(delWish));

//검색-wish
searchRouter.get('/:clothId/wish', LoginCheck, asyncHandler(getWish));
//검색-wish 조회
searchRouter.get('/:clothId/wish', LoginCheck, asyncHandler(getWish));

//검색-follow에 추가
searchRouter.post('/:clothId/follow', LoginCheck, asyncHandler(addFollow));

//검색-follow에서 삭제
searchRouter.delete('/:clothId/follow', LoginCheck, asyncHandler(delFollow));

//검색-follow 조회
searchRouter.get('/:clothId/follow', LoginCheck, asyncHandler(getFollow));
Loading