Example #1
0
func videoDelete(w http.ResponseWriter, r *http.Request) {
	w = utils.SetJSONHeader(w)
	if utils.CheckAuth(r) == false {
		w.Header().Set("WWW-Authenticate", `Basic realm="MY REALM"`)
		w.WriteHeader(401)
		w.Write([]byte("401 Unauthorized\n"))
		return
	}

	o := orm.NewOrm()
	vars := mux.Vars(r)
	videoID, err := strconv.Atoi(vars["videoId"])

	w = utils.SetJSONHeader(w)

	if err != nil {
		w.WriteHeader(400)
		return
	}
	video := Video{Id: videoID}

	if _, err := o.Delete(&video); err == nil {
		w.WriteHeader(204)
	} else {
		w.WriteHeader(404)
	}
}
Example #2
0
func videoUpdate(w http.ResponseWriter, r *http.Request) {
	w = utils.SetJSONHeader(w)
	if utils.CheckAuth(r) == false {
		w.Header().Set("WWW-Authenticate", `Basic realm="MY REALM"`)
		w.WriteHeader(401)
		w.Write([]byte("401 Unauthorized\n"))
		return
	}

	o := orm.NewOrm()

	//パラメタからアップデートしたいvideoIdを取得
	vars := mux.Vars(r)
	videoID, _ := strconv.Atoi(vars["videoId"])
	video := Video{Id: videoID}

	//bodyからデータを取り出す。
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	checkError(w, err)
	if err := r.Body.Close(); err != nil {
		w.WriteHeader(400)
		utils.CheckError(w, err)
		panic(err)
	}

	var newVideo Video
	//jsonをパース
	if err := json.Unmarshal(body, &newVideo); err != nil {
		w.WriteHeader(422)
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
		panic(err)
	}

	if o.Read(&video) == nil {
		//データがぞんざいすればアップデートする
		video.VideoId = newVideo.VideoId
		video.Title = newVideo.Title
		video.Artist = newVideo.Artist
		if _, err := o.Update(&video); err == nil {
			w.WriteHeader(200)
			response, err := json.Marshal(video)
			utils.CheckError(w, err)
			fmt.Fprintln(w, string(response))
		}
	}
}
Example #3
0
func inqueryDelete(w http.ResponseWriter, r *http.Request) {
	o := orm.NewOrm()

	vars := mux.Vars(r)

	id, err := strconv.Atoi(vars["inqueryId"])
	w = setHeader(w)

	if err != nil {
		w.WriteHeader(400)
	}

	if _, err = o.Delete(&Inquery{Id: id}); err == nil {
		w.WriteHeader(204)
	}
}
Example #4
0
func inqueryShow(w http.ResponseWriter, r *http.Request) {
	o := orm.NewOrm()

	params := r.URL.Query()
	dataType := params.Get("dataType")

	vars := mux.Vars(r)
	inqueryID, _ := strconv.Atoi(vars["inqueryId"])
	inquery := Inquery{Id: inqueryID}
	err := o.Read(&inquery)

	if err == orm.ErrNoRows {
		w.WriteHeader(204)
		fmt.Fprintln(w, "No result found.")
	} else if err == orm.ErrMissPK {
		fmt.Fprintln(w, "No primary key found.")
	} else {
		w.WriteHeader(200)
		if dataType == "json" {
			response, err := json.Marshal(inquery)
			checkError(w, err)
			w = setHeader(w)
			fmt.Fprintln(w, string(response))
		} else {
			//こっからテンプレート
			data := map[string]Inquery{
				"Inquery": inquery,
			}
			tmpl := template.Must(template.ParseFiles("templates/base.html", "templates/inquery/show.html"))
			w.Header().Set("Content-Type", "text/html")

			err = tmpl.Execute(w, data)
			if err != nil {
				panic(err)
			}

		}
	}
}