This project provides a RESTful API for managing a library system using the Gin web framework. The API supports CRUD operations for managing authors, books, publishers, borrowers, and users. Each
entity is managed with concurrency-safe operations using the sync.Mutex
for thread-safe data access.
- Models: Defined in
belajar_api/controllers
- Request Handlers: Defined in
request
package
The project uses the following data structures:
AuthorDB
BookDB
PublisherDB
BorrowerDB
UserDB
Each of these data structures is equipped with a mutex to ensure thread-safe operations.
- Endpoint:
POST /user
- Description: Creates a new user.
- Parameters:
name
firstname
username
lastname
email
password
- Endpoint:
POST /borrower
- Description: Creates a new borrower.
- Parameters:
userId
endDate
startDate
bookId
- Endpoint:
POST /publisher
- Description: Creates a new publisher.
- Parameters:
name
address
- Endpoint:
POST /book
- Description: Creates a new book.
- Parameters:
name
description
publisherId
authorId
- Endpoint:
POST /author
- Description: Creates a new author.
- Parameters:
name
email
- Endpoint:
GET /users
- Description: Retrieves a list of all users.
- Endpoint:
GET /user/:id
- Description: Retrieves a user by ID.
- Endpoint:
GET /borrowers
- Description: Retrieves a list of all borrowers.
- Endpoint:
GET /borrower/:id
- Description: Retrieves a borrower by ID.
- Endpoint:
GET /publishers
- Description: Retrieves a list of all publishers.
- Endpoint:
GET /publisher/:id
- Description: Retrieves a publisher by ID.
- Endpoint:
GET /books
- Description: Retrieves a list of all books.
- Endpoint:
GET /book/:id
- Description: Retrieves a book by ID.
- Endpoint:
GET /authors
- Description: Retrieves a list of all authors.
- Endpoint:
GET /author/:id
- Description: Retrieves an author by ID.
- Endpoint:
PUT /user/:id
- Description: Updates a user by ID.
- Parameters:
name
firstname
lastname
username
email
password
- Endpoint:
PUT /borrower/:id
- Description: Updates a borrower by ID.
- Parameters:
userId
endDate
startDate
bookId
- Endpoint:
PUT /publisher/:id
- Description: Updates a publisher by ID.
- Parameters:
name
address
- Endpoint:
PUT /book/:id
- Description: Updates a book by ID.
- Parameters:
name
description
publisherId
authorId
- Endpoint:
PUT /author/:id
- Description: Updates an author by ID.
- Parameters:
name
email
- Endpoint:
DELETE /user/:id
- Description: Deletes a user by ID.
- Endpoint:
DELETE /borrower/:id
- Description: Deletes a borrower by ID.
- Endpoint:
DELETE /publisher/:id
- Description: Deletes a publisher by ID.
- Endpoint:
DELETE /book/:id
- Description: Deletes a book by ID.
- Endpoint:
DELETE /author/:id
- Description: Deletes an author by ID.
To use the API, you need to run the server and make HTTP requests to the endpoints listed above.
To create a new user, you can send a POST request to /user
with the required parameters.
curl -X POST "http://localhost:8080/user?name=John&firstname=Doe&lastname=Doe&username=johndoe&[email protected]&password=secret"
The project uses sync.Mutex
to ensure thread-safe access to the in-memory data structures. This prevents race conditions when multiple goroutines try to read or write data simultaneously.
github.com/gin-gonic/gin
: Gin web framework
- Clone the repository
- Install the dependencies:
go mod tidy
- Run the server:
go run main.go
- Saiful