Exemple #1
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)
	}
}
Exemple #2
0
func HandleGamePageNewAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if params.ByName("wild") == "new" && models.SignedIn(w, r) {
		title := r.FormValue("gameTitle")
		dev := r.FormValue("gameDeveloper")
		pub := r.FormValue("gamePublisher")
		trailer := r.FormValue("gameTrailer")
		//copy := r.FormValue("gameCopyright")
		desc := r.FormValue("gameDescription")
		tagstr := r.FormValue("gameTags")
		var tags []string
		err := json.Unmarshal([]byte(tagstr), &tags)
		if err != nil || tags == nil {
			tags = []string{}
		}
		game := models.NewGame(title, pub, trailer)
		game.Description = desc
		game.Developer = dev
		err = models.Dal.CreateGame(*game)
		if err != nil {
			panic(err)
		}
		for _, tag := range tags {
			models.Dal.AddGameTag(game.GameId, tag)
		}
		http.Redirect(w, r, "/game/"+url.QueryEscape(game.GameId), http.StatusFound)
	} else {
		http.NotFound(w, r)
	}
}
Exemple #3
0
func HandleReviewEdit(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		revid := params.ByName("wild")
		rev, err := models.Dal.FindReview(revid)
		if err != nil {
			panic(err)
		}
		models.RenderTemplate(w, r, "review/edit", map[string]interface{}{"Review": rev})
	}
}
Exemple #4
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})
	}
}
Exemple #5
0
func HandleGameClaimAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		gameid := params.ByName("wild")
		err := models.Dal.AddUserGame(models.RequestUser(r).UserId, gameid)
		if err != nil {
			panic(err)
		}
		http.Redirect(w, r, "/game/"+url.QueryEscape(gameid), http.StatusFound)
	}
}
Exemple #6
0
func HandleReviewNewAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		gameid := 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 := models.NewReview(user.UserId, gameid, review, rating)
		http.Redirect(w, r, "/review/"+rev.ReviewId, http.StatusFound)
	}
}
Exemple #7
0
func HandleVideoNewAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		gameid := params.ByName("wild")
		vidurl := r.FormValue("vidURL")
		user := models.RequestUser(r)
		vid := models.NewVideo(user.UserId, gameid, vidurl)
		err := models.Dal.CreateVideo(*vid)
		if err != nil {
			panic(err)
		}
		http.Redirect(w, r, "/videos/"+url.QueryEscape(vid.ID), http.StatusFound)
	}
}
Exemple #8
0
func HandleGameEditAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	if models.SignedIn(w, r) {
		gameid := params.ByName("wild")
		title := r.FormValue("gameTitle")
		dev := r.FormValue("gameDeveloper")
		pub := r.FormValue("gamePublisher")
		trailer := r.FormValue("gameTrailer")
		//copy := r.FormValue("gameCopyright")
		desc := r.FormValue("gameDescription")
		tagstr := r.FormValue("gameTags")
		var tags []string
		err := json.Unmarshal([]byte(tagstr), &tags)
		if err != nil || tags == nil {
			tags = []string{}
		}
		for k, v := range tags {
			t, err := url.QueryUnescape(v)
			if err == nil {
				tags[k] = t
			}
		}
		var currtags []string
		currtags, err = models.Dal.FindTagsByGame(gameid)
		if err != nil || currtags == nil {
			currtags = []string{}
		}
		game := models.Game{GameId: gameid, Title: title, Developer: dev, Publisher: pub, URL: trailer, Description: desc}
		err = models.Dal.UpdateGame(game)
		if err != nil {
			panic(err)
		}
		for _, t := range currtags {
			found := false
			for k, v := range tags {
				if t == v {
					found = true
					tags = append(tags[:k], tags[k+1:]...)
					break
				}
			}
			if !found {
				models.Dal.DeleteGameTag(gameid, t)
			}
		}
		for _, t := range tags {
			models.Dal.AddGameTag(gameid, t)
		}
		http.Redirect(w, r, "/game/"+url.QueryEscape(game.GameId), http.StatusFound)
	}
}
Exemple #9
0
func HandleChatAction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	if models.SignedIn(w, r) {
		//TODO: send the chatNewMsg to chatTo
		user := models.RequestUser(r)
		//toUser := r.FormValue("chatTo")
		chatID := r.FormValue("chatToID")
		theMessage := r.FormValue("chatNewMsg")
		chatto, err := models.Dal.FindUser(chatID)
		if err != nil {
			panic(err)
		}
		models.Dal.SendMessage(*models.NewMessage(*user, *chatto, theMessage))

	}
}
Exemple #10
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})
	}
}
Exemple #11
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})
	}
}
Exemple #12
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})
	}
}
Exemple #13
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})
	}
}
Exemple #14
0
func HandleGamePageNew(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	if models.SignedIn(w, r) {
		models.RenderTemplate(w, r, "game/new", nil)
	}
}