Skip to content

Commit

Permalink
Merge branch 'hngprojects:dev' into feat/super-admin-delete-an-organi…
Browse files Browse the repository at this point in the history
…sation
  • Loading branch information
Emeriego authored Jul 25, 2024
2 parents 31c23ea + 491b595 commit fe13e32
Show file tree
Hide file tree
Showing 5 changed files with 639 additions and 3 deletions.
173 changes: 173 additions & 0 deletions docs/blog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# Blog API Documentation

This project provides an API for managing blog posts, including creating, editing, deleting, and retrieving blog posts. The API is documented using Swagger.

## Getting Started

### Prerequisites

Make sure you have the following installed on your machine:

- Node.js (>=14.x)
- Yarn (>=1.x)

### Installation

1. Clone the repository:
```bash
git clone repo
cd repo
```

2. Install dependencies:
```bash
yarn install
```

### Running the Server

To start the server, run:
```bash
yarn start
```

## API Endpoints

### Authentication

Ensure that users are authenticated before accessing the endpoints that modify or delete resources.

#### Login
- **URL**: `/api/v1/auth/login`
- **Method**: `POST`
- **Request Body**:
```json
{
"username": "[email protected]",
"password": "password123"
}
```
- **Responses**:
- `200 OK`: Successfully authenticated
- `401 Unauthorized`: Invalid credentials

#### Register
- **URL**: `/api/v1/auth/register`
- **Method**: `POST`
- **Request Body**:
```json
{
"username": "[email protected]",
"password": "password123",
"name": "John Doe"
}
```
- **Responses**:
- `201 Created`: Successfully registered
- `400 Bad Request`: Invalid input

### Create a Blog Post

- **URL**: `/api/v1/blog/create`
- **Method**: `POST`
- **Authentication**: Required
- **Request Body**:
```json
{
"title": "Sample Blog Post",
"content": "This is a sample blog post.",
"author": "John Doe",
"Imageurl": "http://example.com/image.jpg",
"categories": ["Tech", "Programming"],
"tags": ["Node.js", "Express"],
"likes": [],
"comments": []
}
```
- **Responses**:
- `201 Created`: Blog post created successfully
- `400 Bad Request`: Invalid request body
- `401 Unauthorized`: Authentication required
- `500 Internal Server Error`: Server error

### Get All Blog Posts with Pagination

- **URL**: `/api/v1/blog`
- **Method**: `GET`
- **Query Parameters**:
- `page`: Page number (default: 1)
- `limit`: Number of posts per page (default: 10)
- `offset`: Number of posts to skip (default: 0)
- **Responses**:
- `200 OK`: List of blog posts
- `500 Internal Server Error`: Server error

### Get a Single Blog Post by ID

- **URL**: `/api/v1/blog/:id`
- **Method**: `GET`
- **Parameters**:
- `id`: Blog post ID
- **Responses**:
- `200 OK`: Blog post details
- `404 Not Found`: Blog post not found
- `500 Internal Server Error`: Server error

### Edit a Blog Post by ID

- **URL**: `/api/v1/blog/:id`
- **Method**: `PUT`
- **Authentication**: Required
- **Parameters**:
- `id`: Blog post ID
- **Request Body**:
```json
{
"title": "Updated Blog Post",
"content": "This is an updated blog post.",
"author": "John Doe",
"Imageurl": "http://example.com/new-image.jpg",
"categories": ["Tech", "Programming"],
"tags": ["Node.js", "Express"]
}
```
- **Responses**:
- `200 OK`: Blog post updated successfully
- `400 Bad Request`: Invalid request body
- `401 Unauthorized`: Authentication required
- `404 Not Found`: Blog post not found
- `500 Internal Server Error`: Server error

### Delete a Blog Post by ID

- **URL**: `/api/v1/blog/:id`
- **Method**: `DELETE`
- **Authentication**: Required
- **Parameters**:
- `id`: Blog post ID
- **Responses**:
- `204 No Content`: Blog post deleted successfully
- `401 Unauthorized`: Authentication required
- `404 Not Found`: Blog post not found
- `500 Internal Server Error`: Server error

## Project Structure

