-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mariatorrentedev/portfoli…
…o-api into master
- Loading branch information
Showing
17 changed files
with
221 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
-- CreateTable | ||
CREATE TABLE "BlogPost" ( | ||
"id" SERIAL NOT NULL, | ||
"title" TEXT NOT NULL, | ||
"content" TEXT NOT NULL, | ||
"authorId" INTEGER NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "BlogPost_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Subscriber" ( | ||
"id" SERIAL NOT NULL, | ||
"email" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "Subscriber_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Subscriber_email_key" ON "Subscriber"("email"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "BlogPost" ADD CONSTRAINT "BlogPost_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { Request, Response } from "express"; | ||
import { BlogService } from "../services/blog"; | ||
import { composeError } from "../utils"; | ||
|
||
export const createBlogPost = async (req: Request, res: Response) => { | ||
try { | ||
const { title, content, authorId } = req.body; | ||
const newBlogPost = await BlogService.createBlogPost({ | ||
title, | ||
content, | ||
authorId, | ||
}); | ||
res.status(201).json(newBlogPost); | ||
} catch (error) { | ||
res.status(500).json({ error: composeError(error) }); | ||
} | ||
}; | ||
|
||
export const getBlogPosts = async (req: Request, res: Response) => { | ||
try { | ||
const blogPosts = await BlogService.getBlogPosts(); | ||
res.json(blogPosts); | ||
} catch (error) { | ||
res.status(500).json({ error: composeError(error) }); | ||
} | ||
}; | ||
|
||
export const getBlogPostById = async (req: Request, res: Response) => { | ||
try { | ||
const postId = parseInt(req.params.id); | ||
const blogPost = await BlogService.getBlogPostById(postId); | ||
if (!blogPost) { | ||
res.status(404).json({ error: "Blog post not found" }); | ||
} else { | ||
res.json(blogPost); | ||
} | ||
} catch (error) { | ||
res.status(500).json({ error: composeError(error) }); | ||
} | ||
}; | ||
|
||
export const updateBlogPost = async (req: Request, res: Response) => { | ||
try { | ||
const postId = parseInt(req.params.id); | ||
const { title, content } = req.body; | ||
const updatedPost = await BlogService.updateBlogPost(postId, { | ||
title, | ||
content, | ||
}); | ||
res.json(updatedPost); | ||
} catch (error) { | ||
res.status(500).json({ error: composeError(error) }); | ||
} | ||
}; | ||
|
||
export const deleteBlogPost = async (req: Request, res: Response) => { | ||
try { | ||
const postId = parseInt(req.params.id); | ||
await BlogService.deleteBlogPost(postId); | ||
res.sendStatus(204); | ||
} catch (error) { | ||
res.status(500).json({ error: composeError(error) }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import express from "express"; | ||
import * as blogController from "../controllers/blog"; | ||
import { authenticateToken } from "../middleware/authToken"; | ||
|
||
const router = express.Router(); | ||
|
||
// Private routes | ||
router.post("/", authenticateToken, blogController.createBlogPost); | ||
router.put("/:id", authenticateToken, blogController.updateBlogPost); | ||
router.delete("/:id", authenticateToken, blogController.deleteBlogPost); | ||
|
||
// Public routes | ||
router.get("/", blogController.getBlogPosts); | ||
router.get("/:id", blogController.getBlogPostById); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { BlogPost } from "../types/blog"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export const BlogService = { | ||
async createBlogPost(blogPostData: Omit<BlogPost, "id">): Promise<BlogPost> { | ||
const newBlogPost = await prisma.blogPost.create({ data: blogPostData }); | ||
return newBlogPost; | ||
}, | ||
|
||
async getBlogPosts(): Promise<BlogPost[]> { | ||
const blogPosts = await prisma.blogPost.findMany(); | ||
return blogPosts; | ||
}, | ||
|
||
async getBlogPostById(id: number): Promise<BlogPost | null> { | ||
const blogPost = await prisma.blogPost.findUnique({ where: { id } }); | ||
return blogPost; | ||
}, | ||
|
||
async updateBlogPost( | ||
id: number, | ||
updatedData: Partial<BlogPost> | ||
): Promise<BlogPost> { | ||
const updatedPost = await prisma.blogPost.update({ | ||
where: { id }, | ||
data: updatedData, | ||
}); | ||
return updatedPost; | ||
}, | ||
|
||
async deleteBlogPost(id: number): Promise<void> { | ||
await prisma.blogPost.delete({ where: { id } }); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type BlogPost = { | ||
id: number; | ||
title: string; | ||
content: string; | ||
authorId: number; | ||
createdAt?: Date; | ||
updatedAt?: Date; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters