Skip to content

Commit

Permalink
Merge pull request #128 from UMC-FITple/feat/#124_backend_recommend-s…
Browse files Browse the repository at this point in the history
…tyle

스타일 추천 로직 구현
  • Loading branch information
seoyeoneel02 authored Sep 21, 2024
2 parents d4e33ff + d4ae5eb commit 7abe3c4
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/domains/recommend/recommend.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { response } from "../../config/response.js";
import { status } from "../../config/response.status.js";
import { getBodyInfo, getBodyInfoAll } from "./recommend.provider.js";
import { getBodyInfo, getBodyInfoAll, getStyle, getStyleAll } from "./recommend.provider.js";

export const bodyInfo = async (req, res, next) => {
console.log("비슷한 체형의 유저를 추천합니다");
Expand All @@ -12,4 +12,16 @@ export const bodyInfoAll = async (req, res, next) => {
console.log("비슷한 체형의 유저를 모두 추천합니다");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await getBodyInfoAll(userId)));
}

export const style = async (req, res, next) => {
console.log("비슷한 스타일의 유저를 추천합니다");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await getStyle(userId)));
}

export const styleAll = async (req, res, next) => {
console.log("비슷한 스타일의 유저를 모두 추천합니다");
const userId = res.locals.uuid;
res.send(response(status.SUCCESS, await getStyleAll(userId)));
}
75 changes: 74 additions & 1 deletion src/domains/recommend/recommend.dao.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { pool } from "../../config/db.config.js";
import { BaseError } from "../../config/error.js";
import { status } from "../../config/response.status.js";
import { getUser, findUserToBody, getFitToUserId, getStyleToUserId, findUserAllToBody } from "./recommend.sql.js";
import { getUser, findUserToBody, getFitToUserId, getStyleToUserId, findUserAllToBody,
findUserToStyle1, findUserToStyle2, findUserAllToStyle1, findUserAllToStyle2 } from "./recommend.sql.js";

