Exemple #1
0
func TestNewUserPost(t *testing.T) {
	var e error
	var postA, postB *nerdz.UserPost
	if postA, e = nerdz.NewUserPost(13); e != nil {
		t.Fatalf("NewUserPost(13) shouldn't fail, but got: %s\n", e.Error())
	}

	if postB, e = nerdz.NewUserPostWhere(&nerdz.UserPost{nerdz.Post{To: 3, Pid: 2}}); e != nil {
		t.Fatalf("NewUserPostWhere To:3 and Pid:2 shouldn't fail, but got: %s\n", e.Error())
	}

	if !reflect.DeepEqual(postA, postB) {
		t.Fatalf("postA and postB should be equal but\nPostA: %v\nPostB: %v", postA, postB)
	}
}
Exemple #2
0
// SetPost is the middleware that checks if the required post, on the user board, exists.
// If it exists, set the "post" = *UserPost in the current context
func SetPost() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return echo.HandlerFunc(func(c echo.Context) error {
			var e error
			var pid uint64

			if pid, e = strconv.ParseUint(c.Param("pid"), 10, 64); e != nil {
				errstr := "Invalid post identifier specified"
				c.JSON(http.StatusBadRequest, &rest.Response{
					HumanMessage: errstr,
					Message:      e.Error(),
					Status:       http.StatusBadRequest,
					Success:      false,
				})
				return e
			}

			otherID := c.Get("other").(*nerdz.User).ID()
			var post *nerdz.UserPost

			if post, e = nerdz.NewUserPostWhere(&nerdz.UserPost{nerdz.Post{To: otherID, Pid: pid}}); e != nil {
				errstr := "Required post does not exists"
				c.JSON(http.StatusBadRequest, &rest.Response{
					HumanMessage: errstr,
					Message:      e.Error(),
					Status:       http.StatusBadRequest,
					Success:      false,
				})
				return e
			}

			c.Set("post", post)
			return next(c)
		})
	}
}