Skip to content

Commit

Permalink
feat: userId 추가 #84
Browse files Browse the repository at this point in the history
  • Loading branch information
seoyeoneel02 committed Oct 26, 2024
1 parent eaa115b commit b901e45
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/domains/search/search.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { searchService } from "./search.service.js";

export const searchController = async (req, res, next) => {
console.log("수업을 검색합니다!");
res.send(response(status.SUCCESS, await searchService(req.query)));
const userId = req.decoded.userId;
res.send(response(status.SUCCESS, await searchService(userId, req.query)));
}
6 changes: 3 additions & 3 deletions src/domains/search/search.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { status } from "../../response.status.js";
import { pool } from "../../db.config.js";
import { searchSql, searchNameSql } from "./search.sql.js";

export const searchRepo = async (type, name) => {
export const searchRepo = async (userId, type, name) => {
const conn = await pool.getConnection();
try {
if(typeof type == "undefined") {
throw new BaseError(status.PARAMETER_IS_WRONG);
}
if(typeof name == "undefined"){
const major = await pool.query(searchSql, type);
const major = await pool.query(searchSql, [userId, type]);
conn.release();
return major;
}else{
const major = await pool.query(searchNameSql, [type, name]);
const major = await pool.query(searchNameSql, [userId, type, name]);
conn.release();
return major;
}
Expand Down
4 changes: 2 additions & 2 deletions src/domains/search/search.service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { searchDTO } from "./search.dto.js";
import { searchRepo } from "./search.repository.js";

export const searchService = async (query) => {
export const searchService = async (userId, query) => {
const { type, name } = query;
const major = await searchRepo(type, name);
const major = await searchRepo(userId, type, name);
return searchDTO(major);
}
8 changes: 4 additions & 4 deletions src/domains/search/search.sql.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export const searchSql =
"SELECT credit, name, CASE WHEN us.user_id IS NOT NULL THEN 1 ELSE 0 END AS bookmark FROM subject s "
+ "JOIN user_subject us ON s.name = us.subject_name "
"SELECT s.id, credit, name, CASE WHEN us.user_id IS NOT NULL THEN 1 ELSE 0 END AS bookmark FROM subject s "
+ "JOIN user_subject us ON s.name = us.subject_name AND user_id = ? "
+ "WHERE type = ? ORDER BY s.id;"

export const searchNameSql =
"SELECT credit, name, CASE WHEN us.user_id IS NOT NULL THEN 1 ELSE 0 END AS bookmark FROM subject s "
+ "JOIN user_subject us ON s.name = us.subject_name "
"SELECT s.id, credit, name, CASE WHEN us.user_id IS NOT NULL THEN 1 ELSE 0 END AS bookmark FROM subject s "
+ "JOIN user_subject us ON s.name = us.subject_name AND user_id = ? "
+ "WHERE type = ? AND name REGEXP ? ORDER BY s.id;"

0 comments on commit b901e45

Please sign in to comment.