Skip to content
This repository was archived by the owner on Dec 9, 2021. It is now read-only.

(Backend) Added ticket searching #4

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions lib/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRepository } from "typeorm";
import { FindConditions, getRepository, Like } from "typeorm";
import { Tags } from "../src/db/entities/Tags";
import { TicketMessages } from "../src/db/entities/TicketMessages";
import { Tickets } from "../src/db/entities/Tickets";
Expand All @@ -25,7 +25,7 @@ const Query = {
},
tickets: async (
_parent,
{ take, skip, authorId },
{ take, skip, authorId, search },
_context,
{ fieldNodes },
) => {
Expand All @@ -34,7 +34,9 @@ const Query = {
fieldNodes.find((x) => x.name.value === "tickets").selectionSet
.selections,
);
const where = authorId ? { authorId } : {};
const where: FindConditions<Tickets> = {};
if (authorId) where.authorId = authorId;
if (search) where.title = Like(`%${search}%`);
return await getRepository(Tickets).find({
where,
take: Math.min(take, 50),
Expand Down Expand Up @@ -114,7 +116,9 @@ const Mutation = {
},
updateStatus: async (_parent, { input }, _context, _info) => {
await dbConnect();
const ticket = await getRepository(Tickets).findOne({where: {id: input.ticketId}});
const ticket = await getRepository(Tickets).findOne({
where: { id: input.ticketId },
});
if (!ticket) return null;
ticket.status = input.newStatus;
const newTicket = await getRepository(Tickets).save(ticket);
Expand Down
7 changes: 6 additions & 1 deletion lib/type-defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ export default gql`
hello: String!
users: [User!]!
ticket(id: ID!): Ticket
tickets(take: Int = 15, skip: Int = 0, authorId: Int): [Ticket!]!
tickets(
take: Int = 15
skip: Int = 0
authorId: Int
search: String
): [Ticket!]!
ticketsCount: Int!
tags: [Tag!]!
}
Expand Down
3 changes: 2 additions & 1 deletion utils/dbconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export const dbConnect = () => {
return connectionReadyPromise;
};

export const getConnectionString = () => `mysql://${process.env.NEXT_PUBLIC_DB_USERNAME}:${process.env.NEXT_PUBLIC_DB_PASSWORD}@${process.env.NEXT_PUBLIC_DB_ADDRESS}:${process.env.NEXT_PUBLIC_DB_PORT}/${process.env.NEXT_PUBLIC_DB_NAME}`;
export const getConnectionString = () =>
`mysql://${process.env.NEXT_PUBLIC_DB_USERNAME}:${process.env.NEXT_PUBLIC_DB_PASSWORD}@${process.env.NEXT_PUBLIC_DB_ADDRESS}:${process.env.NEXT_PUBLIC_DB_PORT}/${process.env.NEXT_PUBLIC_DB_NAME}`;