diff --git a/lib/resolvers.ts b/lib/resolvers.ts index 7d67b9f..3a49169 100644 --- a/lib/resolvers.ts +++ b/lib/resolvers.ts @@ -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"; @@ -25,7 +25,7 @@ const Query = { }, tickets: async ( _parent, - { take, skip, authorId }, + { take, skip, authorId, search }, _context, { fieldNodes }, ) => { @@ -34,7 +34,9 @@ const Query = { fieldNodes.find((x) => x.name.value === "tickets").selectionSet .selections, ); - const where = authorId ? { authorId } : {}; + const where: FindConditions = {}; + if (authorId) where.authorId = authorId; + if (search) where.title = Like(`%${search}%`); return await getRepository(Tickets).find({ where, take: Math.min(take, 50), @@ -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); diff --git a/lib/type-defs.ts b/lib/type-defs.ts index 47db185..1c5b5b2 100644 --- a/lib/type-defs.ts +++ b/lib/type-defs.ts @@ -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!]! } diff --git a/utils/dbconnect.ts b/utils/dbconnect.ts index e481ea4..faa8e45 100644 --- a/utils/dbconnect.ts +++ b/utils/dbconnect.ts @@ -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}`;