This repository contains a simple CRUD (Create, Read, Update, Delete) API for a movie application built using Golang. The API allows you to manage a collection of movies, each with an ID, ISBN, title, and director information.
- Clone the repository:
git clone https://github.com/Cybersayak/MovieCRUD2ndApp.git
- Navigate to the project directory:
cd MovieCRUD2ndApp
- Install the required dependencies:
go get -u github.com/gorilla/mux
- Run the application:
go run main.go
Once the server is running, you can interact with the API using tools like curl
or Postman. The server will be running on http://localhost:8080
.
- GET /movies: Retrieve all movies.
- GET /movies/{id}: Retrieve a specific movie by ID.
- POST /movies: Create a new movie.
- PUT /movies/{id}: Update an existing movie by ID.
- DELETE /movies/{id}: Delete a movie by ID.
The main code for the API is in the main.go
file. Below is an explanation of the key parts of the code:
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"log"
"math/rand"
"net/http"
"strconv"
)
type Movie struct {
ID string `json:"id"`
Isbn string `json:"isbn"`
Title string `json:"title"`
Director *Director `json:"director"`
}
type Director struct {
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
}
- The necessary packages are imported.
- Two structs,
Movie
andDirector
, are defined to represent the movie data.
var movies []Movie
func getMovies(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movies)
}
func deleteMovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range movies {
if item.ID == params["id"] {
movies = append(movies[:index], movies[index+1:]...)
break
}
}
json.NewEncoder(w).Encode(movies)
}
func getMovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for _, item := range movies {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
}
func createMovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var movie Movie
_ = json.NewDecoder(r.Body).Decode(&movie)
movie.ID = strconv.Itoa(rand.Intn(100000000))
movies = append(movies, movie)
json.NewEncoder(w).Encode(movie)
}
func updateMovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range movies {
if item.ID == params["id"] {
movies = append(movies[:index], movies[index+1:]...)
var movie Movie
_ = json.NewDecoder(r.Body).Decode(&movie)
movie.ID = params["id"]
movies = append(movies, movie)
json.NewEncoder(w).Encode(movie)
}
}
}
movies
is a global variable that stores the list of movies.- The handler functions (
getMovies
,deleteMovie
,getMovie
,createMovie
,updateMovie
) manage the CRUD operations.
func main() {
r := mux.NewRouter()
movies = append(movies, Movie{ID: "1", Isbn: "111-1111", Title: "Movie 1", Director: &Director{FirstName: "Rituporno", LastName: "Ghosh"}})
movies = append(movies, Movie{ID: "2", Isbn: "111-1234", Title: "Movie 2", Director: &Director{FirstName: "Sayak", LastName: "Ghosh"}})
r.HandleFunc("/movies", getMovies).Methods("GET")
r.HandleFunc("/movies/{id}", getMovie).Methods("GET")
r.HandleFunc("/movies", createMovie).Methods("POST")
r.HandleFunc("/movies/{id}", updateMovie).Methods("PUT")
r.HandleFunc("/movies/{id}", deleteMovie).Methods("DELETE")
fmt.Println("Starting server on port 8080\n")
log.Fatal(http.ListenAndServe(":8080", r))
}
- The
main
function initializes the router and sets up the initial movie data. - The routes are defined and associated with their respective handler functions.
- The server is started on port 8080.
This project is licensed under the MIT License. See the LICENSE file for details.