A simple application RESTful API with Golang.
- Mysql database mysql connection
- Golang installed
This Friends Management API service is a RESTful web API built on Golang, HTTP, GORM, Mysql and Unit Test with 90% statement coverage.
Project have 5 packages:
- main.go: Start server and routing.
- configs: Define constant.
- controllers: Handle request from client.
- models: Define structures models related to database table schema.
- repositories: Process logic business and actions query, update database.
- utils: Common functions: Db connect, Response json.
Application config in the src/configs/Config.example. Copy it to Config.go. There are some constants:
// Application Database
const DB_NAME = "friend_managerment"
// Testing only Database name
const DB_NAME_TEST = "friend_managerment_test"
const DB_USER = "root"
const DB_PASSWORD = "123456"
const DB_HOST = "localhost"
const DB_DRIVER = "mysql"
// Application port
const APP_PORT = "8000"
At the root of this project and run the following command:
go get -u github.com/go-sql-driver/mysql
go get -u github.com/jinzhu/gorm
go get gopkg.in/go-playground/validator.v9
To Start project:
cd /path/to/project
# Run run full test
go run main.go
To run unitest:
cd /path/to/project/src/repositories
# Run run full test
go test
# Run test with coverage
go test -cover
Uri:
/api/user/create_friend
Method: POST
The API should receive the following JSON request:
{
"friends":
[
"[email protected]",
"[email protected]"
]
}
The Api response: On success: HttpStatus = 200
{
"success": true
}
On error: HttpStatus = 400
{
"message": "Target [email protected] Blocked Requestor [email protected]",
"success": false
}
Uri:
/api/user/get_friend
The API should receive the following JSON request:
{
"email": "[email protected]"
}
The Api response: On success: HttpStatus = 200
{
"count": 2,
"friends": [
"[email protected]",
"[email protected]"
],
"success": true
}
On error: HttpStatus = 400
{
"message": "User [email protected] not exits",
"success": false
}
Uri:
/api/user/get_friend_common
The API should receive the following JSON request:
{
"count": 2,
"friends": [
"[email protected]",
"[email protected]"
],
"success": true
}
The Api response: On success: HttpStatus = 200
{
"success": true,
"friends" :
[
"[email protected]"
],
"count" : 1
}
On error: HttpStatus = 400
{
"message": "User [email protected] not exits",
"success": false
}
Uri:
/api/user/subscribe
The API should receive the following JSON request:
{
"requestor": "[email protected]",
"target": "[email protected]"
}
On success: HttpStatus = 200
{
"success": true
}
On error: HttpStatus = 400
{
"message": "User [email protected] not exits",
"success": false
}
Uri:
/api/user/block
Suppose "[email protected]" blocks "[email protected]": The API should receive the following JSON request:
{
"requestor": "[email protected]",
"target": "[email protected]"
}
On success: HttpStatus = 200
{
"success": true
}
On error: HttpStatus = 400
{
"message": "User [email protected] not exits",
"success": false
}
6. As a user, I need an API to retrieve all email addresses that can receive updates from an email address.
Uri:
/api/post/create
The API should receive the following JSON request:
{
"sender": "[email protected]",
"text": "Hello World! [email protected]"
}
On success: HttpStatus = 200
{
"success": true,
"recipients":
[
"[email protected]",
"[email protected]"
]
}
On error: HttpStatus = 400
{
"message": "User [email protected] not exits",
"success": false
}
Uri:
/api/user/register
The API should receive the following JSON request:
{
"email": "[email protected]"
}
On success: HttpStatus = 200
{
"success": true
}
On error: HttpStatus = 400
{
"message": "Email [email protected] is exits, please use other email",
"success": false
}