moviedb/backend/addViews.go

64 lines
1.3 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func addMovie(w http.ResponseWriter, r *http.Request) {
var newmov Movie
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Please enter a movie")
}
json.Unmarshal(reqBody, &newmov)
fmt.Println(newmov)
mov, err := db.CreateMovie(newmov.Title, newmov.Imdb)
if err != nil || mov.ID == 0 {
fmt.Fprintf(w, "Error creating movie")
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(mov)
}
func addTaggedMovie(w http.ResponseWriter, r *http.Request) {
movieIDstr := mux.Vars(r)["mid"]
tagIDstr := mux.Vars(r)["tid"]
movieID, err := strconv.Atoi(movieIDstr)
if err != nil {
fmt.Fprintf(w, "Error: invalid movie ID passed")
return
}
tagID, err := strconv.Atoi(tagIDstr)
if err != nil {
fmt.Fprintf(w, "Error: invalid tag ID passed")
return
}
err = db.TagMovie(tagID, movieID)
if err != nil {
fmt.Fprintf(w, "Error tagging movie")
}
json.NewEncoder(w).Encode("tagged")
}
func addTag(w http.ResponseWriter, r *http.Request) {
var newtag Tag
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Please enter a tag")
}
json.Unmarshal(reqBody, &newtag)
tag, _ := db.CreateTag(newtag.Name)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(tag)
}