Example #1
0
// UserGet func
func UserGet(r render.Render, params martini.Params) {

	var user models.User

	// Get all user
	if len(params) == 0 {
	}

	// find by id
	if params["id"] != "" {
		if err := db.Read("user:"******"id"], &user); err != nil {
			r.JSON(500, map[string]string{"error": err.Error()})
			return
		}
	}

	// error
	if user.Name == "" && user.Tel == "" {
		r.JSON(400, map[string]string{"error": "Argument invalid."})
		return
	}

	r.JSON(200, user)

}
Example #2
0
// RedisUserGet function
func RedisUserGet(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	log.Print("access to user get api of redis")

	id := params.ByName("id")

	if id == "" {
		log.Print("get all")

		if jsonArr, err := db.ReadAll("user"); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		} else {
			var json bytes.Buffer
			json.Grow(1024)
			json.WriteString("[")
			for i := range jsonArr {
				if i%2 == 0 {
					continue
				}
				if elem, ok := jsonArr[i].([]byte); ok {
					json.Write(elem)
					if i != len(jsonArr)-1 {
						json.WriteString(",")
					}
				}
			}
			json.WriteString("]")
			w.Header().Set("Content-Type", "application/json")
			w.Write(json.Bytes())

		}

	} else {
		log.Printf("get by id: id = %s", id)

		if json, err := db.Read("user", id); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)

		} else {
			w.Header().Set("Content-Type", "application/json")
			w.Write([]byte(json))

		}

	}
}