API: putUser & putTorrent impl
This commit is contained in:
parent
f656133a6f
commit
6ee6c0bef4
1 changed files with 44 additions and 0 deletions
44
http/api.go
44
http/api.go
|
@ -6,11 +6,13 @@ package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/drivers/tracker"
|
"github.com/chihaya/chihaya/drivers/tracker"
|
||||||
|
"github.com/chihaya/chihaya/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *Tracker) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
func (t *Tracker) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||||
|
@ -36,6 +38,27 @@ func (t *Tracker) getTorrent(w http.ResponseWriter, r *http.Request, p httproute
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tracker) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
func (t *Tracker) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
var torrent models.Torrent
|
||||||
|
err = json.Unmarshal(body, &torrent)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := t.tp.Get()
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conn.PutTorrent(&torrent)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
return http.StatusOK
|
return http.StatusOK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +101,27 @@ func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.P
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
var user models.User
|
||||||
|
err = json.Unmarshal(body, &user)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := t.tp.Get()
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conn.PutUser(&user)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
return http.StatusOK
|
return http.StatusOK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue