Skip to content
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
93 changes: 93 additions & 0 deletions api-reference.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
```markdown
# API Reference

## Blog Posts API

### `GET /api/blog`

Fetch published blog posts with their authors.

#### Example Request

```http
GET /api/blog
```

#### Example Response

```json
[
{
"id": "clwlskj4a0000bqggs4zpuffq",
"title": "Hello World",
"content": "My first post.",
"published": true,
"authorId": "clwlsj3840000bqggsyj4mfyz",
"createdAt": "2024-06-20T12:00:00.000Z",
"updatedAt": "2024-06-20T12:00:00.000Z",
"author": {
"id": "clwlsj3840000bqggsyj4mfyz",
"name": "Jane Doe",
"email": "jane@example.com"
// ...other user fields
}
}
]
```

#### Errors

If there is an internal error while fetching posts:

```json
{
"message": "Error fetching posts"
}
```

Status: `500 Internal Server Error`

---

## Database Models

### `Post` Model

The `Post` model represents a blog post.

| Field | Type | Notes |
|------------|-----------|-----------------------------------------------|
| id | String | Primary key, autogenerated |
| title | String | Title of the post |
| content | String? | Post content, optional (`Text` in DB) |
| published | Boolean | `true` if post is published, default: `false` |
| author | User | Relation to the `User` (author) |
| authorId | String | Foreign key to `User` |
| createdAt | DateTime | Defaults to creation time |
| updatedAt | DateTime | Auto-updates on modification |

#### Prisma Schema Example

```prisma
model Post {
id String @id @default(cuid())
title String
content String? @db.Text
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
```

The `User` model now also has

```prisma
posts Post[]
```

to represent the one-to-many relationship between users and posts.

---
```