This repository contains code for a RESTful API built in Go, designed for user, post, and comment management. It provides APIs to create, retrieve, update, and delete users, posts, and comments.
- Clone the Repository
git clone https://github.com/zaahidali/Learn-go-language.git
- Navigate to the directory
cd blog_post_orm_system
- Install dependencies
Before you can run this project, you must have the following software installed on your system:
- Go (version 1.15 or later)
- PostgreSQL
Ensure you've installed the required dependencies for this project:
go mod tidy
- Set up database
Make sure that a PostgreSQL server is running and accessible through the given connection string. The database named "blog_posts_orm" should exist. If it doesn't, you can create one:
psql -U postgres -c 'create database blog_posts_orm;'
- Install go-migrate
# Install the migrate library
go get -u github.com/golang-migrate/migrate/v4
# Install the migrate command line tool
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
# Make sure the bin directory of your GOPATH is in your system's PATH
echo 'export PATH=$PATH:$(go env GOPATH)/bin' >> ~/.bashrc
# Source the bashrc file to load the new PATH into your current shell session
source ~/.bashrc
- Run migrations
With the migrate
tool installed, you can now run migrations to set up your database schema.
make migrate-up
To run the server, use the following command:
make run
The Makefile
includes several commands:
make run
: runs the applicationmake migrate-up
: applies all up migrationsmake migrate-down
: rolls back all migrationsmake create-migration <name>
: creates a new migration with the specified namemake remove-migration <migration>
: removes a specified migration file.
POST /users
: Create a new user. Accept a JSON body with the user's name and email.GET /users
: Get a list of all users.GET /users/:id
: Get the details of a specific user.PUT /users/:id
: Update a specific user. Accept a JSON body with the new details of the user.DELETE /users/:id
: Delete a specific user.
POST /posts
: Create a new post. Accept a JSON body with the post's title, content, and user_id.GET /posts
: Get a list of all posts.GET /posts/:id
: Get the details of a specific post.PUT /posts/:id
: Update a specific post. Accept a JSON body with the new details of the post.DELETE /posts/:id
: Delete a specific post.
POST /comments
: Create a new comment. Accept a JSON body with the comment's content, user_id, and post_id.GET /comments
: Get a list of all comments.GET /comments/:id
: Get the details of a specific comment.PUT /comments/:id
: Update a specific comment. Accept a JSON body with the new details of the comment.DELETE /comments/:id
: Delete a specific comment.
When developing, you might want to use some helpful commands. Please refer to the Makefile section for further information.
If you encounter any issues while setting up or running this project, please file an issue on this repository.