Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Simple search functionality #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,36 @@ const main = async () => {
res.json(data);
});

app.get("/text-stories/search/:cursor/:searchInput", async (req, res) => {
let cursor = 0;
const nCursor = parseInt(req.params.cursor);
if (!Number.isNaN(nCursor)) {
cursor = nCursor;
}

let searchInput = req.params.searchInput;
const limit = 21;
const stories = await getConnection().query(`
select
ts.id,
u.username "creatorUsername",
u."photoUrl" "creatorAvatarUrl",
u.flair
from text_story ts
inner join "user" u on u.id = ts."creatorId"
where u.username ILIKE %$1%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I tried making a request but the %$1% doesn't work for me. I think it's because of the missing apostrophes. Did you try it? @FluentCoding

order by (ts."numLikes"+1) / power(EXTRACT(EPOCH FROM current_timestamp-ts."createdAt")/3600,1.8) DESC
limit ${limit + 1}
${cursor ? `offset ${limit * cursor}` : ""}
`, [searchInput]);

const data = {
stories: stories.slice(0, limit),
hasMore: stories.length === limit + 1,
};
res.json(data);
});

app.post("/delete-text-story/:id", isAuth(), async (req: any, res) => {
const { id } = req.params;
if (!isUUID.v4(id)) {
Expand Down