Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
Implemented updating a device. Fixes #17.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paddy Foran committed Nov 19, 2012
1 parent 6e91b72 commit dba9834
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,70 @@ func newDevice(w http.ResponseWriter, r *twocloud.RequestBundle) {
}

func updateDevice(w http.ResponseWriter, r *twocloud.RequestBundle) {
username := r.Request.URL.Query().Get(":username")
deviceId := r.Request.URL.Query().Get(":device")
user := r.AuthUser
if strings.ToLower(username) != strings.ToLower(r.AuthUser.Username) {
if !r.AuthUser.IsAdmin {
Respond(w, r, http.StatusUnauthorized, "You don't have access to that user's devices.", []interface{}{})
return
}
id, err := r.GetUserID(username)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
user, err = r.GetUser(id)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
}
devID, err := ruid.RUIDFromString(deviceId)
if err != nil {
Respond(w, r, http.StatusBadRequest, "Invalid device ID", []interface{}{})
return
}
device := r.Device
if device.ID != devID {
device, err = r.GetDevice(devID)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
}
if device.UserID != user.ID {
Respond(w, r, http.StatusBadRequest, "The specified device does not belong to the specified user", []interface{}{})
return
}
var req twocloud.Device
body, err := ioutil.ReadAll(r.Request.Body)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
return
}
err = json.Unmarshal(body, &req)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusBadRequest, "Error decoding request.", []interface{}{})
return
}
req.ClientType = strings.ToLower(req.ClientType)
if !req.ValidClientType() {
Respond(w, r, http.StatusBadRequest, "Invalid client type.", []interface{}{})
return
}
device, err = r.UpdateDevice(device, req.Name, req.ClientType, req.Pushers.GCM.Key)
if err != nil {
Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
return
}
Respond(w, r, http.StatusCreated, "Successfully updated the device", []interface{}{device})
return
}

func deleteDevice(w http.ResponseWriter, r *twocloud.RequestBundle) {
Expand Down

0 comments on commit dba9834

Please sign in to comment.