Ejemplo n.º 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)
	}
}
Ejemplo n.º 2
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)
	}
}
Ejemplo n.º 3
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)
	}
}
Ejemplo n.º 4
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)
	}
}
Ejemplo n.º 5
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))

	}
}
Ejemplo n.º 6
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})
	}
}
Ejemplo n.º 7
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})
	}
}
Ejemplo n.º 8
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})
	}
}