Example #1
0
func SignupAction(env Env) (status Status, headers Headers, body Body) {

	user := users.NewUser()
	formdata.UnmarshalByNames(env.Request().Request, user,
		[]string{"Email", "Name", "Password", "ConfirmPassword"})

	if validated := user.ValidateSignupForm(); validated.HasError() {
		mangotemplate.ForRender(env, "sessions/signup", &SessionData{User: user, Validated: validated})
		return
	}

	if validated := user.ValidateEmailExist(); validated.HasError() {
		mangotemplate.ForRender(env, "sessions/signup", &SessionData{User: user, Validated: validated})
		return
	}

	if err := user.Signup(); err != nil {
		panic(err)
		return
	}

	services.PutUserIdToSession(env, user.Id.Hex())

	return Redirect(http.StatusFound, "/")
}
Example #2
0
func Edit(env Env) (status Status, headers Headers, body Body) {

	brandId := env.Request().URL.Query().Get(":id")
	brandInput, err := services.EditBrand(brandId)
	if validated, ok := err.(*govalidations.Validated); ok {
		mangotemplate.ForRender(env, "brands/edit", newBrandViewData(brandInput, validated))
		return
	}
	if err != nil {
		panic(err)
	}

	mangotemplate.ForRender(env, "brands/edit", newBrandViewData(brandInput, nil))
	return
}
Example #3
0
func Index(env Env) (status Status, headers Headers, body Body) {

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

	apiBrands, err := services.AllBrands()
	if err != nil {
		panic(err)
	}

	apiUsers, err := services.GetUsers()
	if err != nil {
		panic(err)
	}

	homeViewData := &HomeViewData{
		ApiProducts: apiProducts,
		ApiBrands:   apiBrands,
		ApiUsers:    apiUsers,
	}

	mangotemplate.ForRender(env, "feeds/index", homeViewData)

	return
}
Example #4
0
func Show(env Env) (status Status, headers Headers, body Body) {
	id := env.Request().URL.Query().Get(":id")

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

	apiNotes, err := services.GetUserNotes(id)
	if err != nil {
		panic(err)
	}

	apiPosts, err := services.GetUserPosts(id)
	if err != nil {
		panic(err)
	}

	userViewData := &UserViewData{
		ApiUser:   apiUser,
		ApiNotes:  apiNotes,
		ApiPosts:  apiPosts,
		NewPostId: bson.NewObjectId().Hex(),
		IsCurrent: services.IsCurrentUserWithId(env, id),
	}

	mangotemplate.ForRender(env, "users/show", userViewData)
	return
}
Example #5
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
}
Example #6
0
func LoginPage(env Env) (status Status, headers Headers, body Body) {
	user := services.FetchUserFromEnv(env)
	if user != nil {
		return Redirect(http.StatusFound, "/")
	}
	mangotemplate.ForRender(env, "sessions/login", &SessionData{User: users.NewUser()})
	return
}
Example #7
0
func Index(env Env) (status Status, headers Headers, body Body) {
	a := &accounts.Account{
		Name:       "Felix",
		Gender:     "2",
		Department: "IT",
	}
	mangotemplate.ForRender(env, "home/index", forms.AccountRegisterForm().Render(a, env))
	return
}
Example #8
0
func Index(env Env) (status Status, headers Headers, body Body) {

	apiProducts, err := services.AllProducts()
	if err != nil {
		panic(err)
	}
	mangotemplate.ForRender(env, "products/index", &ProductViewData{ApiProducts: apiProducts})
	return
}
Example #9
0
func Index(env Env) (status Status, headers Headers, body Body) {

	apiBrands, err := services.AllBrands()
	if err != nil {
		panic(err)
	}

	mangotemplate.ForRender(env, "brands/index", &BrandViewData{ApiBrands: apiBrands})
	return
}
Example #10
0
func Index(env Env) (status Status, headers Headers, body Body) {

	viewData := &AdminCategoryViewData{
		CategoryInput: new(duoerlapi.CategoryInput),
		ApiCategories: services.GetCategories(),
	}

	mangotemplate.ForRender(env, "admin/categories", viewData)
	return
}
Example #11
0
func New(env Env) (status Status, headers Headers, body Body) {

	noteInput := services.NewNote()

	noteViewData := &NoteViewData{
		NoteInput: noteInput,
	}

	mangotemplate.ForRender(env, "notes/new", noteViewData)
	return
}
Example #12
0
func LoginAction(env Env) (status Status, headers Headers, body Body) {
	user := users.NewUser()
	formdata.UnmarshalByNames(env.Request().Request, user, []string{"Email", "Password"})

	validated := user.ValidateLoginForm()
	if validated.HasError() {
		mangotemplate.ForRender(env, "sessions/login", &SessionData{User: user, Validated: validated})
		return
	}

	loginUser := users.LoginWith(user.Email, user.Password)
	if loginUser == nil {
		validated.AddError("Password", "User and password do not match!")
		mangotemplate.ForRender(env, "sessions/login", &SessionData{User: user, Validated: validated})
		return
	}

	services.PutUserIdToSession(env, loginUser.Id.Hex())
	return Redirect(http.StatusFound, "/")
}
Example #13
0
func Join(env Env) (status Status, headers Headers, body Body) {
	email := env.Request().FormValue("email")
	if email == "" {
		return Redirect(http.StatusFound, "/")
	}

	r := env.Request()
	mangotemplate.ForRender(env, "chats/room", &RenderData{Email: email, WebSocketHost: r.Host})
	headers = Headers{}
	return
}
Example #14
0
func Create(env Env) (status Status, headers Headers, body Body) {

	brandInput := new(duoerlapi.BrandInput)
	formdata.UnmarshalByNames(env.Request().Request, &brandInput, brandFields)

	result, err := services.CreateBrand(brandInput)
	if validated, ok := err.(*govalidations.Validated); ok {
		mangotemplate.ForRender(env, "brands/new", newBrandViewData(result, validated))
		return
	}
	if err != nil {
		panic(err)
	}

	return Redirect(http.StatusFound, "/brand/"+result.Id)
}
Example #15
0
func New(env Env) (status Status, headers Headers, body Body) {

	newsInput := services.NewNews()
	apiBrands, err := services.AllBrands()
	if err != nil {
		panic(err)
	}

	newsViewData := &NewsViewData{
		NewsInput: newsInput,
		ApiBrands: apiBrands,
	}

	mangotemplate.ForRender(env, "news/new", newsViewData)
	return
}
Example #16
0
func New(env Env) (status Status, headers Headers, body Body) {
	productInput := services.NewProduct()
	apiBrands, err := services.AllBrands()
	if err != nil {
		panic(err)
	}

	productViewData := &ProductViewData{
		ProductInput:  productInput,
		ApiBrands:     apiBrands,
		ApiCategories: services.GetCategories(),
	}

	mangotemplate.ForRender(env, "products/new", productViewData)
	return
}
Example #17
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
}
Example #18
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
}
Example #19
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)
}
Example #20
0
func Update(env Env) (status Status, headers Headers, body Body) {
	newsInput := new(duoerlapi.NewsInput)
	formdata.UnmarshalByNames(env.Request().Request, &newsInput, newsFields)

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

	if err != nil {
		panic(err)
	}

	return Redirect(http.StatusFound, "/news/"+result.Id)
}
Example #21
0
func Update(env Env) (status Status, headers Headers, body Body) {

	productInput := new(duoerlapi.ProductInput)
	formdata.UnmarshalByNames(env.Request().Request, &productInput, productFields)

	result, err := services.UpdateProduct(productInput)
	if validated, ok := err.(*govalidations.Validated); ok {
		viewData := &ProductViewData{
			ProductInput: result,
			Validated:    validated,
		}
		mangotemplate.ForRender(env, "products/edit", viewData)
		return
	}
	if err != nil {
		panic(err)
	}

	return Redirect(http.StatusFound, "/product/"+result.Id)
}
Example #22
0
func Join(env Env) (status Status, headers Headers, body Body) {
	email := env.Request().FormValue("email")
	// Anyadimos contrasenya
	password := env.Request().FormValue("password")
	// Comprobamos ahora tambien la contrasenya
	if email == "" || password == "" {
		return Redirect(http.StatusFound, "/")
	}

	/**
	// Md5 no es para nada seguro por tanto no se debe utilizar y se utilizar sha512
	// Pasamos la contrasenya a hash ya que las tenemos asi en el fichero
	h := md5.New() //Utilizamos md5
	io.WriteString(h, password)
	passwHash := hex.EncodeToString(h.Sum(nil))
	*/

	// Pasamos la contrasenya a sha512
	h := sha512.New() //Utilizamos sha512
	io.WriteString(h, password)
	passwHash := hex.EncodeToString(h.Sum(nil))

	// Leemos desde fichero de texto y comprobamos si existe el usuario
	file, err := os.Open("usuarios.txt")
	check(err)
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		s := strings.Split(scanner.Text(), " ")
		if s[0] == email {
			if s[1] == passwHash {
				r := env.Request()
				mangotemplate.ForRender(env, "chats/room", &RenderData{Email: email, WebSocketHost: r.Host})
				headers = Headers{}
				return
			}
		}
	}

	return Redirect(http.StatusFound, "/")

}
Example #23
0
func Create(env Env) (status Status, headers Headers, body Body) {

	noteInput := new(duoerlapi.NoteInput)
	formdata.UnmarshalByNames(env.Request().Request, &noteInput, noteFields)
	noteInput.AuthorId = services.FetchUserIdFromSession(env)

	result, err := services.CreateNote(noteInput)
	if validated, ok := err.(*govalidations.Validated); ok {
		viewData := &NoteViewData{
			NoteInput: noteInput,
			Validated: validated,
		}
		mangotemplate.ForRender(env, "notes/new", viewData)
		return
	}
	if err != nil {
		panic(err)
	}

	return Redirect(http.StatusFound, "/note/"+result.Id)
}
Example #24
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
}
Example #25
0
func Edit(env Env) (status Status, headers Headers, body Body) {
	newsId := env.Request().URL.Query().Get(":id")
	currentUser := services.FetchUserFromEnv(env)

	newsInput, err := services.EditNews(currentUser, newsId)
	if err != nil {
		panic(err)
	}

	apiBrands, err := services.AllBrands()
	if err != nil {
		panic(err)
	}

	newsViewData := &NewsViewData{
		NewsInput: newsInput,
		ApiBrands: apiBrands,
	}

	mangotemplate.ForRender(env, "news/edit", newsViewData)
	return
}
Example #26
0
func Create(env Env) (status Status, headers Headers, body Body) {
	categoryInput := new(duoerlapi.CategoryInput)
	formdata.UnmarshalByNames(env.Request().Request, &categoryInput, categoryFields)

	viewData := &AdminCategoryViewData{
		CategoryInput: categoryInput,
		ApiCategories: services.GetCategories(),
	}

	_, err := services.CreateCategory(categoryInput)
	if validated, ok := err.(*govalidations.Validated); ok {
		viewData.Validated = validated
		mangotemplate.ForRender(env, "admin/categories", viewData)
		return
	}

	if err != nil {
		panic(err)
	}

	return Redirect(http.StatusFound, "/admin/categories")
}
Example #27
0
func Edit(env Env) (status Status, headers Headers, body Body) {

	productId := env.Request().URL.Query().Get(":id")

	productInput, err := services.EditProduct(productId)
	if err != nil {
		panic(err)
	}

	apiBrands, err := services.AllBrands()
	if err != nil {
		panic(err)
	}

	productViewData := &ProductViewData{
		ProductInput:  productInput,
		ApiBrands:     apiBrands,
		ApiCategories: services.GetCategories(),
	}

	mangotemplate.ForRender(env, "products/edit", productViewData)
	return
}
Example #28
0
func Home(env Env) (status Status, headers Headers, body Body) {
	mangotemplate.ForRender(env, "chats/home", nil)
	headers = Headers{}
	return
}
Example #29
0
func New(env Env) (status Status, headers Headers, body Body) {
	brandInput := services.NewBrand()
	mangotemplate.ForRender(env, "brands/new", newBrandViewData(brandInput, nil))
	return
}