示例#1
0
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
	fields := createUserForm{}

	// parse & validate your form and response errors with a pretty json error message
	// if not all fields are valid
	// e.g.
	//
	// HTTP 400
	// {
	// "message": "Invalid Data",
	//   "fields": {
	//     "email": "must be a valid email address",
	//     "password": "******"
	//   }
	// }
	if formutils.ParseSend(w, r, &fields) {
		// oh! some fields are not valid, exit your handler
		return
	}

	// OR use formutils.Parse(r, &fields) instead if you don't want to response
	// with an error message automatically.
	// Handle your invalids manually
	// invalids, err := formutils.Parse(r, &fields)

	// everything is OK, fields should be filled with their values
	fmt.Println(fields)
	httpres.Json(w, http.StatusCreated, fields)
}
示例#2
0
文件: user.go 项目: oeeckhoutte/stack
func ListUsersHandler(w http.ResponseWriter, r *http.Request) {
	fields := listUsersForm{}

	if formutils.ParseSend(w, r, &fields) {
		return
	}

	m := bson.M{}

	if fields.Name != "" {
		//todo: do full text search instead
		m["name"] = bson.RegEx{fields.Name, "i"}
	}

	q := ctx.M(r).DB("").C("users").Find(m)

	totalCount, err := q.Count()
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	p := paging.Paging{
		Page:  fields.Page,
		Limit: fields.Limit,
		Count: totalCount,
	}.Calc()

	users := []model.User{}

	if err = q.
		Limit(p.Limit).
		Skip(p.Offset).
		Sort("-created_at").
		All(&users); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	rp := usersResponse{
		Users:           users,
		CurrentPage:     p.Page,
		TotalPagesCount: p.TotalPages,
	}

	httpres.Json(w, http.StatusOK, rp)
}
示例#3
0
func CreateEventHandler(w http.ResponseWriter, r *http.Request) {
	event := model.Event{}

	if formutils.ParseSend(w, r, &event) {
		return
	}

	if err := ctx.M(r).DB("").C("events").Insert(&event); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	httpres.Json(w, http.StatusCreated, eventResponse{Event: event})
}
示例#4
0
文件: user.go 项目: oeeckhoutte/stack
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
	fields := createUserForm{}

	if formutils.ParseSend(w, r, &fields) {
		return
	}

	hash, err := cryptoutils.Hash(fields.Password, conf.PasswordLevel)
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	user := model.User{
		ID:           bson.NewObjectId(),
		Name:         fields.Name,
		Email:        strings.TrimSpace(fields.Email),
		PasswordHash: hash,
		CreatedAt:    time.Now(),
	}

	if err := ctx.M(r).DB("").C("users").Insert(&user); err != nil {
		if mgo.IsDup(err) {
			eres.New(w).AddField("email", "already exists").Send()
			return
		}

		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	token := model.NewToken(user.ID, false)

	if err := ctx.M(r).DB("").C("tokens").Insert(&token); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	rp := userResponse{
		User:        user,
		AccessToken: token.Token,
	}

	httpres.Json(w, http.StatusCreated, rp)
}
示例#5
0
func ListEventsHandler(w http.ResponseWriter, r *http.Request) {
	fields := listEventsForm{}

	if formutils.ParseSend(w, r, &fields) {
		return
	}

	q := ctx.M(r).DB("").C("events")

	totalCount, err := q.Count()
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	p := paging.Paging{
		Page:  fields.Page,
		Limit: fields.Limit,
		Count: totalCount,
	}.Calc()

	events := []model.Event{}

	if err := q.Find(nil).
		Skip(p.Offset).
		Limit(p.Limit).
		Sort("-date").
		All(&events); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	rp := eventsResponse{
		CurrentPage:     p.Page,
		TotalPagesCount: p.TotalPages,
		Events:          events,
	}

	httpres.Json(w, http.StatusOK, rp)
}
示例#6
0
func CreateTokenHandler(w http.ResponseWriter, r *http.Request) {
	fields := createTokenForm{}

	if formutils.ParseSend(w, r, &fields) {
		return
	}

	var user model.User

	if err := ctx.M(r).DB("").C("users").Find(bson.M{
		"email": strings.TrimSpace(fields.Email),
	}).One(&user); err != nil {
		if err == mgo.ErrNotFound {
			eres.New(w).SetMessage("bad credentials").Send()
			return
		}

		log.Println(err)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(fields.Password)); err != nil {
		eres.New(w).SetMessage("bad credentials").Send()
		return
	}

	token := model.NewToken(user.ID, fields.Forever)

	if err := ctx.M(r).DB("").C("tokens").Insert(&token); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	httpres.Json(w, http.StatusCreated, tokenResponse{token.Token})
}