```
├── src
│ ├── models
│ │ └── blog.ts
│ ├── routes
│ │ └── blog.ts
│ ├── blogSwaggerConfig.ts
│ └── server.ts
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
```
54 changes: 52 additions & 2 deletions src/controllers/OrgController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export class OrgController {
* post:
* summary: Create a new organisation
* description: This endpoint allows a user to create a new organisation
* tags:
* - Organisation
* tags: [Organisations]
* operationId: createOrganisation
* security:
* - bearerAuth: []
* requestBody:
* description: Organisation payload
* required: true
Expand All @@ -30,8 +31,29 @@ export class OrgController {
* description:
* type: string
* example: This is a sample organisation.
* email:
* type: string
* example: [email protected]
* industry:
* type: string
* example: entertainment
* type:
* type: string
* example: music
* country:
* type: string
* example: Nigeria
* state:
* type: string
* example: Oyo
* required:
* - name
* - description
* - email
* - industry
* - type
* - country
* - state
* responses:
* '201':
* description: Organisation created successfully
Expand All @@ -58,6 +80,27 @@ export class OrgController {
* description:
* type: string
* example: This is a sample organisation.
* email:
* type: string
* example: [email protected]
* industry:
* type: string
* example: entertainment
* type:
* type: string
* example: music
* country:
* type: string
* example: Nigeria
* state:
* type: string
* example: Oyo
* slug:
* type: string
* example: 86820688-fd94-4b58-9bdd-99a701714a77
* owner_id:
* type: string
* example: 86820688-fd94-4b58-9bdd-99a701714a76
* createdAt:
* type: string
* format: date-time
Expand Down Expand Up @@ -99,7 +142,14 @@ export class OrgController {
* status_code:
* type: integer
* example: 500
* components:
* securitySchemes:
* bearerAuth:
* type: http
* scheme: bearer
* bearerFormat: JWT
*/

async createOrganisation(req: Request, res: Response, next: NextFunction) {
try {
const payload = req.body;
Expand Down
116 changes: 116 additions & 0 deletions src/routes/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,136 @@ import { updateBlogController } from "../controllers/updateBlogController";
const blogRouter = Router();
const blogController = new BlogController();

/**
* @swagger
* /api/v1/blog/create:
* post:
* summary: Create a blog post
* description: Allow user to create a blog post
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BlogInput'
* responses:
* '201':
* description: Blog post created successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Blog'
* '400':
* description: Bad request
* '500':
* description: Server error
*/

blogRouter.post("/create", authMiddleware, createBlogController);

/**
* @swagger
* /api/v1/blog:
* get:
* summary: Get all blog posts with pagination
* description: Allow user to get all blog posts with pagination (page, limit, offset)
* parameters:
* - name: page
* in: query
* schema:
* type: integer
* default: 1
* - name: limit
* in: query
* schema:
* type: integer
* default: 10
* - name: offset
* in: query
* schema:
* type: integer
* default: 0
* responses:
* '200':
* description: List of blog posts
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/Blog'
* '500':
* description: Server error
*/

blogRouter.get("/", blogController.listBlogs.bind(blogController));


blogRouter.get(
"/",
authMiddleware,
blogController.listBlogs.bind(blogController),
);

/**
* @swagger
* /api/v1/blog/{id}:
* put:
* summary: Edit a blog post by ID
* description: Allow an author to edit a blog post by ID (requires authentication)
* parameters:
* - name: id
* in: path
* required: true
* schema:
* type: string
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BlogInput'
* responses:
* '200':
* description: Blog post updated successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Blog'
* '400':
* description: Bad request
* '404':
* description: Blog post not found
* '500':
* description: Server error
*/
blogRouter.put("/:id", authMiddleware, updateBlogController);
blogRouter.get(
"/",
authMiddleware,
blogController.listBlogs.bind(blogController),
);

/**
* @swagger
* /api/v1/blog/{id}:
* delete:
* summary: Delete a blog post by ID
* description: Allow an author to delete a blog post by ID (requires authentication)
* parameters:
* - name: id
* in: path
* required: true
* schema:
* type: string
* responses:
* '204':
* description: Blog post deleted successfully
* '404':
* description: Blog post not found
* '500':
* description: Server error
*/
blogRouter.delete("/:id", blogController.deleteBlogPost.bind(blogController));

export { blogRouter };
Loading

0 comments on commit fe13e32

Please sign in to comment.