Ejemplo n.º 1
0
func Show(env Env) (status Status, headers Headers, body Body) {
	productId := env.Request().URL.Query().Get(":id")
	currentUserId := services.FetchUserIdFromSession(env)

	// Get Product
	apiProduct, err := services.ShowProduct(productId, currentUserId)
	if err != nil {
		panic(err)
	}

	// Get Product Reviews
	apiReviews, err := services.GetReviewsInProduct(productId)
	if err != nil {
		panic(err)
	}

	// Init new Review Form data
	productViewData := &ProductViewData{
		ApiProduct:     apiProduct,
		ApiReviews:     apiReviews,
		ReviewInput:    services.NewReview(),
		GotFromOptions: global.GotFromOptions,
		RatingOptions:  global.RatingOptions,
	}

	mangotemplate.ForRender(env, "products/show", productViewData)
	return
}
Ejemplo n.º 2
0
func Create(env Env) (status Status, headers Headers, body Body) {

	productId := env.Request().FormValue("pid")
	userId := services.FetchUserIdFromSession(env)

	err := services.AddWishItem(userId, productId)
	if err != nil {
		panic(err)
	}

	return
}
Ejemplo n.º 3
0
func AuthenticateUser() Middleware {
	return func(env Env, app App) (status Status, headers Headers, body Body) {
		userId := services.FetchUserIdFromSession(env)
		if userId != "" {
			if user, _ := users.FindById(bson.ObjectIdHex(userId)); user != nil {
				services.PutUserToEnv(env, user)
			}
		}

		return app(env)
	}
}
Ejemplo n.º 4
0
func Create(env Env) (status Status, headers Headers, body Body) {

	brandId := env.Request().FormValue("bid")
	userId := services.FetchUserIdFromSession(env)

	err := services.CreateFollowBrand(userId, brandId)
	if err != nil {
		panic(err)
	}

	return
}
Ejemplo n.º 5
0
func Update(env Env) (status Status, headers Headers, body Body) {

	userInput := &duoerlapi.UserInput{Id: services.FetchUserIdFromSession(env)}
	formdata.UnmarshalByNames(env.Request().Request, userInput, userFields)

	err := services.UpdateProfile(userInput)
	if err != nil {
		panic(err)
	}

	return Redirect(http.StatusFound, "/user/edit")
}
Ejemplo n.º 6
0
func Create(env Env) (status Status, headers Headers, body Body) {

	ownItemInput := new(duoerlapi.OwnItemInput)
	formdata.UnmarshalByNames(env.Request().Request, &ownItemInput, ownItemFields)

	ownItemInput.UserId = services.FetchUserIdFromSession(env)

	err := services.AddOwnItem(ownItemInput)
	if err != nil {
		panic(err)
	}

	return
}
Ejemplo n.º 7
0
func Show(env Env) (status Status, headers Headers, body Body) {
	newsId := env.Request().URL.Query().Get(":id")
	currentUserId := services.FetchUserIdFromSession(env)

	apiNews, err := services.ShowNews(newsId, currentUserId)
	if err != nil {
		panic(err)
	}

	newsViewData := &NewsViewData{
		ApiNews: apiNews,
	}

	mangotemplate.ForRender(env, "news/show", newsViewData)
	return
}
Ejemplo n.º 8
0
func HardAuthenUser() Middleware {
	return func(env Env, app App) (status Status, headers Headers, body Body) {
		userId := services.FetchUserIdFromSession(env)
		if userId == "" {
			return Redirect(http.StatusFound, "/logout")
		}

		user, _ := users.FindById(bson.ObjectIdHex(userId))
		if user == nil {
			return Redirect(http.StatusFound, "/logout")
		}

		services.PutUserToEnv(env, user)
		return app(env)
	}
}
Ejemplo n.º 9
0
func Edit(env Env) (status Status, headers Headers, body Body) {
	id := services.FetchUserIdFromSession(env)

	apiUser, err := services.GetUser(id)
	if err != nil {
		panic(err)
	}

	userViewData := &UserViewData{
		ApiUser:            apiUser,
		SkinTextureOptions: global.SkinTextureOptions,
		HairTextureOptions: global.HairTextureOptions,
	}

	mangotemplate.ForRender(env, "users/edit", userViewData)
	return
}
Ejemplo n.º 10
0
func Create(env Env) (status Status, headers Headers, body Body) {

	reviewInput := new(duoerlapi.ReviewInput)
	formdata.UnmarshalByNames(env.Request().Request, &reviewInput, reviewFields)
	reviewInput.AuthorId = services.FetchUserIdFromSession(env)

	result, err := services.CreateReview(reviewInput)
	if validated, ok := err.(*govalidations.Validated); ok {
		// TODO: Ajax needed here
		mangotemplate.ForRender(env, "products/new", validated)
		return
	}
	if err != nil {
		panic(err)
	}

	return Redirect(http.StatusFound, "/product/"+result.ProductId)
}
Ejemplo n.º 11
0
func Create(env Env) (status Status, headers Headers, body Body) {

	postInput := new(duoerlapi.PostInput)
	formdata.UnmarshalByNames(env.Request().Request, &postInput, postFields)
	postInput.AuthorId = services.FetchUserIdFromSession(env)

	result, err := services.CreatePost(postInput)
	if validated, ok := err.(*govalidations.Validated); ok {
		b, _ := json.Marshal(validated)
		body = Body(b)
		return
	}
	if err != nil {
		panic(err)
	}

	b, _ := json.Marshal(result)
	body = Body(b)
	return
}
Ejemplo n.º 12
0
func Create(env Env) (status Status, headers Headers, body Body) {

	newsInput := new(duoerlapi.NewsInput)
	formdata.UnmarshalByNames(env.Request().Request, &newsInput, newsFields)
	newsInput.AuthorId = services.FetchUserIdFromSession(env)

	result, err := services.CreateNews(newsInput)
	if validated, ok := err.(*govalidations.Validated); ok {
		viewData := &NewsViewData{
			NewsInput: newsInput,
			Validated: validated,
		}
		mangotemplate.ForRender(env, "news/new", viewData)
		return
	}
	if err != nil {
		panic(err)
	}

	return Redirect(http.StatusFound, "/news/"+result.Id)
}
Ejemplo n.º 13
0
func Show(env Env) (status Status, headers Headers, body Body) {
	brandIdHex := env.Request().URL.Query().Get(":id")
	currentUserId := services.FetchUserIdFromSession(env)

	apiBrand, err := services.ShowBrand(brandIdHex, currentUserId)
	if err != nil {
		panic(err)
	}

	apiProducts, err := services.GetBrandProducts(brandIdHex)
	if err != nil {
		panic(err)
	}

	brandFollowers, err := services.GetBrandFollowers(brandIdHex)
	if err != nil {
		panic(err)
	}

	apiReviews, err := services.GetReviewsInBrand(brandIdHex)
	if err != nil {
		panic(err)
	}

	apiNews, err := services.GetNewsInBrand(brandIdHex)
	if err != nil {
		panic(err)
	}

	brandViewData := &BrandViewData{
		ApiBrand:       apiBrand,
		ApiProducts:    apiProducts,
		BrandFollowers: brandFollowers,
		ApiReviews:     apiReviews,
		ApiNews:        apiNews,
	}

	mangotemplate.ForRender(env, "brands/show", brandViewData)
	return
}
Ejemplo n.º 14
0
func Like(env Env) (status Status, headers Headers, body Body) {
	reviewIdHex := env.Request().FormValue("rid")
	userIdHex := services.FetchUserIdFromSession(env)

	count, err := services.LikeReview(userIdHex, reviewIdHex)
	viewData := &ReviewViewData{LikeCount: count}
	if validated, ok := err.(*govalidations.Validated); ok {
		viewData.Validated = validated
		b, _ := json.Marshal(viewData)
		body = Body(b)
		return
	}

	if err != nil {
		panic(err)
	}

	b, _ := json.Marshal(viewData)
	body = Body(b)

	return
}