bluewhale2025 commited on
Commit
ffc6bd8
·
1 Parent(s): 6d25504

Fix TypeScript errors and update dependencies

Browse files
prisma/schema.prisma CHANGED
@@ -1,6 +1,6 @@
1
  datasource db {
2
  provider = "sqlite"
3
- url = env("DATABASE_URL")
4
  }
5
 
6
  generator client {
@@ -8,7 +8,7 @@ generator client {
8
  }
9
 
10
  model Post {
11
- id String @id @default(uuid())
12
  title String
13
  content String
14
  author String
 
1
  datasource db {
2
  provider = "sqlite"
3
+ url = "file:./dev.db"
4
  }
5
 
6
  generator client {
 
8
  }
9
 
10
  model Post {
11
+ id String @id @default(cuid())
12
  title String
13
  content String
14
  author String
src/graphql/resolvers.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { createPubSub } from 'graphql-yoga';
2
  import { getAllPosts, getPostById, createPost, updatePost, deletePost, Post } from '../lib/markdown';
3
  import { getCommitHistory, rollbackToCommit, getFileDiff } from '../lib/git';
4
  import { User, NFT, Contribution, ContributionType, ContributionStatus, LeaderboardEntry } from '../types/incentive';
5
- import { PrismaClient } from '@prisma/client';
6
  import { PubSub } from 'graphql-subscriptions';
7
 
8
  interface PostInput {
@@ -31,7 +31,6 @@ interface PubSubEvents {
31
  LEADERBOARD_UPDATED: { leaderboardUpdated: LeaderboardEntry[] };
32
  }
33
 
34
- const prisma = new PrismaClient();
35
  const pubsub = new PubSub<PubSubEvents>();
36
 
37
  const startPingInterval = () => {
@@ -88,9 +87,11 @@ const updateLeaderboard = (): LeaderboardEntry[] => {
88
 
89
  export const resolvers = {
90
  Query: {
91
- posts: () => posts,
92
- post: (_: any, { id }: { id: string }) => {
93
- return posts.find(post => post.id === id);
 
 
94
  },
95
  postsByTag: async (_: unknown, { tag }: { tag: string }) => {
96
  const posts = await getAllPosts();
@@ -101,12 +102,14 @@ export const resolvers = {
101
  return posts.filter(post => post.category === category);
102
  },
103
  searchPosts: async (_: unknown, { query }: { query: string }) => {
104
- const posts = await getAllPosts();
105
- const searchQuery = query.toLowerCase();
106
- return posts.filter(post =>
107
- post.title.toLowerCase().includes(searchQuery) ||
108
- post.content.toLowerCase().includes(searchQuery)
109
- );
 
 
110
  },
111
  commits: async () => {
112
  return await getCommitHistory();
@@ -132,37 +135,35 @@ export const resolvers = {
132
  },
133
 
134
  Mutation: {
135
- createPost: async (_: any, { input }: { input: Omit<Post, 'id'> }) => {
136
- const post: Post = {
137
- id: Math.random().toString().slice(2),
138
- ...input
139
- };
140
-
141
- posts.push(post);
142
- console.log('Published POST_CREATED event:', post);
143
- await pubsub.publish('POST_CREATED', { postCreated: post });
144
- return post;
145
  },
146
-
147
- updatePost: async (_: any, { id, input }: { id: string; input: Partial<Post> }) => {
148
- const index = posts.findIndex(post => post.id === id);
149
- if (index !== -1) {
150
- const post = { ...posts[index], ...input };
151
- posts[index] = post;
152
- await pubsub.publish('POST_UPDATED', { postUpdated: post });
153
  return post;
 
 
 
154
  }
155
- return null;
156
  },
157
-
158
- deletePost: async (_: any, { id }: { id: string }) => {
159
- const index = posts.findIndex(post => post.id === id);
160
- if (index !== -1) {
161
- posts.splice(index, 1);
162
- await pubsub.publish('POST_DELETED', { postDeleted: id });
163
- return id;
 
164
  }
165
- return null;
166
  },
167
  rollbackCommit: async (_: unknown, { commitHash }: { commitHash: string }) => {
168
  return await rollbackToCommit(commitHash);
 
1
+ import { createPubSub } from '@graphql-yoga/subscription';
2
  import { getAllPosts, getPostById, createPost, updatePost, deletePost, Post } from '../lib/markdown';
3
  import { getCommitHistory, rollbackToCommit, getFileDiff } from '../lib/git';
4
  import { User, NFT, Contribution, ContributionType, ContributionStatus, LeaderboardEntry } from '../types/incentive';
5
+ import { prisma } from '../lib/prisma';
6
  import { PubSub } from 'graphql-subscriptions';
7
 
8
  interface PostInput {
 
31
  LEADERBOARD_UPDATED: { leaderboardUpdated: LeaderboardEntry[] };
32
  }
33
 
 
34
  const pubsub = new PubSub<PubSubEvents>();
35
 
36
  const startPingInterval = () => {
 
87
 
88
  export const resolvers = {
89
  Query: {
90
+ posts: async () => {
91
+ return await prisma.post.findMany();
92
+ },
93
+ post: async (_: unknown, { id }: { id: string }) => {
94
+ return await getPostById(id);
95
  },
96
  postsByTag: async (_: unknown, { tag }: { tag: string }) => {
97
  const posts = await getAllPosts();
 
102
  return posts.filter(post => post.category === category);
103
  },
104
  searchPosts: async (_: unknown, { query }: { query: string }) => {
105
+ return await prisma.post.findMany({
106
+ where: {
107
+ OR: [
108
+ { title: { contains: query } },
109
+ { content: { contains: query } }
110
+ ]
111
+ }
112
+ });
113
  },
114
  commits: async () => {
115
  return await getCommitHistory();
 
135
  },
136
 
137
  Mutation: {
138
+ createPost: async (_: unknown, { input }: { input: PostInput }) => {
139
+ try {
140
+ const post = await createPost(input);
141
+ await pubsub.publish('POST_CREATED', post);
142
+ return post;
143
+ } catch (error) {
144
+ console.error('Error creating post:', error);
145
+ throw error;
146
+ }
 
147
  },
148
+ updatePost: async (_: unknown, { id, input }: { id: string; input: PostInput }) => {
149
+ try {
150
+ const post = await updatePost(id, input);
151
+ await pubsub.publish('POST_UPDATED', post);
 
 
 
152
  return post;
153
+ } catch (error) {
154
+ console.error('Error updating post:', error);
155
+ throw error;
156
  }
 
157
  },
158
+ deletePost: async (_: unknown, { id }: { id: string }) => {
159
+ try {
160
+ const post = await deletePost(id);
161
+ await pubsub.publish('POST_DELETED', post);
162
+ return post;
163
+ } catch (error) {
164
+ console.error('Error deleting post:', error);
165
+ throw error;
166
  }
 
167
  },
168
  rollbackCommit: async (_: unknown, { commitHash }: { commitHash: string }) => {
169
  return await rollbackToCommit(commitHash);
src/lib/posts.ts ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { prisma } from './prisma';
2
+ import { Post, PostInput } from '../types/post';
3
+
4
+ export async function getPostById(id: string): Promise<Post | null> {
5
+ return await prisma.post.findUnique({ where: { id } });
6
+ }
7
+
8
+ export async function createPost(input: PostInput): Promise<Post> {
9
+ return await prisma.post.create({
10
+ data: {
11
+ ...input,
12
+ createdAt: new Date(),
13
+ updatedAt: new Date()
14
+ }
15
+ });
16
+ }
17
+
18
+ export async function updatePost(id: string, input: PostInput): Promise<Post> {
19
+ return await prisma.post.update({
20
+ where: { id },
21
+ data: {
22
+ ...input,
23
+ updatedAt: new Date()
24
+ }
25
+ });
26
+ }
27
+
28
+ export async function deletePost(id: string): Promise<Post> {
29
+ return await prisma.post.delete({ where: { id } });
30
+ }
src/lib/prisma.ts ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import { PrismaClient } from '@prisma/client';
2
+
3
+ const prisma = new PrismaClient();
4
+
5
+ export { prisma };
src/types/post.ts CHANGED
@@ -8,4 +8,9 @@ export interface Post {
8
  category: string;
9
  createdAt: Date;
10
  updatedAt: Date;
 
 
 
 
 
11
  }
 
8
  category: string;
9
  createdAt: Date;
10
  updatedAt: Date;
11
+ }
12
+
13
+ export interface PostInput {
14
+ title: string;
15
+ content: string;
16
  }