func CreatePostHandler(r render.Render, post model.Post, appx *appx.Datastore, location middleware.RequestLocation) {

	response := model.Response{
		ErrorCode: http.StatusOK,
		Message:   []string{},
		Data:      nil,
	}

	post.CarPlate = strings.ToUpper(post.CarPlate)
	post.Country = location.Country
	post.CreatedAt = time.Now()

	isValid, validationErr := post.IsValid(location.Country)

	if !isValid {
		response.ErrorCode = http.StatusBadRequest
		response.Message = validationErr
	} else {
		err := appx.Save(&post)

		if err != nil {
			log.Printf("Error: %+v", err)
			response.ErrorCode = http.StatusInternalServerError
			response.Message = append(response.Message, err.Error())
		} else {
			postResource := &resources.PostResource{}
			postResource.From(post)
			response.Data = postResource
		}
	}

	r.JSON(200, response)
}
Exemplo n.º 2
0
func (this *PostResource) From(post model.Post) {
	this.CarPlate = post.CarPlate
	this.CreatedAt = post.CreatedAt
	this.Id = post.EncodedKey()
	this.Message = post.Message
	this.UserName = post.UserName
}
func FlagPostHandler(r render.Render, params martini.Params, appx *appx.Datastore) {
	postId := params["post_id"]

	response := model.Response{
		ErrorCode: http.StatusOK,
		Message:   []string{},
		Data:      nil,
	}

	post := model.Post{}
	post.SetEncodedKey(postId)
	err := appx.Load(&post)

	fmt.Print("aqui")
	if err != nil {
		fmt.Print("not found")
		fmt.Print(err)
		response.Message = []string{err.Error()}
		response.ErrorCode = http.StatusBadRequest
	} else {
		fmt.Print("found")
		fmt.Print(err)
		post.Flagged = true
		err = appx.Save(&post)

		if err != nil {
			fmt.Print("save problem")
			fmt.Print(err)
			response.Message = []string{err.Error()}
			response.ErrorCode = http.StatusInternalServerError
		} else {
			postResource := &resources.PostResource{}
			postResource.From(post)
			response.Data = postResource
		}
	}

	r.JSON(200, response)
}