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

Update postController.ts #1150

Open
wants to merge 1 commit 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
197 changes: 80 additions & 117 deletions week-11/solution/src/controller/postController.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';

import { Context } from 'hono';

enum StatusCode {
BADREQ = 400,
NOTFOUND = 404,
NOTPERMISSIOON = 403,
NOTPERMISSION = 403,
}

export async function getPosts(c: Context) {
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());
const prisma = new PrismaClient({
datasourceUrl: process.env.DATABASE_URL,
}).$extends(withAccelerate());

// Utility function to handle errors
const handleError = (c: Context, error: any, statusCode: number = 500) => {
console.error(error); // Log the error for debugging
return c.json({ message: 'Internal server error', error: error.message }, statusCode);
};

// Utility function to parse tags
const parseTags = (tags: string) => {
return tags.split(',').map(tag => tag.trim()).filter(Boolean);
};

// Get all posts
export async function getPosts(c: Context) {
try {
const response = await prisma.posts.findMany({
include: {
Expand All @@ -22,7 +33,7 @@ export async function getPosts(c: Context) {
},
});
return c.json({
post: response.map((res) => ({
post: response.map(res => ({
id: res.id,
username: res.User.username,
userId: res.User.id,
Expand All @@ -33,159 +44,120 @@ export async function getPosts(c: Context) {
})),
});
} catch (error) {
return c.body(`Internal server error: ${error}`, 500);
return handleError(c, error);
}
}

// Get posts by user
export async function getUserPosts(c: Context) {
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());

try {
const resp = await prisma.posts.findMany({
where: {
userId: c.get('userId'),
},
});
return c.json({
post: resp,
});
const userId = c.get('userId');
const resp = await prisma.posts.findMany({ where: { userId } });
return c.json({ post: resp });
} catch (error) {
return c.body(`Internal server error: ${error}`, 500);
return handleError(c, error);
}
}

// Create a new post
export async function createPost(c: Context) {
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());

try {
const body: {
title: string;
body: string;
tags: string;
} = await c.req.json();

const tagNames = body.tags.split(',').map((tag) => tag.trim());
const { title, body, tags } = await c.req.json();
const userId = c.get('userId');

if ((body.body && body.title) == null) {
if (!title || !body) {
return c.body('Invalid user input', StatusCode.BADREQ);
}

const tagNames = parseTags(tags);
const res = await prisma.posts.create({
data: {
title: body.title,
body: body.body,
userId: c.get('userId'),
title,
body,
userId,
tags: {
connectOrCreate: tagNames.map((tag) => ({
connectOrCreate: tagNames.map(tag => ({
where: { tag },
create: { tag },
})),
},
},
include: {
tags: true,
},
include: { tags: true },
});

return c.json({
message: 'Post successfully',
message: 'Post created successfully',
post: {
id: res.id,
title: res.title,
body: res.body,
tags: res.tags.map((tag) => tag.tag),
tags: res.tags.map(tag => tag.tag),
createdAt: res.createdAt,
},
});
} catch (error) {
return c.body(`Internal server error: ${error}`, 500);
return handleError(c, error);
}
}

// Get a specific post
export async function getPost(c: Context) {
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());

try {
const id: number = Number(c.req.param('id'));
const id = Number(c.req.param('id'));
const userId = c.get('userId');

const isPostExist = await prisma.posts.findFirst({
where: {
id: id,
userId: c.get('userId'),
},
include: {
tags: true,
},
const post = await prisma.posts.findFirst({
where: { id, userId },
include: { tags: true },
});

if (isPostExist == null) {
return c.body('Post does not exists', StatusCode.NOTFOUND);
if (!post) {
return c.body('Post does not exist', StatusCode.NOTFOUND);
}

return c.json({
data: {
id: isPostExist.id,
title: isPostExist.title,
body: isPostExist.body,
tags: isPostExist.tags,
createdAt: isPostExist.createdAt,
id: post.id,
title: post.title,
body: post.body,
tags: post.tags,
createdAt: post.createdAt,
},
});
} catch (error) {
return c.body(`Internal server error: ${error}`, 500);
return handleError(c, error);
}
}

// this controller update the specific post
// Update a specific post
export async function updatePost(c: Context) {
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());

try {
const id: number = Number(c.req.param('id'));
const id = Number(c.req.param('id'));
const { title, body, tags } = await c.req.json();
const userId = c.get('userId');

const body: {
title: string;
body: string;
tags: string;
} = await c.req.json();

const tagNames = body.tags.split(',').map((tag) => tag.trim());

const isPostExist = await prisma.posts.findFirst({
where: {
id: id,
userId: c.get('userId'),
},
const post = await prisma.posts.findFirst({
where: { id, userId },
});

if (isPostExist == null) {
return c.body('Post does not exists', StatusCode.NOTFOUND);
if (!post) {
return c.body('Post does not exist', StatusCode.NOTFOUND);
}

const tagNames = parseTags(tags);
const res = await prisma.posts.update({
where: {
id: id,
userId: c.get('userId'),
},
where: { id, userId },
data: {
title: body.title,
body: body.body,
title,
body,
tags: {
connectOrCreate: tagNames.map((tag) => ({
connectOrCreate: tagNames.map(tag => ({
where: { tag },
create: { tag },
})),
},
},
include: {
tags: true,
},
include: { tags: true },
});

return c.json({
Expand All @@ -198,39 +170,30 @@ export async function updatePost(c: Context) {
},
});
} catch (error) {
return c.body(`Internal server error: ${error}`, 500);
return handleError(c, error);
}
}

// Delete a specific post
export async function deletePost(c: Context) {
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());

try {
const id: number = Number(c.req.param('id'));
const id = Number(c.req.param('id'));
const userId = c.get('userId');

const isPostExist = await prisma.posts.findFirst({
where: {
id: id,
userId: c.get('userId'),
},
const post = await prisma.posts.findFirst({
where: { id, userId },
});

if (isPostExist == null) {
return c.body('Post does not exists', StatusCode.NOTFOUND);
if (!post) {
return c.body('Post does not exist', StatusCode.NOTFOUND);
}

const res = await prisma.posts.delete({
where: {
id: id,
userId: c.get('userId'),
},
});
return c.json({
message: 'post deleted',
await prisma.posts.delete({
where: { id, userId },
});

return c.json({ message: 'Post deleted successfully' });
} catch (error) {
return c.json({ msg: `Internal server error: ${error}` }, 500);
return handleError(c, error);
}
}