예제 #1
0
func inqueryCreate(w http.ResponseWriter, r *http.Request) {
	o := orm.NewOrm()

	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	checkError(w, err)
	if err := r.Body.Close(); err != nil {
		w.WriteHeader(400)
		checkError(w, err)
	}

	w = setHeader(w)

	var inquery Inquery
	if err := json.Unmarshal(body, &inquery); err != nil {
		w.WriteHeader(422)
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
	}

	inquery.Created = time.Now()
	_, err = o.Insert(&inquery)
	if err == nil {
		w.WriteHeader(201)
	}
}
예제 #2
0
//LikeをPOSTする関数。もし動画がなければ作成する。
func likeUpdate(w http.ResponseWriter, r *http.Request) {
	o := orm.NewOrm()

	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)
	}

	w = utils.SetJSONHeader(w)

	var video Video

	//jsonをパース
	if err := json.Unmarshal(body, &video); err != nil {
		w.WriteHeader(422)
		if err := json.NewEncoder(w).Encode(err); err != nil {
			utils.CheckError(w, err)
		}
	}

	//DBに保存
	if _, _, err := o.ReadOrCreate(&video, "videoId"); err == nil {
		video.Liked = video.Liked + 1
		if _, err := o.Update(&video); err == nil {
			w.WriteHeader(200)
			response, err := json.Marshal(video)
			utils.CheckError(w, err)
			fmt.Fprintln(w, string(response))
		}
	}
}
예제 #3
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)
	}
}
예제 #4
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))
		}
	}
}
예제 #5
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)
	}
}
예제 #6
0
func inqueryIndex(w http.ResponseWriter, r *http.Request) {
	o := orm.NewOrm()

	params := r.URL.Query()
	page, _ := strconv.Atoi(params.Get("page"))
	maxResults, _ := strconv.Atoi(params.Get("maxResults"))
	dataType := params.Get("dataType")

	offset := maxResults * (page - 1)

	if page == 0 {
		page = 0
	}

	if maxResults == 0 {
		maxResults = 40
	}

	var inqueries Inqueries

	_, err := o.QueryTable("inquery").Limit(maxResults, offset).OrderBy("id").All(&inqueries)
	checkError(w, err)

	w.WriteHeader(200)

	if dataType == "json" {
		response, err := json.Marshal(inqueries)
		if err != nil {
			fmt.Fprintln(w, err)
		}
		w = setHeader(w)
		fmt.Fprintln(w, string(response))
	} else {
		//こっからテンプレート
		data := map[string]Inqueries{
			"Inqueries": inqueries,
		}
		tmpl := template.Must(template.ParseFiles("templates/base.html", "templates/inquery/index.html"))
		w.Header().Set("Content-Type", "text/html")

		err = tmpl.Execute(w, data)
		if err != nil {
			panic(err)
		}
	}
}
예제 #7
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)
			}

		}
	}
}
예제 #8
0
//Videoを一覧する関数order=likeでおすすめの多い順で取得
func videoIndex(w http.ResponseWriter, r *http.Request) {
	w = utils.SetJSONHeader(w)

	o := orm.NewOrm()

	params := r.URL.Query()
	page, _ := strconv.Atoi(params.Get("page"))
	maxResults, _ := strconv.Atoi(params.Get("maxResults"))
	order := params.Get("order")

	offset := maxResults * (page - 1)

	if page == 0 {
		page = 0
	}

	if maxResults == 0 {
		maxResults = 40
	}

	var videos Videos

	var err error
	//DBからデータを取ってくる
	if order == "like" {
		_, err = o.QueryTable("video").Limit(maxResults, offset).OrderBy("-liked").All(&videos)
	} else {
		_, err = o.QueryTable("video").Limit(maxResults, offset).OrderBy("id").All(&videos)
	}
	if err != nil {
		panic(err)
	}

	w.WriteHeader(200)
	response, err := json.Marshal(videos)
	utils.CheckError(w, err)
	fmt.Fprintln(w, string(response))
}