api routes; status codes fixed

This commit is contained in:
Jimmy Zelinskie
2014-07-09 00:53:57 -04:00
parent 6ee6c0bef4
commit afb2376286
4 changed files with 83 additions and 67 deletions
+42 -42
View File
@@ -15,156 +15,156 @@ import (
"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, error) {
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
torrent, err := conn.FindTorrent(p.ByName("infohash"))
if err == tracker.ErrTorrentDNE {
return http.StatusNotFound
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
e := json.NewEncoder(w)
err = e.Encode(torrent)
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
return http.StatusOK
return http.StatusOK, nil
}
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, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
var torrent models.Torrent
err = json.Unmarshal(body, &torrent)
if err != nil {
return http.StatusBadRequest
return http.StatusBadRequest, err
}
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
err = conn.PutTorrent(&torrent)
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
return http.StatusOK
return http.StatusOK, nil
}
func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
err = conn.DeleteTorrent(p.ByName("infohash"))
if err == tracker.ErrTorrentDNE {
return http.StatusNotFound
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
return http.StatusOK
return http.StatusOK, nil
}
func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
user, err := conn.FindUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
return http.StatusNotFound
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
e := json.NewEncoder(w)
err = e.Encode(user)
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
return http.StatusOK
return http.StatusOK, nil
}
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, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
var user models.User
err = json.Unmarshal(body, &user)
if err != nil {
return http.StatusBadRequest
return http.StatusBadRequest, err
}
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
err = conn.PutUser(&user)
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
return http.StatusOK
return http.StatusOK, nil
}
func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
err = conn.DeleteUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
return http.StatusNotFound
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
return http.StatusOK
return http.StatusOK, nil
}
func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
err = conn.PutClient(p.ByName("clientID"))
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
return http.StatusOK
return http.StatusOK, nil
}
func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
err = conn.DeleteClient(p.ByName("clientID"))
if err != nil {
return http.StatusInternalServerError
return http.StatusInternalServerError, err
}
return http.StatusOK
return http.StatusOK, nil
}