UpdateDeviceByID handles PUT on /devices/{deviceID}
( req *http.Request, userAPI api.ClientUserAPI, device *api.Device, deviceID string, )
| 118 | |
| 119 | // UpdateDeviceByID handles PUT on /devices/{deviceID} |
| 120 | func UpdateDeviceByID( |
| 121 | req *http.Request, userAPI api.ClientUserAPI, device *api.Device, |
| 122 | deviceID string, |
| 123 | ) util.JSONResponse { |
| 124 | |
| 125 | defer req.Body.Close() // nolint: errcheck |
| 126 | |
| 127 | payload := deviceUpdateJSON{} |
| 128 | |
| 129 | if resErr := httputil.UnmarshalJSONRequest(req, &payload); resErr != nil { |
| 130 | return *resErr |
| 131 | } |
| 132 | |
| 133 | var performRes api.PerformDeviceUpdateResponse |
| 134 | err := userAPI.PerformDeviceUpdate(req.Context(), &api.PerformDeviceUpdateRequest{ |
| 135 | RequestingUserID: device.UserID, |
| 136 | DeviceID: deviceID, |
| 137 | DisplayName: payload.DisplayName, |
| 138 | }, &performRes) |
| 139 | if err != nil { |
| 140 | util.GetLogger(req.Context()).WithError(err).Error("PerformDeviceUpdate failed") |
| 141 | return jsonerror.InternalServerError() |
| 142 | } |
| 143 | if !performRes.DeviceExists { |
| 144 | return util.JSONResponse{ |
| 145 | Code: http.StatusNotFound, |
| 146 | JSON: jsonerror.Forbidden("device does not exist"), |
| 147 | } |
| 148 | } |
| 149 | if performRes.Forbidden { |
| 150 | return util.JSONResponse{ |
| 151 | Code: http.StatusForbidden, |
| 152 | JSON: jsonerror.Forbidden("device not owned by current user"), |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return util.JSONResponse{ |
| 157 | Code: http.StatusOK, |
| 158 | JSON: struct{}{}, |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // DeleteDeviceById handles DELETE requests to /devices/{deviceId} |
| 163 | func DeleteDeviceById( |
no test coverage detected