Skip to content

Commit

Permalink
[change] - ユーザー検索用ハンドラを指定のチャンネル参加者限定で検索できるように (#38)
Browse files Browse the repository at this point in the history
あとSQL構文直した
  • Loading branch information
NfoAlex authored May 26, 2024
1 parent f500d80 commit e94a033
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
60 changes: 40 additions & 20 deletions src/actionHandler/User/searchUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,53 @@ const db = new sqlite3.Database("./records/USER.db");

import { IUserInfo } from "../../type/User";

export default async function searchUser(userName:string, rule:"FULL"|"PARTIAL")
:Promise<IUserInfo[]> {
export default async function searchUser(
userName: string,
rule: "FULL"|"PARTIAL",
channelId?: string
):Promise<IUserInfo[]> {
return new Promise<IUserInfo[]>((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([]);
} else {
//console.log("searchUser :: 検索結果->", userName, datUser);
resolve(datUser);
}
});
}
}
);

});
}
11 changes: 9 additions & 2 deletions src/socketHandler/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//セッション確認
Expand All @@ -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) {
Expand Down

0 comments on commit e94a033

Please sign in to comment.