Exemplo n.º 1
0
func (i *Impl) CreateUser(w rest.ResponseWriter, r *rest.Request) {
	var user UserPayload
	err := r.DecodeJsonPayload(&user)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	// returns -1 if username is unable to be registered, ie, already taken
	uid, err := i.DB.CreateUser(user.Username, user.Name)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if uid == -1 {
		w.WriteJson(map[string]string{
			"uid":  strconv.Itoa(uid),
			"user": "******",
		})
		return
	}

	userOut, err := i.DB.GetUser(uid)

	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteJson(&userOut)
}
Exemplo n.º 2
0
/*
 * consumes JSON of the form
   {
		"uid": <user id>
		"msg": <text string containing message>
   }
*/
func (i *Impl) PostStatus(w rest.ResponseWriter, r *rest.Request) {
	var status StatusPayload
	if err := r.DecodeJsonPayload(&status); err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	sid, err := i.DB.PostStatus(status.Uid, status.Msg)

	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	post, err := i.DB.GetStatus(sid)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.WriteJson(&post)
}