GithubHelp home page GithubHelp logo

moviecrud2ndapp's Introduction

MovieCRUD2ndApp

This is my second app of learning golang

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.

Table of Contents

Installation

  1. Clone the repository:
    git clone https://github.com/Cybersayak/MovieCRUD2ndApp.git
  2. Navigate to the project directory:
    cd MovieCRUD2ndApp
  3. Install the required dependencies:
    go get -u github.com/gorilla/mux
  4. Run the application:
    go run main.go

Usage

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.

API Endpoints

  • 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.

Code Explanation

The main code for the API is in the main.go file. Below is an explanation of the key parts of the code:

Imports and Struct Definitions

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 and Director, are defined to represent the movie data.

Global Variables and Handlers

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.

Main Function

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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

moviecrud2ndapp's People

Contributors

cybersayak avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.