From e94a033e39756e79c4156d72fd22f4c541302496 Mon Sep 17 00:00:00 2001 From: "Alex.exe" <40430040+NfoAlex@users.noreply.github.com> Date: Sun, 26 May 2024 14:19:51 +0900 Subject: [PATCH] =?UTF-8?q?[change]=20-=20=E3=83=A6=E3=83=BC=E3=82=B6?= =?UTF-8?q?=E3=83=BC=E6=A4=9C=E7=B4=A2=E7=94=A8=E3=83=8F=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=83=A9=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=AE=E3=83=81=E3=83=A3?= =?UTF-8?q?=E3=83=B3=E3=83=8D=E3=83=AB=E5=8F=82=E5=8A=A0=E8=80=85=E9=99=90?= =?UTF-8?q?=E5=AE=9A=E3=81=A7=E6=A4=9C=E7=B4=A2=E3=81=A7=E3=81=8D=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=20(#38)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit あとSQL構文直した --- src/actionHandler/User/searchUser.ts | 60 ++++++++++++++++++---------- src/socketHandler/User.ts | 11 ++++- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/actionHandler/User/searchUser.ts b/src/actionHandler/User/searchUser.ts index 6f2fddb2..171b0515 100644 --- a/src/actionHandler/User/searchUser.ts +++ b/src/actionHandler/User/searchUser.ts @@ -3,25 +3,44 @@ const db = new sqlite3.Database("./records/USER.db"); import { IUserInfo } from "../../type/User"; -export default async function searchUser(userName:string, rule:"FULL"|"PARTIAL") -:Promise { +export default async function searchUser( + userName: string, + rule: "FULL"|"PARTIAL", + channelId?: string +):Promise { return new Promise((resolve) => { - if (rule === "PARTIAL") { //部分検索 - //検索用クエリー - const searchQuery = "%" + userName + "%"; - //ユーザー名でクエリが含まれるものを取得 - db.all("SELECT * FROM USERS_INFO LIKE userName = ?", [searchQuery], (err:Error, datUser:IUserInfo[]) => { - if (err) { - console.log("searchUser :: ERROR ->", err); - resolve([]); - } else { - //console.log("searchUser :: 検索結果->", userName, datUser); - resolve(datUser); - } - }); - } else { //完全検索 - //ユーザー名でクエリが含まれるものを取得 - db.all("SELECT * FROM USERS_INFO WHERE userName = ?", [userName], (err:Error, datUser:IUserInfo[]) => { + //チャンネルIdの指定があるかどうかでSQL文へ追加する文構成 + const optionChannel = + channelId!==undefined + ? + " AND channelJoined LIKE '%" + channelId + "%'" + : + ""; + + + //検索用クエリー + const searchQuery = + rule==='PARTIAL' + ? + "%" + userName + "%" //部分検索 + : + userName; //完全検索 + + console.log("searchUser :: sql文 ->", + ` + SELECT * FROM USERS_INFO + WHERE userName LIKE + ` + searchQuery + optionChannel + ); + + //ユーザー名でクエリが含まれるものを取得 + db.all( + ` + SELECT * FROM USERS_INFO + WHERE userName LIKE ? + ` + optionChannel, + [searchQuery], + (err:Error, datUser:IUserInfo[]) => { if (err) { console.log("searchUser :: ERROR ->", err); resolve([]); @@ -29,7 +48,8 @@ export default async function searchUser(userName:string, rule:"FULL"|"PARTIAL") //console.log("searchUser :: 検索結果->", userName, datUser); resolve(datUser); } - }); - } + } + ); + }); } diff --git a/src/socketHandler/User.ts b/src/socketHandler/User.ts index d9895039..37b1d8d9 100644 --- a/src/socketHandler/User.ts +++ b/src/socketHandler/User.ts @@ -132,7 +132,12 @@ module.exports = (io:Server) => { //ユーザー名で検索して一括取得 socket.on("searchUserInfo", async ( - dat:{RequestSender:IRequestSender, userName:string, rule:"FULL"|"PARTIAL"} + dat: { + RequestSender: IRequestSender, + userName: string, //検索文字列 + rule: "FULL"|"PARTIAL" //全文検索か部分検索か + channelId?: string //チャンネル限定か、それならチャンネルId + } ) => { console.log("User :: searchUserInfo : data->", dat); //セッション確認 @@ -143,7 +148,9 @@ module.exports = (io:Server) => { try { //情報検索、取得 - const userInfos = await searchUser(dat.userName, dat.rule); + const userInfos = await searchUser( + dat.userName, dat.rule, dat.channelId + ); socket.emit("RESULT::searchUserInfo", { result:"SUCCESS", data:userInfos }) } catch(e) {