Skip to content

Commit b843297

Browse files
committed
a
1 parent 4327c75 commit b843297

File tree

7 files changed

+300
-52
lines changed

7 files changed

+300
-52
lines changed

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"next": "latest",
13+
"pg": "^8.13.1",
14+
"pg-hstore": "^2.3.4",
1215
"react": "latest",
1316
"react-dom": "latest",
14-
"next": "latest"
17+
"sequelize": "^6.37.5"
1518
},
1619
"devDependencies": {
17-
"typescript": "latest",
18-
"@types/react": "latest",
1920
"@types/node": "latest",
21+
"@types/react": "latest",
2022
"@types/react-dom": "latest",
2123
"eslint": "latest",
22-
"eslint-config-next": "latest"
24+
"eslint-config-next": "latest",
25+
"typescript": "latest"
2326
}
2427
}

src/app/layout.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { Metadata } from 'next'
22
import { Inter } from 'next/font/google'
3+
import sequelize from "./config/database";
4+
import "./models/Post"; // Garanta que o modelo Post foi importado
5+
6+
sequelize.sync({ force: false }).then(() => {
7+
console.log("Database synchronized");
8+
});
39

410
const inter = Inter({ subsets: ['latin'] })
511

src/app/lib/data.ts

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,32 @@
1-
import file from "../api/posts.json" assert { type: "json" };
2-
const fs = require("fs");
3-
const customer =
4-
{
5-
name: "d Co.",
6-
order_count: 0,
7-
address: "Po Box City",
8-
}
1+
import Post from "../models/Post";
92

10-
function saveJson(data:any){
11-
const jsonString = JSON.stringify(data);
12-
fs.writeFile("./src/app/api/posts.json", jsonString, (err:any) => {
13-
if (err) {
14-
console.log("Error writing file", err);
15-
} else {
16-
console.log("Successfully wrote file");
17-
}
18-
});
19-
}
20-
21-
22-
23-
type Post = {
24-
id: string;
25-
title: string;
26-
desc: string;
27-
date: any;
3+
export const getPosts = async () => {
4+
return await Post.findAll();
285
};
296

30-
let posts: Post[] = file;
31-
32-
export const getPosts = () => posts;
33-
export const addPost = (post: Post) => {
34-
posts.push(post);
35-
saveJson(posts)
36-
};
37-
export const deletePost = (id: string) => {
38-
posts = posts.filter((post) => post.id !== id);
39-
saveJson(posts)
7+
export const addPost = async (post: { id: string; title: string; desc: string; date: Date }) => {
8+
await Post.create(post);
409
};
41-
export const updatePost = (id: string, title: string, desc: string) => {
42-
const post = posts.find((post) => post.id === id);
43-
4410

45-
if (post) {
46-
post.title = title;
47-
post.desc = desc;
48-
saveJson(posts)
49-
} else {
50-
throw new Error("NO POST FOUND");
51-
}
11+
export const deletePost = async (id: string) => {
12+
const deleted = await Post.destroy({
13+
where: { id },
14+
});
15+
if (deleted === 0) throw new Error("NO POST FOUND");
5216
};
5317

54-
export const getById = (id: string) => {
55-
return posts.find((post) => post.id === id);
18+
export const updatePost = async (id: string, title: string, desc: string) => {
19+
const [updated] = await Post.update(
20+
{ title, desc },
21+
{
22+
where: { id },
23+
}
24+
);
25+
if (updated === 0) throw new Error("NO POST FOUND");
5626
};
27+
28+
export const getById = async (id: string) => {
29+
const post = await Post.findByPk(id);
30+
if (!post) throw new Error("NO POST FOUND");
31+
return post;
32+
};

src/app/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default function Home() {
44
return (
55
<main>
66
<p>My Api</p>
7+
<a href='/api/blog'>Blog</a>
78
</main>
89
)
910
}

src/config/database.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { Sequelize } = require('sequelize');
2+
3+
const sequelize = new Sequelize('database', 'username', 'password', {
4+
host: 'localhost',
5+
dialect: 'postgres',
6+
});
7+
8+
(async () => {
9+
try {
10+
await sequelize.authenticate();
11+
console.log('Connection has been established successfully.');
12+
} catch (error) {
13+
console.error('Unable to connect to the database:', error);
14+
}
15+
})();

src/models/Post.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { DataTypes, Model } from "sequelize";
2+
import sequelize from "../config/database";
3+
4+
class Post extends Model {
5+
public id!: string;
6+
public title!: string;
7+
public desc!: string;
8+
public date!: Date;
9+
}
10+
11+
Post.init(
12+
{
13+
id: {
14+
type: DataTypes.STRING,
15+
primaryKey: true,
16+
},
17+
title: {
18+
type: DataTypes.STRING,
19+
allowNull: false,
20+
},
21+
desc: {
22+
type: DataTypes.TEXT,
23+
allowNull: false,
24+
},
25+
date: {
26+
type: DataTypes.DATE,
27+
allowNull: false,
28+
},
29+
},
30+
{
31+
sequelize,
32+
modelName: "Post",
33+
tableName: "posts",
34+
}
35+
);
36+
37+
export default Post;

0 commit comments

Comments
 (0)