Skip to content

Commit

Permalink
Tag.taggedVideosByOffsetの実装
Browse files Browse the repository at this point in the history
Fixes #963
  • Loading branch information
SnO2WMaN committed Nov 28, 2023
1 parent 0912aa5 commit fdadac7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Tag/Tag.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ type Tag implements Node {

orderBy: TagTaggedVideosOrderBy! = { createdAt: DESC }
): VideoTagConnection!

"offsetベースでタグ付けされた動画を取得する"
taggedVideosByOffset(input: TaggedVideosByOffsetInput!): TagTaggedVideosByOffsetPayload!
}

input TaggedVideosByOffsetInput {
offset: Int!
take: Int!
orderBy: TagTaggedVideosOrderBy! = { createdAt: DESC }
}

type TagTaggedVideosByOffsetPayload {
nodes: [VideoTag!]!
hasMore: Boolean!
}

enum TagType {
Expand Down
17 changes: 16 additions & 1 deletion src/Tag/Tag.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const resolveTagType = ({ prisma }: Pick<ResolverDeps, "prisma">) =>
}
}) satisfies TagResolvers["type"];

export const resolveTag = ({ prisma, logger }: Pick<ResolverDeps, "prisma" | "logger">) =>
export const resolveTag = ({ prisma, logger, TagsService }: Pick<ResolverDeps, "prisma" | "logger" | "TagsService">) =>
({
id: ({ id }): string => buildGqlId("Tag", id),
type: resolveTagType({ prisma }),
Expand Down Expand Up @@ -218,6 +218,21 @@ export const resolveTag = ({ prisma, logger }: Pick<ResolverDeps, "prisma" | "lo

taggedVideos: resolveTaggedVideos({ prisma, logger }),

taggedVideosByOffset: async ({ id: tagId }, { input: { offset, take, orderBy: unparsedOrderBy } }, _ctx, info) => {
const orderBy = parseOrderBy(unparsedOrderBy);
if (isErr(orderBy)) {
logger.error({ path: info.path, args: unparsedOrderBy }, "OrderBy args error");
throw new GraphQLError("Wrong args");
}

const { hasMore, nodes } = await TagsService.taggedVideosByOffset(tagId, {
offset,
take,
orderBy: orderBy.data,
});
return { hasMore, nodes };
},

canTagTo: async ({ id: tagId }, { videoId: videoGqlId }) =>
prisma.videoTag
.findUnique({ where: { videoId_tagId: { tagId, videoId: parseGqlID("Video", videoGqlId) } } })
Expand Down
15 changes: 15 additions & 0 deletions src/Tag/Tag.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { PrismaClient } from "@prisma/client";

import { VideoTagDTO } from "../Video/dto.js";

export const mkTagService = ({ prisma }: { prisma: PrismaClient }) => {
return {
countAll() {
return prisma.tag.count();
},
async taggedVideosByOffset(
tagId: string,
{ offset, take, orderBy }: { offset: number; take: number; orderBy: { createdAt?: "desc" | "asc" } },
) {
const [count, nodes] = await prisma.$transaction([
prisma.videoTag.count({ where: { tagId } }),
prisma.videoTag.findMany({ where: { tagId }, orderBy, skip: offset, take }),
]);
return {
hasMore: offset + take < count,
nodes: nodes.map((v) => new VideoTagDTO(v)),
};
},
};
};

Expand Down

0 comments on commit fdadac7

Please sign in to comment.