Skip to content

Commit

Permalink
Merge pull request #129 from UMC-FITple/feat/#125_backend_search-item…
Browse files Browse the repository at this point in the history
…-wish

검색한 옷 wish에 생성, 삭제, 조회하는 로직 구현
  • Loading branch information
seoyeoneel02 authored Sep 25, 2024
2 parents 7abe3c4 + c315afb commit 9195fb0
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 15 deletions.
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 } from "./search.provider.js";
import { addMyCloth } from "./search.service.js";
import { getSearch, getCloth, getSearchResult, getSearchBrand, getMyCloth, getMyWish } from "./search.provider.js";
import { addMyCloth, addMyWish, delMyWish } from "./search.service.js";

export const searchPreview = async (req, res, next) => {
console.log("검색 메인화면을 조회합니다");
Expand Down Expand Up @@ -33,4 +33,22 @@ export const addCloth = async (req, res, next) => {
console.log("옷 등록을 요청하였습니다!");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await addMyCloth(userId, req.body)));
}

export const addWish = async (req, res, next) => {
console.log("관심 있는 옷 추가를 요청하였습니다!");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await addMyWish(userId, req.params.clothId)));
}

export const delWish = async (req, res, next) => {
console.log("관심 있는 옷 삭제를 요청하였습니다!");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await delMyWish(userId, req.params.clothId)));
}

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)));
}
47 changes: 46 additions & 1 deletion src/domains/search/search.dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { UserNicknameToClothId, UserCategoryToClothId,
UserNicknameToClothName, UserCategoryToClothName,
brandToBrandName, userIdToNickname, userToNickname, getBrandToBrandId,
userToBrand, categoryToBrand, clothToBrand, clothCategoryToBrand,
insertCloth, insertRealSize, getCloth } from "./search.sql.js";
insertCloth, insertRealSize, getCloth,
addWishSQL, delWishSQL, getWishSQL } from "./search.sql.js";

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

// Wish 추가
export const addWishDAO = async (userId, clothId) => {
try{
const conn = await pool.getConnection();
const is_exist = await pool.query(getWishSQL, [clothId, userId]);
if(is_exist[0].length !== 0){
throw new BaseError(status.PARAMETER_IS_WRONG);
}
const wish = await pool.query(addWishSQL, [clothId, userId]);

conn.release();
return wish[0].insertId;
}catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}

// Wish 삭제
export const delWishDAO = async (userId, clothId) => {
try{
const conn = await pool.getConnection();
await pool.query(delWishSQL, [clothId, userId]);
const wish = await pool.query(getWishSQL, [clothId, userId]);

conn.release();
return wish;
}catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}

// Wish 조회
export const getWishDAO = async (userId, clothId) => {
try{
const conn = await pool.getConnection();
const wish = await pool.query(getWishSQL, [clothId, userId]);

conn.release();
return wish;
}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 @@ -170,4 +170,29 @@ export const addClothResponseDTO = (data) => {
})
}
return {"clothData": cloth};
}

export const addWishDTO = (wish) => {
return {"wish_id": wish};
}

export const delWishDTO = (wish) => {
let wish_id;
if(wish[0].length == 0){
wish_id = 0;
}

return {"wish_id": wish_id};
}

export const getWishDTO = (wish) => {

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

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

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

return previewMyClothResponseDTO(await getPreviewMyCloth(userId, clothId));
}

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

return getWishDTO(await getWishDAO(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 } from "./search.dto.js";
import { clothAdd, getAddCloth } from "./search.dao.js";
import { addClothResponseDTO, addWishDTO, delWishDTO } from "./search.dto.js";
import { clothAdd, getAddCloth, addWishDAO, delWishDAO } from "./search.dao.js";

export const addMyCloth = async (userId, body) => {
const requiredFields = ['name', 'product_code', 'category', 'size', 'fit'];
Expand Down Expand Up @@ -46,4 +46,12 @@ export const addMyCloth = async (userId, body) => {
'hem': body.hem
});
return addClothResponseDTO(await getAddCloth(clothData));
}

export const addMyWish = async (userId, clothId) => {
return addWishDTO(await addWishDAO(userId, clothId));
}

export const delMyWish = async (userId, clothId) => {
return delWishDTO(await delWishDAO(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 @@ -105,4 +105,10 @@ export const insertCloth =
export const insertRealSize =
"INSERT INTO real_size (cloth_id, length, shoulder, chest, armhole, sleeve, sleeve_length, hem) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ;"

export const getCloth = "SELECT * FROM cloth WHERE id = ? ; "
export const getCloth = "SELECT * FROM cloth WHERE id = ? ; "

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 = ? ;"
13 changes: 11 additions & 2 deletions src/routes/search.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from "express";
import asyncHandler from 'express-async-handler';
import { LoginCheck } from "../middlewares/logincheck.js";
import { searchPreview, clothView, searchView, brandView, addClothPreview, addCloth } from "../domains/search/search.controller.js";
import { searchPreview, clothView, searchView, brandView, addClothPreview, addCloth, addWish, delWish, getWish } from "../domains/search/search.controller.js";

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

Expand All @@ -21,4 +21,13 @@ searchRouter.get('/brand/:brandId', LoginCheck, asyncHandler(brandView));
searchRouter.get('/:clothId/add', LoginCheck, asyncHandler(addClothPreview));

//검색-옷장에 등록
searchRouter.post('/:clothId/add', LoginCheck, asyncHandler(addCloth));
searchRouter.post('/:clothId/add', LoginCheck, asyncHandler(addCloth));

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

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

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

0 comments on commit 9195fb0

Please sign in to comment.