working on updating items' box id
This commit is contained in:
parent
012c2bd645
commit
ea4c6142d0
|
@ -0,0 +1,227 @@
|
|||
# API Reference
|
||||
|
||||
## Base URL
|
||||
```
|
||||
http://<your-server-address>:<port>
|
||||
```
|
||||
|
||||
## Authentication
|
||||
All endpoints (except `/login`) require a valid JWT token in the `Authorization` header. The token is prefixed with `Bearer `.
|
||||
|
||||
---
|
||||
|
||||
## Endpoints
|
||||
|
||||
### 1. **Login**
|
||||
- **Endpoint:** `/login`
|
||||
- **Method:** `POST`
|
||||
- **Request Body:**
|
||||
```json
|
||||
{
|
||||
"username": "string",
|
||||
"password": "string"
|
||||
}
|
||||
```
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"token": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Authenticates a user and returns a JWT token.
|
||||
|
||||
---
|
||||
|
||||
### 2. **Get Boxes**
|
||||
- **Endpoint:** `/boxes`
|
||||
- **Method:** `GET`
|
||||
- **Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
- **Description:** Retrieves all boxes.
|
||||
|
||||
---
|
||||
|
||||
### 3. **Create Box**
|
||||
- **Endpoint:** `/boxes`
|
||||
- **Method:** `POST`
|
||||
- **Request Body:**
|
||||
```json
|
||||
{
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Creates a new box and returns its ID.
|
||||
|
||||
---
|
||||
|
||||
### 4. **Delete Box**
|
||||
- **Endpoint:** `/boxes/{id}`
|
||||
- **Method:** `DELETE`
|
||||
- **Description:** Deletes a box by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 5. **Get Items**
|
||||
- **Endpoint:** `/items`
|
||||
- **Method:** `GET`
|
||||
- **Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
- **Description:** Retrieves all items.
|
||||
|
||||
---
|
||||
|
||||
### 6. **Create Item**
|
||||
- **Endpoint:** `/items`
|
||||
- **Method:** `POST`
|
||||
- **Request Body:**
|
||||
```json
|
||||
{
|
||||
"name": "string",
|
||||
"description": "string",
|
||||
"box_id": "integer"
|
||||
}
|
||||
```
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Creates a new item and returns its ID.
|
||||
|
||||
---
|
||||
|
||||
### 7. **Get Item**
|
||||
- **Endpoint:** `/items/{id}`
|
||||
- **Method:** `GET`
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string",
|
||||
"description": "string",
|
||||
"box_id": "integer"
|
||||
}
|
||||
```
|
||||
- **Description:** Retrieves an item by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 8. **Update Item**
|
||||
- **Endpoint:** `/items/{id}`
|
||||
- **Method:** `PUT`
|
||||
- **Request Body:**
|
||||
```json
|
||||
{
|
||||
"name": "string",
|
||||
"description": "string",
|
||||
"box_id": "integer"
|
||||
}
|
||||
```
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Updates an item by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 9. **Delete Item**
|
||||
- **Endpoint:** `/items/{id}`
|
||||
- **Method:** `DELETE`
|
||||
- **Description:** Deletes an item by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 10. **Get Items in Box**
|
||||
- **Endpoint:** `/boxes/{id}/items`
|
||||
- **Method:** `GET`
|
||||
- **Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
- **Description:** Retrieves all items within a specified box.
|
||||
|
||||
---
|
||||
|
||||
### 11. **Upload Item Image**
|
||||
- **Endpoint:** `/items/{id}/upload`
|
||||
- **Method:** `POST`
|
||||
- **Form Data:**
|
||||
- **File:** `image` (the image file to upload)
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"imagePath": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Uploads an image for a specified item.
|
||||
|
||||
---
|
||||
|
||||
### 12. **Get Item Image**
|
||||
- **Endpoint:** `/items/{id}/image`
|
||||
- **Method:** `GET`
|
||||
- **Response:** Returns the image of the specified item.
|
||||
- **Description:** Retrieves the image for an item by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 13. **Search Items**
|
||||
- **Endpoint:** `/search/items`
|
||||
- **Method:** `GET`
|
||||
- **Query Parameters:**
|
||||
- `q`: Search query string
|
||||
- **Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
- **Description:** Searches for items based on a query string.
|
||||
|
||||
---
|
||||
|
||||
## Middleware
|
||||
- **AuthMiddleware:** Validates the JWT token in the `Authorization` header and restricts access to protected endpoints.
|
||||
|
||||
## Notes
|
||||
- Replace `<your-server-address>` and `<port>` with your actual server address and port.
|
||||
- Ensure that CORS settings allow your frontend to access the API.
|
34
Dockerfile
34
Dockerfile
|
@ -1,39 +1,19 @@
|
|||
# Stage 1: Build the Go binary
|
||||
FROM golang:1.17-alpine AS builder
|
||||
FROM golang:latest
|
||||
|
||||
# Install build tools and SQLite development libraries
|
||||
RUN apk add --no-cache gcc musl-dev sqlite-dev
|
||||
|
||||
# Set the working directory inside the container
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the Go modules files and download dependencies
|
||||
COPY go.mod go.sum ./
|
||||
COPY go.mod ./
|
||||
|
||||
COPY go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
|
||||
# Copy the rest of the application source code
|
||||
COPY . .
|
||||
|
||||
# Build the Go application
|
||||
RUN go build -o boxes-api
|
||||
ENV CGO_ENABLED=1
|
||||
|
||||
# Stage 2: Create a minimal image with the Go binary
|
||||
FROM alpine:latest
|
||||
RUN go build -o boxes-api .
|
||||
|
||||
# Install necessary runtime dependencies
|
||||
RUN apk add --no-cache ca-certificates
|
||||
|
||||
# Set the working directory inside the container
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the binary from the builder stage
|
||||
COPY --from=builder /app/boxes-api .
|
||||
|
||||
# Copy any other necessary files (e.g., configuration files)
|
||||
# COPY --from=builder /app/config.yaml .
|
||||
|
||||
# Expose the port your app listens on
|
||||
EXPOSE 8080
|
||||
|
||||
# Command to run the executable
|
||||
CMD ["./boxes-api"]
|
|
@ -0,0 +1,228 @@
|
|||
|
||||
# API Reference
|
||||
|
||||
## Base URL
|
||||
```
|
||||
http://<your-server-address>:<port>
|
||||
```
|
||||
|
||||
## Authentication
|
||||
All endpoints (except `/login`) require a valid JWT token in the `Authorization` header. The token is prefixed with `Bearer `.
|
||||
|
||||
---
|
||||
|
||||
## Endpoints
|
||||
|
||||
### 1. **Login**
|
||||
- **Endpoint:** `/login`
|
||||
- **Method:** `POST`
|
||||
- **Request Body:**
|
||||
```json
|
||||
{
|
||||
"username": "string",
|
||||
"password": "string"
|
||||
}
|
||||
```
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"token": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Authenticates a user and returns a JWT token.
|
||||
|
||||
---
|
||||
|
||||
### 2. **Get Boxes**
|
||||
- **Endpoint:** `/boxes`
|
||||
- **Method:** `GET`
|
||||
- **Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
- **Description:** Retrieves all boxes.
|
||||
|
||||
---
|
||||
|
||||
### 3. **Create Box**
|
||||
- **Endpoint:** `/boxes`
|
||||
- **Method:** `POST`
|
||||
- **Request Body:**
|
||||
```json
|
||||
{
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Creates a new box and returns its ID.
|
||||
|
||||
---
|
||||
|
||||
### 4. **Delete Box**
|
||||
- **Endpoint:** `/boxes/{id}`
|
||||
- **Method:** `DELETE`
|
||||
- **Description:** Deletes a box by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 5. **Get Items**
|
||||
- **Endpoint:** `/items`
|
||||
- **Method:** `GET`
|
||||
- **Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
- **Description:** Retrieves all items.
|
||||
|
||||
---
|
||||
|
||||
### 6. **Create Item**
|
||||
- **Endpoint:** `/items`
|
||||
- **Method:** `POST`
|
||||
- **Request Body:**
|
||||
```json
|
||||
{
|
||||
"name": "string",
|
||||
"description": "string",
|
||||
"box_id": "integer"
|
||||
}
|
||||
```
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Creates a new item and returns its ID.
|
||||
|
||||
---
|
||||
|
||||
### 7. **Get Item**
|
||||
- **Endpoint:** `/items/{id}`
|
||||
- **Method:** `GET`
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string",
|
||||
"description": "string",
|
||||
"box_id": "integer"
|
||||
}
|
||||
```
|
||||
- **Description:** Retrieves an item by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 8. **Update Item**
|
||||
- **Endpoint:** `/items/{id}`
|
||||
- **Method:** `PUT`
|
||||
- **Request Body:**
|
||||
```json
|
||||
{
|
||||
"name": "string",
|
||||
"description": "string",
|
||||
"box_id": "integer"
|
||||
}
|
||||
```
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Updates an item by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 9. **Delete Item**
|
||||
- **Endpoint:** `/items/{id}`
|
||||
- **Method:** `DELETE`
|
||||
- **Description:** Deletes an item by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 10. **Get Items in Box**
|
||||
- **Endpoint:** `/boxes/{id}/items`
|
||||
- **Method:** `GET`
|
||||
- **Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
- **Description:** Retrieves all items within a specified box.
|
||||
|
||||
---
|
||||
|
||||
### 11. **Upload Item Image**
|
||||
- **Endpoint:** `/items/{id}/upload`
|
||||
- **Method:** `POST`
|
||||
- **Form Data:**
|
||||
- **File:** `image` (the image file to upload)
|
||||
- **Response:**
|
||||
```json
|
||||
{
|
||||
"imagePath": "string"
|
||||
}
|
||||
```
|
||||
- **Description:** Uploads an image for a specified item.
|
||||
|
||||
---
|
||||
|
||||
### 12. **Get Item Image**
|
||||
- **Endpoint:** `/items/{id}/image`
|
||||
- **Method:** `GET`
|
||||
- **Response:** Returns the image of the specified item.
|
||||
- **Description:** Retrieves the image for an item by its ID.
|
||||
|
||||
---
|
||||
|
||||
### 13. **Search Items**
|
||||
- **Endpoint:** `/search/items`
|
||||
- **Method:** `GET`
|
||||
- **Query Parameters:**
|
||||
- `q`: Search query string
|
||||
- **Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "integer",
|
||||
"name": "string"
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
- **Description:** Searches for items based on a query string.
|
||||
|
||||
---
|
||||
|
||||
## Middleware
|
||||
- **AuthMiddleware:** Validates the JWT token in the `Authorization` header and restricts access to protected endpoints.
|
||||
|
||||
## Notes
|
||||
- Replace `<your-server-address>` and `<port>` with your actual server address and port.
|
||||
- Ensure that CORS settings allow your frontend to access the API.
|
13
handlers.go
13
handlers.go
|
@ -74,6 +74,19 @@ func GetBoxesHandler(w http.ResponseWriter, r *http.Request) {
|
|||
json.NewEncoder(w).Encode(boxes)
|
||||
}
|
||||
|
||||
func GetBoxHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
var box Box
|
||||
if err := db.First(&box, id).Error; err != nil {
|
||||
http.Error(w, "Box not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(box)
|
||||
}
|
||||
|
||||
// createBoxHandler handles the POST /boxes endpoint.
|
||||
func CreateBoxHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var box Box
|
||||
|
|
2
main.go
2
main.go
|
@ -35,7 +35,6 @@ func main() {
|
|||
|
||||
// Connect to the database
|
||||
db, err = ConnectDB(config.DatabasePath)
|
||||
fmt.Println("DB Connection String:", db.DB().Ping())
|
||||
if err != nil || db == nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
@ -51,6 +50,7 @@ func main() {
|
|||
router.Handle("/boxes", AuthMiddleware(http.HandlerFunc(GetBoxesHandler))).Methods("GET", "OPTIONS")
|
||||
router.Handle("/boxes", AuthMiddleware(http.HandlerFunc(CreateBoxHandler))).Methods("POST", "OPTIONS")
|
||||
router.Handle("/boxes/{id}", AuthMiddleware(http.HandlerFunc(DeleteBoxHandler))).Methods("DELETE", "OPTIONS")
|
||||
router.Handle("/boxes/{id}", AuthMiddleware(http.HandlerFunc(GetBoxHandler))).Methods("GET", "OPTIONS")
|
||||
router.Handle("/items", AuthMiddleware(http.HandlerFunc(GetItemsHandler))).Methods("GET", "OPTIONS")
|
||||
router.Handle("/items", AuthMiddleware(http.HandlerFunc(CreateItemHandler))).Methods("POST", "OPTIONS")
|
||||
router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(GetItemHandler))).Methods("GET", "OPTIONS")
|
||||
|
|
Loading…
Reference in New Issue