From 6ee6c0bef40e140c86126105ad349609c2bef642 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Tue, 8 Jul 2014 16:55:24 -0400 Subject: [PATCH] API: putUser & putTorrent impl --- http/api.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/http/api.go b/http/api.go index d6da6b2..a48eaeb 100644 --- a/http/api.go +++ b/http/api.go @@ -6,11 +6,13 @@ package http import ( "encoding/json" + "io/ioutil" "net/http" "github.com/julienschmidt/httprouter" "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 { @@ -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 { + 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 } @@ -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 { + 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 }