CRUD API example is written in Go using net/http package and MySQL database.
GoCrudBook
├── LICENSE.md
├── cmd
│ └── web
│ ├── config
│ │ └── config.go
│ ├── database
│ │ └── connection.go
│ ├── handler
│ │ └── handler.go
│ ├── main.go
│ └── router
│ └── router.go
├── go.mod
├── go.sum
├── gopher-lib.jpeg
├── pkg
│ └── models
│ ├── book.go
│ └── mysql
│ ├── book.sql
│ └── mysql.go
└── to-do.md
- Clone the repository and move to the project directory:
git clone https://github.com/3n0ugh/GoCrudBook.git cd GoCrudBook
- To load the database
- To create a database into MySQL:
CREATE DATABASE library;
- Then, create a table and insert sample values into the database:
mysql -u root library < pkg/models/book.sql
- To create a database into MySQL:
- Tidy the modules
go mod tidy
- Run the main.go
go run ./cmd/web/main.go -port 5000 -dsn "root@/library"
- Get All Book
curl -X GET localhost:5000/book
- Get Book (by id)
curl -X GET "localhost:5000/book/id?id=1933988673"
- Get Book (by name) (enough to contain the word)
curl -X GET "localhost:5000/book/name?name=android"
- Add Book
curl -X POST localhost:5000/book/create -d
'{ "isbn" : 1932394161,
"book_name" : "Time To Go Forward",
"author": "3n0ugh",
"page_count" : 14,
"book_count": 1,
"borrow_times": 3,
"barrow_date": null,
"last_recieved_dat": null
}'
- Delete Book (by id)
curl -X DELETE "localhost:5000/book/delete?id=1932394161"
- Update Book
curl -X PUT localhost:5000/book/update -d
'{ "isbn" : 1932394161,
"book_name" : "Time To Go Forward",
"author": "3n0ugh",
"page_count" : 14,
"book_count": 1,
"borrow_times": 3,
"barrow_date": null,
"last_recieved_dat": null
}'