Exemplo n.º 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, "/")
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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, "/")
}
Exemplo n.º 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
}
Exemplo n.º 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)
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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)
}
Exemplo n.º 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)
}
Exemplo n.º 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)
}
Exemplo n.º 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, "/")

}
Exemplo n.º 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)
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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")
}
Exemplo n.º 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
}
Exemplo n.º 28
0
func Home(env Env) (status Status, headers Headers, body Body) {
	mangotemplate.ForRender(env, "chats/home", nil)
	headers = Headers{}
	return
}
Exemplo n.º 29
0
func New(env Env) (status Status, headers Headers, body Body) {
	brandInput := services.NewBrand()
	mangotemplate.ForRender(env, "brands/new", newBrandViewData(brandInput, nil))
	return
}