Skip to content

Commit

Permalink
Adds endpoint to get movie by id
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronzi committed Jul 14, 2024
1 parent 75472ef commit 25a2649
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"label": "Hello Remote World",
"onAutoForward": "notify"
}
}
},
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",
"postCreateCommand": "go install github.com/swaggo/swag/cmd/swag@latest",
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ go.work.sum
.vscode/

# Output of vscode debugger (files tsarting with __debug_)
__debug_*
**/__debug_*
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# Go Demo API
# Go Demo API

## Rebuild the Swagger Documentation

To build the Swagger documentation, run the following command:

```bash
swag init -g cmd/movie-api/main.go --parseDependency --parseInternal -o docs
```

This will generate the `docs` directory with the Swagger documentation.

> **Note:** You should run this command every time you make changes to the API.
5 changes: 3 additions & 2 deletions cmd/movie-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
_ "go-demo-api/docs"
"go-demo-api/internal/api" // Import the new package
"go-demo-api/internal/api"
"net/http"

"github.com/gorilla/mux"
Expand All @@ -11,7 +11,8 @@ import (

func main() {
r := mux.NewRouter()
r.HandleFunc("/movies", api.GetMovies).Methods("GET") // Update the handler reference
r.HandleFunc("/movies", api.GetMovies).Methods("GET")
r.HandleFunc("/movies/{id}", api.GetMovie).Methods("GET")

// Serve Swagger UI
r.PathPrefix("/swagger/").Handler(httpSwagger.WrapHandler)
Expand Down
42 changes: 40 additions & 2 deletions docs/docs.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,61 @@ const docTemplate = `{
"produces": [
"application/json"
],
"tags": [
"movies"
],
"summary": "Retrieve list of movies",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/main.Movie"
"$ref": "#/definitions/internal_api.Movie"
}
}
}
}
}
},
"/movies/{id}": {
"get": {
"description": "Get a single movie by its ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"movies"
],
"summary": "Get a movie",
"parameters": [
{
"type": "string",
"description": "Movie ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/internal_api.Movie"
}
},
"404": {
"description": "Movie not found"
}
}
}
}
},
"definitions": {
"main.Movie": {
"internal_api.Movie": {
"type": "object",
"properties": {
"director": {
Expand Down
42 changes: 40 additions & 2 deletions docs/swagger.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,61 @@
"produces": [
"application/json"
],
"tags": [
"movies"
],
"summary": "Retrieve list of movies",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/main.Movie"
"$ref": "#/definitions/internal_api.Movie"
}
}
}
}
}
},
"/movies/{id}": {
"get": {
"description": "Get a single movie by its ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"movies"
],
"summary": "Get a movie",
"parameters": [
{
"type": "string",
"description": "Movie ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/internal_api.Movie"
}
},
"404": {
"description": "Movie not found"
}
}
}
}
},
"definitions": {
"main.Movie": {
"internal_api.Movie": {
"type": "object",
"properties": {
"director": {
Expand Down
29 changes: 27 additions & 2 deletions docs/swagger.yaml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
definitions:
main.Movie:
internal_api.Movie:
properties:
director:
type: string
Expand All @@ -23,7 +23,32 @@ paths:
description: OK
schema:
items:
$ref: '#/definitions/main.Movie'
$ref: '#/definitions/internal_api.Movie'
type: array
summary: Retrieve list of movies
tags:
- movies
/movies/{id}:
get:
consumes:
- application/json
description: Get a single movie by its ID
parameters:
- description: Movie ID
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/internal_api.Movie'
"404":
description: Movie not found
summary: Get a movie
tags:
- movies
swagger: "2.0"
33 changes: 32 additions & 1 deletion internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"encoding/json"
"net/http"

"github.com/gorilla/mux"
)

// Movie struct to hold movie data
Expand All @@ -20,8 +22,37 @@ var movies = []Movie{
{ID: "3", Title: "Interstellar", Director: "Christopher Nolan", Year: "2014"},
}

// GetMovies retrieves list of movies
// getMovies godoc
// @Summary Retrieve list of movies
// @Description Get all movies
// @Tags movies
// @Produce json
// @Success 200 {array} Movie
// @Router /movies [get]
func GetMovies(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movies)
}

// getMovie godoc
// @Summary Get a movie
// @Description Get a single movie by its ID
// @Tags movies
// @Accept json
// @Produce json
// @Param id path string true "Movie ID"
// @Success 200 {object} Movie
// @Failure 404 {object} nil "Movie not found"
// @Router /movies/{id} [get]
func GetMovie(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

for _, movie := range movies {
if movie.ID == id {
json.NewEncoder(w).Encode(movie)
return
}
}
http.NotFound(w, r)
}

0 comments on commit 25a2649

Please sign in to comment.