Skip to content

Commit

Permalink
Merge pull request #108 from lyve-app/feat/search
Browse files Browse the repository at this point in the history
feat: added search route closes #107
  • Loading branch information
Louis3797 authored Jun 11, 2024
2 parents 978d2be + 8620375 commit cc0b364
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 4 deletions.
3 changes: 2 additions & 1 deletion apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch"]
}

datasource db {
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { errorHandler } from "./middleware/errorHandler";
import config from "./config/config";
import { xssMiddleware } from "./middleware/xssMiddleware";
import { createServer } from "http";
import { streamRouter, userRouter } from "./routes";
import { searchRouter, streamRouter, userRouter } from "./routes";
import { Server } from "socket.io";
import logger from "./middleware/logger";
import {
Expand Down Expand Up @@ -132,6 +132,8 @@ app.use("/api/user", userRouter);

app.use("/api/stream", streamRouter);

app.use("/api/search", searchRouter);

io.use(async (socket, next) => {
const { token } = socket.handshake.auth;

Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/controller/index.ts
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 };
119 changes: 119 additions & 0 deletions apps/api/src/controller/search.controller.ts
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
};
3 changes: 2 additions & 1 deletion apps/api/src/routes/index.ts
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 };
8 changes: 8 additions & 0 deletions apps/api/src/routes/search.route.ts
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;

0 comments on commit cc0b364

Please sign in to comment.