当前位置: 首页>>代码示例>>Golang>>正文


Golang Item.LockUsername方法代码示例

本文整理汇总了Golang中rter/data.Item.LockUsername方法的典型用法代码示例。如果您正苦于以下问题:Golang Item.LockUsername方法的具体用法?Golang Item.LockUsername怎么用?Golang Item.LockUsername使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rter/data.Item的用法示例。


在下文中一共展示了Item.LockUsername方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Update


//.........这里部分代码省略.........

		val = v
	case "roles":
		v := new(data.Role)
		v.Title = vars["key"]

		val = v
	case "taxonomy":
		v := new(data.Term)
		v.Term = vars["key"]

		val = v
	case "taxonomy/ranking":
		v := new(data.TermRanking)
		v.Term = vars["key"]

		val = v
	default:
		http.NotFound(w, r)
		return
	}

	if err != nil {
		log.Println(err, vars)
		http.Error(w, "Malformed key in URI", http.StatusBadRequest)
		return
	}

	err = storage.Select(val) //Load previous values so that update is non distructive of empty fields

	if err == storage.ErrZeroAffected {
		http.NotFound(w, r)
		return
	} else if err != nil {
		log.Println(err)
		http.Error(w, "Select3 Database error, likely due to malformed request.", http.StatusInternalServerError)
		return
	}

	// Decode the JSON into our generic object. The decode will leave unscpecified fields untouched.
	decoder := json.NewDecoder(r.Body)
	err = decoder.Decode(&val)

	if err != nil {
		log.Println(err)
		http.Error(w, "Malformed json.", http.StatusBadRequest)
		return
	}

	// Validate JSON, run pre-update hooks, etc...
	//We must reset fields we set earlier incase they were changed during the JSON decode
	switch v := val.(type) {
	case (*data.Item):
		v.ID, err = strconv.ParseInt(vars["key"], 10, 64)
		v.Author = user.Username
	case (*data.ItemComment):
		v.ID, err = strconv.ParseInt(vars["childkey"], 10, 64)
		v.Author = user.Username
	case (*data.User):
		v.Username = vars["key"]
	case (*data.UserDirection):
		v.Username = vars["key"]
		v.LockUsername = user.Username
	case (*data.Role):
		v.Title = vars["key"]
	case (*data.Term):
		v.Term = vars["key"]
		v.Author = user.Username
	case (*data.TermRanking):
		v.Term = vars["key"]
	}

	if err != nil {
		log.Println(err, vars)
		http.Error(w, "Malformed key in URI", http.StatusBadRequest)
		return
	}

	// Run the update
	err = storage.Update(val)

	if err == storage.ErrZeroAffected {
		w.WriteHeader(http.StatusNotModified)
		return
	} else if err != nil {
		log.Println(err)
		http.Error(w, "Update2 Database error, likely due to malformed request.", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json") // Header are important when GZIP is enabled

	// Return the updated item
	encoder := json.NewEncoder(w)
	err = encoder.Encode(val)

	if err != nil {
		log.Println(err)
	}
}
开发者ID:cyc115,项目名称:rter,代码行数:101,代码来源:crud.go


注:本文中的rter/data.Item.LockUsername方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。