-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from lyve-app/feat/search
feat: added search route closes #107
- Loading branch information
Showing
6 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as userController from "./user.controller"; | ||
import * as streamController from "./stream.controller"; | ||
import * as searchController from "./search.controller"; | ||
|
||
export { userController, streamController }; | ||
export { userController, streamController, searchController }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import type { Request, Response } from "express"; | ||
import httpStatus from "http-status"; | ||
import prismaClient from "../config/prisma"; | ||
import { User } from "@prisma/client"; | ||
import { TypedResponse } from "../types/types"; | ||
import { createErrorObject } from "../utils/createErrorObject"; | ||
|
||
export const search = async ( | ||
req: Request< | ||
object, | ||
unknown, | ||
unknown, | ||
{ | ||
query: string; | ||
courser: string; | ||
limit: string; | ||
} | ||
>, | ||
res: Response< | ||
TypedResponse<{ | ||
result: { | ||
users: Pick< | ||
User, | ||
"id" | "username" | "dispname" | "avatar_url" | "followerCount" | ||
>[]; | ||
}; | ||
nextCursor: string; | ||
hasNext: boolean; | ||
}> | ||
> | ||
) => { | ||
const { query, courser, limit } = req.query; | ||
|
||
if (!query || !limit) { | ||
return res.status(httpStatus.BAD_REQUEST).json({ | ||
success: false, | ||
data: null, | ||
error: [ | ||
...createErrorObject( | ||
httpStatus.BAD_REQUEST, | ||
"query and limit must be defined" | ||
) | ||
] | ||
}); | ||
} | ||
|
||
// prisma query | ||
const userData = await prismaClient.user.findMany({ | ||
take: 30, | ||
...(courser && { | ||
skip: 1, // Do not include the cursor itself in the query result. | ||
cursor: { | ||
id: courser | ||
} | ||
}), | ||
where: { | ||
OR: [ | ||
{ | ||
dispname: { | ||
search: query | ||
} | ||
}, | ||
{ | ||
username: { | ||
search: query | ||
} | ||
} | ||
] | ||
}, | ||
select: { | ||
id: true, | ||
username: true, | ||
dispname: true, | ||
avatar_url: true, | ||
followerCount: true | ||
} | ||
}); | ||
|
||
const next = await prismaClient.user.findMany({ | ||
take: 30, | ||
|
||
skip: 1, | ||
cursor: { | ||
id: courser | ||
}, | ||
|
||
where: { | ||
OR: [ | ||
{ | ||
dispname: { | ||
search: query | ||
} | ||
}, | ||
{ | ||
username: { | ||
search: query | ||
} | ||
} | ||
] | ||
}, | ||
select: { | ||
id: true | ||
} | ||
}); | ||
|
||
return res.status(httpStatus.OK).json({ | ||
success: true, | ||
data: { | ||
result: { | ||
users: userData | ||
}, | ||
nextCursor: userData.at(-1)?.id ?? "", | ||
hasNext: next.length > 0 | ||
}, | ||
error: [] | ||
}); | ||
|
||
// return | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import userRouter from "./user.route"; | ||
import streamRouter from "./stream.route"; | ||
import searchRouter from "./search.route"; | ||
|
||
export { userRouter, streamRouter }; | ||
export { userRouter, streamRouter, searchRouter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Router } from "express"; | ||
import { searchController } from "src/controller"; | ||
|
||
const searchRouter = Router(); | ||
|
||
searchRouter.get("/", searchController.search); | ||
|
||
export default searchRouter; |