This is a simple backend application for managing a bookstore. It provides basic CRUD (Create, Read, Update, Delete) operations on the Book struct using Go and MySQL.
- Add a new book
- Retrieve details of a book
- Update book information
- Delete a book from the inventory
- List all books
- Go (Golang)
- MySQL
jinzhu/dialects/mysql
for database interactions
- Go 1.16 or later
- MySQL 5.7 or later
-
Clone the repository:
git clone https://github.com/Darshan016/go-bookstore.git cd go-bookstore
-
Install dependencies:
go get -u github.com/jinzhu/gorm go get -u github.com/jinzhu/gorm/dialects/mysql
-
Set up MySQL:
Create a database named
bookstore
and a user with appropriate privileges. For example:CREATE DATABASE bookstore; CREATE USER 'bookstore_user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON bookstore.* TO 'bookstore_user'@'localhost'; FLUSH PRIVILEGES;
-
Configure the application:
Update the
main.go
file with your MySQL credentials:dsn := "bookstore_user:password@tcp(127.0.0.1:3306)/bookstore?charset=utf8&parseTime=True&loc=Local"
-
Run the application:
go run main.go
-
URL:
/book/
-
Method:
POST
-
Request Body:
{ "name": "Book Title", "author": "Author Name", "publication": "publication" }
-
Response:
{ "id": 1, "name": "Book Title", "author": "Author Name", "publication": "publication" }
-
URL:
/book/{id}
-
Method:
GET
-
Response:
{ "id": 1, "name": "Book Title", "author": "Author Name", "publication": "publication" }
-
URL:
/book/{id}
-
Method:
PUT
-
Request Body:
{ "name": "Book Title", "author": "Author Name", "publication": "publication" }
-
Response:
{ "id": 1, "name": "Book Title", "author": "Author Name", "publication": "publication" }
-
URL:
/book/{id}
-
Method:
DELETE
-
Response:
{ "message": "Book deleted successfully" }
-
URL:
/book/
-
Method:
GET
-
Response:
[ { "id": 1, "name": "Book Title", "author": "Author Name", "publication": "publication" }, { "id": 2, "name": "Book Title", "author": "Author Name", "publication": "publication" } ]