update implements of FilterTargets()

This commit is contained in:
Wenkai Yin 2016-06-13 14:24:13 +08:00
parent 82b4781571
commit cf7ee9a327

View File

@ -206,12 +206,6 @@ func (t *TargetAPI) Post() {
// Put ...
func (t *TargetAPI) Put() {
id := t.getIDFromURL()
if id == 0 {
t.CustomAbort(http.StatusBadRequest, "id can not be empty or 0")
}
target := &models.RepTarget{}
t.DecodeJSONReqAndValidate(target)
originTarget, err := dao.GetRepTarget(id)
if err != nil {
@ -219,6 +213,13 @@ func (t *TargetAPI) Put() {
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if originTarget == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
target := &models.RepTarget{}
t.DecodeJSONReqAndValidate(target)
if target.Name != originTarget.Name {
ta, err := dao.GetRepTargetByName(target.Name)
if err != nil {
@ -246,9 +247,6 @@ func (t *TargetAPI) Put() {
// Delete ...
func (t *TargetAPI) Delete() {
id := t.getIDFromURL()
if id == 0 {
t.CustomAbort(http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
}
target, err := dao.GetRepTarget(id)
if err != nil {
@ -269,12 +267,12 @@ func (t *TargetAPI) Delete() {
func (t *TargetAPI) getIDFromURL() int64 {
idStr := t.Ctx.Input.Param(":id")
if len(idStr) == 0 {
return 0
t.CustomAbort(http.StatusBadRequest, "invalid target ID")
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
t.CustomAbort(http.StatusBadRequest, "invalid ID in request URL")
if err != nil || id <= 0 {
t.CustomAbort(http.StatusBadRequest, "invalid target ID")
}
return id