export const getBodyInfoDAO = async (userId) => {
try {
Expand Down Expand Up @@ -55,4 +56,76 @@ export const getBodyInfoAllDAO = async (userId) => {
} catch (err) {
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}

export const getStyleDAO = async (userId) => {
try {
const conn = await pool.getConnection();
const result = [];
const style = await pool.query(getStyleToUserId, userId);
let recommend;

if(style[0].length = 1){
recommend = await pool.query(findUserToStyle1, [userId, style[0][0].style_name]);
}else if(style[0].length = 2){
recommend = await pool.query(findUserToStyle2, [userId, style[0][1].style_name, style[0].style_name]);
}else{
throw new BaseError(status.MYPROFILE_PREFER_STYLE_LENGHT_ERROR);
}

if(recommend[0].length == 0){
conn.release();
return -1;
}else{
for (let i = 0; i < recommend[0].length; i++) {
const user = await pool.query(getUser, recommend[0][i].uuid);
const fit = await pool.query(getFitToUserId, recommend[0][i].uuid);
const style = await pool.query(getStyleToUserId, recommend[0][i].uuid);
result.push({user, fit, style});
}
}
conn.release();
return result;
} catch (err) {
if (err.data.code === status.MYPROFILE_PREFER_STYLE_LENGHT_ERROR.code) {
throw err;
}
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}

export const getStyleAllDAO = async (userId) => {
try {
const conn = await pool.getConnection();
const result = [];
const style = await pool.query(getStyleToUserId, userId);
let recommend;

if(style[0].length = 1){
recommend = await pool.query(findUserAllToStyle1, [userId, style[0][0].style_name]);
}else if(style[0].length = 2){
recommend = await pool.query(findUserAllToStyle2, [userId, style[0][1].style_name, style[0].style_name]);
}else{
throw new BaseError(status.MYPROFILE_PREFER_STYLE_LENGHT_ERROR);
}

if(recommend[0].length == 0){
conn.release();
return -1;
}else{
for (let i = 0; i < recommend[0].length; i++) {
const user = await pool.query(getUser, recommend[0][i].uuid);
const fit = await pool.query(getFitToUserId, recommend[0][i].uuid);
const style = await pool.query(getStyleToUserId, recommend[0][i].uuid);
result.push({user, fit, style});
}
}
conn.release();
return result;
} catch (err) {
if (err.data.code === status.MYPROFILE_PREFER_STYLE_LENGHT_ERROR.code) {
throw err;
}
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}
20 changes: 20 additions & 0 deletions src/domains/recommend/recommend.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,24 @@ export const getBodyInfoDTO = (data) => {
}
}
return {"userData": user};
}

export const getStyleDTO = (data) => {
const user = [];
if(data == -1){
user.push("해당 유저는 등록되어 있지 않아요.");
}else{
for (let i = 0; i < data.length; i++) {
user.push({
"user_id": data[i].user[0][0].uuid,
"nickname": data[i].user[0][0].nickname,
"user_image": data[i].user[0][0].img_url,
"height": data[i].user[0][0].height,
"weight": data[i].user[0][0].weight,
"prefer_fit": data[i].fit[0].map(fitItem => fitItem.pf_name),
"prefer_style": data[i].style[0].map(styleItem => styleItem.style_name)
})
}
}
return {"userData": user};
}
12 changes: 10 additions & 2 deletions src/domains/recommend/recommend.provider.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { getBodyInfoDTO } from "./recommend.dto.js"
import { getBodyInfoDAO, getBodyInfoAllDAO } from "./recommend.dao.js";
import { getBodyInfoDTO, getStyleDTO } from "./recommend.dto.js"
import { getBodyInfoDAO, getBodyInfoAllDAO, getStyleDAO, getStyleAllDAO } from "./recommend.dao.js";

export const getBodyInfo = async (userId) => {
return getBodyInfoDTO(await getBodyInfoDAO(userId));
}

export const getBodyInfoAll = async (userId) => {
return getBodyInfoDTO(await getBodyInfoAllDAO(userId));
}

export const getStyle = async (userId) => {
return getStyleDTO(await getStyleDAO(userId));
}

export const getStyleAll = async (userId) => {
return getStyleDTO(await getStyleAllDAO(userId));
}
31 changes: 29 additions & 2 deletions src/domains/recommend/recommend.sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,37 @@ export const findUserToFit =
+ "FROM user_fit uf "
+ "WHERE uf.pf_name = ? ;"


export const findUserAllToBody =
"SELECT b.uuid "
+ "FROM body_info b "
+ "WHERE NOT uuid = ? and height BETWEEN ?-5 AND ?+5 "
+ "AND weight BETWEEN ?-5 AND ?+5 "
+ "ORDER BY ABS(height-?) + ABS(weight-?) ASC LIMIT 24;"
+ "ORDER BY ABS(height-?) + ABS(weight-?) ASC LIMIT 24;"

export const findUserToStyle1 =
"SELECT DISTINCT uuid "
+ "FROM user_style "
+ "WHERE NOT uuid = ? "
+ "AND style_name = ? "
+ "ORDER BY uuid LIMIT 8;"

export const findUserToStyle2 =
"SELECT DISTINCT uuid "
+ "FROM user_style "
+ "WHERE NOT uuid = ? "
+ "AND (style_name = ? OR style_name = ?) "
+ "ORDER BY uuid LIMIT 8;"

export const findUserAllToStyle1 =
"SELECT DISTINCT uuid "
+ "FROM user_style "
+ "WHERE NOT uuid = ? "
+ "AND style_name = ? "
+ "ORDER BY uuid LIMIT 24;"

export const findUserAllToStyle2 =
"SELECT DISTINCT uuid "
+ "FROM user_style "
+ "WHERE NOT uuid = ? "
+ "AND (style_name = ? OR style_name = ?) "
+ "ORDER BY uuid LIMIT 24;"
10 changes: 8 additions & 2 deletions src/routes/recommend.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import express from "express";
import asyncHandler from 'express-async-handler';
import { LoginCheck } from "../middlewares/logincheck.js";
import { bodyInfo, bodyInfoAll } from "../domains/recommend/recommend.controller.js";
import { bodyInfo, bodyInfoAll, style, styleAll } from "../domains/recommend/recommend.controller.js";

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

//체형 추천
recommendRouter.get('/body_info', LoginCheck, asyncHandler(bodyInfo));

//체형 추천-모두 보기
recommendRouter.get('/body_info/all', LoginCheck, asyncHandler(bodyInfoAll));
recommendRouter.get('/body_info/all', LoginCheck, asyncHandler(bodyInfoAll));

//스타일 추천
recommendRouter.get('/style', LoginCheck, asyncHandler(style));

//스타일 추천-모두 보기
recommendRouter.get('/style/all', LoginCheck, asyncHandler(styleAll));
Loading

0 comments on commit 7abe3c4

Please sign in to comment.