Beispiel #1
0
func HandleGamePage(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	encgameid := params.ByName("wild")
	if encgameid == "new" {
		HandleGamePageNew(w, r, params)
		return
	}
	var err error
	gameid, err := url.QueryUnescape(encgameid)
	if err != nil {
		panic(err)
	}
	var game *models.Game
	game, err = models.Dal.FindGame(gameid)
	if err != nil {
		panic(err)
	}
	game.UpdateRating()
	revs, err := models.Dal.FindTopReviewsByGame(gameid, 5)
	if err != nil {
		panic(err)
	}
	vids, err := models.Dal.FindTopVideosByGame(gameid, 6)
	if err != nil {
		panic(err)
	}
	models.RenderTemplate(w, r, "game/page", map[string]interface{}{"Game": game, "TopReviews": revs, "TopUserVids": vids})
}
Beispiel #2
0
func HandleReviewEditAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		reviewid := params.ByName("wild")
		//reviewid := r.FormValue("reviewID")
		user := models.RequestUser(r)
		//gameid := r.FormValue("gameID")
		review := r.FormValue("reviewBody")
		rating, _ := strconv.ParseFloat(r.FormValue("reviewRate"), 64)
		rev, err := models.Dal.FindReview(reviewid)
		if err != nil {
			panic(err)
		}
		rev.Body = review
		rev.Rating = rating
		if rev.UserId != user.UserId {
			models.RenderTemplate(w, r, "review/edit", map[string]interface{}{"Review": rev})
			return
		}
		err = models.Dal.UpdateReview(*rev)
		if err != nil {
			panic(err)
		}
		http.Redirect(w, r, "/review/"+rev.ReviewId, http.StatusFound)
	}
}
Beispiel #3
0
func HandleLoginAction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	var user *models.User
	next := string(r.FormValue("next"))
	flash := ""
	if r.URL.Query().Get("signup") == "true" {
		uname := r.FormValue("signUser")
		email := r.FormValue("signEmail")
		pword := r.FormValue("signPass")
		repeat := r.FormValue("repeatPass")
		if pword != repeat {
			models.RenderTemplate(w, r, "users/login", map[string]interface{}{"Error": "Passwords don't match.", "UName": uname, "Email": email, "Next": next})
			return
		}
		var err error
		user, err = models.GlobalUserStore.CreateUser(uname, pword, email)
		if err != nil {
			if err == models.ErrUserNameUnavailable {
				models.RenderTemplate(w, r, "users/login", map[string]interface{}{"Error": err.Error(), "UName": uname, "Email": email, "Next": next})
				return
			}
			panic(err)
		}
		if user == nil {
			log.Fatal("Failed to create user.")
		}
		flash = "?flash=Signup+Success"
	} else {
		uname := r.FormValue("loginUser")
		pword := r.FormValue("loginPass")
		var err error
		user, err = models.GlobalUserStore.Authenticate(uname, pword)
		if err != nil {
			models.RenderTemplate(w, r, "users/login", map[string]interface{}{"Error": err.Error(), "UName": uname, "Next": next})
			return
		}
		flash = "?flash=Login+Success"
	}
	next, err := url.QueryUnescape(next)
	if err != nil {
		next = ""
	}
	if next == "" {
		next = "/"
	}
	models.FindOrCreateSession(w, r, user.UserId)
	http.Redirect(w, r, next+flash, http.StatusFound)
}
Beispiel #4
0
func HandleReview(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	reviewid := params.ByName("wild")
	rev, err := models.Dal.FindReview(reviewid)
	if err != nil {
		panic(err)
	}
	models.RenderTemplate(w, r, "review/review", map[string]interface{}{"Review": rev})
}
Beispiel #5
0
func HandleVideo(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	videoid := params.ByName("wild")
	vid, err := models.Dal.FindVideo(videoid)
	if err != nil {
		panic(err)
	}
	models.RenderTemplate(w, r, "video/uservideo", map[string]interface{}{"Video": vid, "VideoWidth": 640, "VideoHeight": 480})
}
Beispiel #6
0
func HandleIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	randgames := []models.Game{}
	var err error
	randgames, err = models.Dal.GetGames(20, 0)
	if err != nil {
		panic(err)
	}
	models.RenderTemplate(w, r, "home/index", map[string]interface{}{"RandomGames": randgames})
}
Beispiel #7
0
func HandleReviewNew(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		gameid := params.ByName("wild")
		gam, err := models.Dal.FindGame(gameid)
		if err != nil {
			panic(err)
		}
		models.RenderTemplate(w, r, "review/new", map[string]interface{}{"Game": gam})
	}
}
Beispiel #8
0
func HandleLogout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	sess := models.RequestSession(r)
	if sess != nil {
		err := models.GlobalSessionStore.Delete(sess)
		if err != nil {
			panic(err)
		}
	}
	models.RenderTemplate(w, r, "users/logout", nil)
}
Beispiel #9
0
func HandleFriendAdd(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		friendid := params.ByName("wild")
		friend, err := models.GlobalUserStore.FindUser(friendid)
		if err != nil {
			panic(err)
		}
		user := models.RequestUser(r)
		if user == nil || friend == nil || user.UserId == friend.UserId {
			models.RenderTemplate(w, r, "users/friend", nil)
			return
		}
		err = models.Dal.AddUserFriend(user.UserId, friendid)
		if err != nil {
			panic(err)
		}
		models.RenderTemplate(w, r, "users/friend", map[string]interface{}{"Friend": friend})
	}
}
Beispiel #10
0
func HandleUserVideos(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	userid := params.ByName("wild")
	user, err := models.GlobalUserStore.FindUser(userid)
	if err != nil {
		panic(err)
	}
	var vids []models.Video
	vids, err = models.Dal.FindVideosByUser(user.UserId)
	if err != nil {
		panic(err)
	}
	models.RenderTemplate(w, r, "videos/alluservideos", map[string]interface{}{"User": user, "AllVideos": vids})
}
Beispiel #11
0
func HandleGameVideos(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	gameid := params.ByName("wild")
	game, err := models.Dal.FindGame(gameid)
	if err != nil {
		panic(err)
	}
	var vids []models.Video
	vids, err = models.Dal.FindVideosByGame(gameid)
	if err != nil {
		panic(err)
	}
	models.RenderTemplate(w, r, "videos/allgamevideos", map[string]interface{}{"Game": game, "AllVideos": vids})
}
Beispiel #12
0
func HandleAccountPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	if models.SignedIn(w, r) {
		user := models.RequestUser(r)
		games, err := models.Dal.GetGamesList(user.UserId)
		if err != nil {
			panic(err)
		}
		user.Games = games
		friends, err := models.Dal.GetFriendsList(user.UserId)
		if err != nil {
			panic(err)
		}
		user.Friends = friends
		models.RenderTemplate(w, r, "users/account", map[string]interface{}{"CurrentUser": user})
	}
}
Beispiel #13
0
func HandleSearch(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	tag := r.URL.Query().Get("tag")
	nameinclude := r.URL.Query().Get("NameIncludes")
	res := []models.Game{}
	var err error
	if tag != "" {
		var tagres []models.Game
		tagres, err = models.Dal.FindGamesByTag(tag)
		res = append(res, tagres...)
	}
	if nameinclude != "" {
		var namres []models.Game
		namres, err = models.Dal.SearchGames(nameinclude)
		res = append(res, namres...)
	}
	models.RenderTemplate(w, r, "game/search", map[string]interface{}{"NameIncludes": nameinclude, "Tag": tag, "SearchResults": res, "Error": err})
}
Beispiel #14
0
func HandleUserPage(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	userid := params.ByName("wild")
	user, err := models.GlobalUserStore.FindUser(userid)
	if err != nil {
		panic(err)
	}
	var vids []models.Video
	var revs []models.Review
	vids, err = models.Dal.FindTopVideosByUser(userid, 10)
	if err != nil {
		panic(err)
	}
	revs, err = models.Dal.FindTopReviewsByUser(userid, 10)
	if err != nil {
		panic(err)
	}
	models.RenderTemplate(w, r, "users/view", map[string]interface{}{"User": user, "Reviews": revs, "Videos": vids})
}
Beispiel #15
0
func HandleGameEditPage(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		gameid := params.ByName("wild")
		game, err := models.Dal.FindGame(gameid)
		if err != nil {
			panic(err)
		}
		if game == nil {
			http.Redirect(w, r, "/?flash=Game+Not+Found", http.StatusNotFound)
			return
		}
		tags := []string{}
		tags, err = models.Dal.FindTagsByGame(gameid)
		if err != nil {
			panic(err)
		}
		models.RenderTemplate(w, r, "game/edit", map[string]interface{}{"Game": game, "Tags": tags})
	}
}
Beispiel #16
0
func HandleGamePageNew(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	if models.SignedIn(w, r) {
		models.RenderTemplate(w, r, "game/new", nil)
	}
}
Beispiel #17
0
func HandleLoginPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	models.RenderTemplate(w, r, "users/login", map[string]interface{}{"Next": r.URL.Query().Get("next")})
}
Beispiel #18
0
func HandleAccountAction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	if models.SignedIn(w, r) {
		//update user information
		username := r.FormValue("accountName")
		email := r.FormValue("accountEmail")
		newPassword := r.FormValue("accountNewPassword")
		confirmNewPassword := r.FormValue("confirmPassword")
		oldPassword := r.FormValue("accountPassword")
		friendarrstr := r.FormValue("accountFriendList")
		gamearrstr := r.FormValue("accountGameList")

		user, autherr := models.GlobalUserStore.Authenticate(username, oldPassword)
		if user == nil {
			user = models.RequestUser(r)
		}

		friends, err := models.Dal.GetFriendsList(user.UserId)
		if err != nil {
			panic(err)
		}
		user.Friends = friends
		var friendarr []string
		err = json.Unmarshal([]byte(friendarrstr), &friendarr)
		if err != nil || friendarr == nil {
			friendarr = []string{}
		}
		newfriendlist := []models.User{}
		delfriendlist := []string{}
		for _, friend := range user.Friends {
			found := false
			for _, id := range friendarr {
				if friend.UserId == id {
					found = true
				}
			}
			if found {
				newfriendlist = append(newfriendlist, friend)
			} else {
				delfriendlist = append(delfriendlist, friend.UserId)
			}
		}
		user.Friends = newfriendlist

		games, err := models.Dal.GetGamesList(user.UserId)
		if err != nil {
			panic(err)
		}
		user.Games = games
		var gamearr []string
		err = json.Unmarshal([]byte(gamearrstr), &gamearr)
		if err != nil || gamearr == nil {
			gamearr = []string{}
		}
		newgamelist := []models.Game{}
		delgamelist := []string{}
		for _, game := range user.Games {
			found := false
			for _, id := range gamearr {
				if game.GameId == id {
					found = true
				}
			}
			if found {
				newgamelist = append(newgamelist, game)
			} else {
				delgamelist = append(delgamelist, game.GameId)
			}
		}
		user.Games = newgamelist

		if autherr != nil {
			models.RenderTemplate(w, r, "users/account", map[string]interface{}{"Error": autherr.Error(), "CurrentUser": user})
			return
		}

		user.Email = email
		if newPassword != "" {
			if newPassword != confirmNewPassword {
				models.RenderTemplate(w, r, "users/account", map[string]interface{}{"Error": "Passwords do not match.", "CurrentUser": user})
				return
			}
			user.SetPassword(newPassword)
		}

		err = models.Dal.UpdateUser(*user)
		if err != nil {
			panic(err)
		}
		for _, remid := range delfriendlist {
			err = models.Dal.DeleteUserFriend(user.UserId, remid)
			if err != nil {
				panic(err)
			}
		}
		for _, remid := range delgamelist {
			err = models.Dal.DeleteUserGame(user.UserId, remid)
			if err != nil {
				panic(err)
			}
		}

		models.RenderTemplate(w, r, "users/account", map[string]interface{}{"CurrentUser": user})
	}
}