Skip to content

Commit

Permalink
Fix vote count on account page
Browse files Browse the repository at this point in the history
This change consolidates a bunch of the vote count logic into a single
function.  I'm not completely happy with this solution, but it works.

Fixes #65
  • Loading branch information
zorchenhimer committed Jun 29, 2020
1 parent e6228aa commit ef4f7bc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 51 deletions.
27 changes: 10 additions & 17 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,29 +571,22 @@ func (s *Server) handlerRoot(w http.ResponseWriter, r *http.Request) {
}

if data.User != nil {
votedMovies, err := s.data.GetUserVotes(data.User.Id)
maxVotes, err := s.data.GetCfgInt("MaxUserVotes", DefaultMaxUserVotes)
if err != nil {
s.l.Error("Error getting MaxUserVotes config setting: %v", err)
maxVotes = DefaultMaxUserVotes
}

active, _, err := s.getUserVotes(data.User)
if err != nil {
s.doError(
http.StatusBadRequest,
fmt.Sprintf("Cannot get user votes: %v", err),
fmt.Sprintf("Cannot get user votes :C"),
w, r)
s.l.Error("Unable to get votes for user %d: %v", data.User.Id, err)
return
}

count := 0
for _, movie := range votedMovies {
// Only count active movies
if movie.CycleWatched == nil && movie.Removed == false {
count++
}
}

maxVotes, err := s.data.GetCfgInt("MaxUserVotes", DefaultMaxUserVotes)
if err != nil {
s.l.Error("Error getting MaxUserVotes config setting: %v", err)
maxVotes = DefaultMaxUserVotes
}
data.AvailableVotes = maxVotes - count
data.AvailableVotes = maxVotes - len(active)
}

data.Movies = common.SortMoviesByVotes(movieList)
Expand Down
22 changes: 0 additions & 22 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,6 @@ func (d dataAddMovie) isError() bool {
return d.ErrTitle || d.ErrDescription || d.ErrLinks || d.ErrPoster || d.ErrAutofill || d.ErrRemarks
}

type dataAccount struct {
dataPageBase

CurrentVotes []*common.Movie
TotalVotes int
AvailableVotes int

SuccessMessage string

PassError []string
NotifyError []string
EmailError []string

ErrCurrentPass bool
ErrNewPass bool
ErrEmail bool
}

func (a dataAccount) IsErrored() bool {
return a.ErrCurrentPass || a.ErrNewPass || a.ErrEmail
}

type dataError struct {
dataPageBase

Expand Down
11 changes: 10 additions & 1 deletion templates/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@
next to each entry here to easily remove votes.
*/}}
<ul>
{{if .CurrentVotes}}{{range .CurrentVotes}}<li><a href="/movie/{{.Id}}">{{.Name}}</a></li>{{end}}
{{if .ActiveVotes}}{{range .ActiveVotes}}<li><a href="/movie/{{.Id}}">{{.Name}}</a></li>{{end}}
{{else}}<li>No votes :c</li>{{end}}
</ul>
</div>

<div>Past Votes</div>
<div>
<ul>
{{if .WatchedVotes}}
{{range .WatchedVotes}}<li><a href="/movie/{{.Id}}">{{.Name}}</a></li>{{end}}
{{else}}<li>No Votes :c</li>{{end}}
</ul>
</div>
</div>
{{end}}
63 changes: 52 additions & 11 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import (
"github.com/zorchenhimer/MoviePolls/common"
)

// Returns current active votes and votes for watched movies
func (s *Server) getUserVotes(user *common.User) ([]*common.Movie, []*common.Movie, error) {
voted, err := s.data.GetUserVotes(user.Id)

This comment has been minimized.

Copy link
@CptPie

CptPie Jun 29, 2020

Collaborator

Maybe the naming of these methods could be improved since they are called the same atm.

This comment has been minimized.

Copy link
@zorchenhimer

zorchenhimer Jun 29, 2020

Author Owner

Ideally, this function would be in either a "business" layer or the data layer, not in the front-end code. Like I said, I'm not completely satisfied with this.

if err != nil {
return nil, nil, fmt.Errorf("Unable to get all user votes for ID %d: %v", user.Id, err)
}

current := []*common.Movie{}
watched := []*common.Movie{}

for _, movie := range voted {
if movie.Removed == true {
continue
}

if movie.CycleWatched == nil {
current = append(current, movie)
} else {
watched = append(watched, movie)
}
}

return current, watched, nil
}

func (s *Server) handlerUser(w http.ResponseWriter, r *http.Request) {
user := s.getSessionUser(w, r)
if user == nil {
Expand All @@ -22,22 +47,38 @@ func (s *Server) handlerUser(w http.ResponseWriter, r *http.Request) {
totalVotes = DefaultMaxUserVotes
}

currentVotes, err := s.data.GetUserVotes(user.Id)
activeVotes, watchedVotes, err := s.getUserVotes(user)
if err != nil {
s.doError(
http.StatusBadRequest,
fmt.Sprintf("Cannot get user votes: %v", err),
w, r)
return
s.l.Error("Unable to get votes for user %d: %v", user.Id, err)
}

data := dataAccount{
data := struct {
dataPageBase

TotalVotes int
AvailableVotes int

ActiveVotes []*common.Movie
WatchedVotes []*common.Movie

SuccessMessage string

PassError []string
NotifyError []string
EmailError []string

ErrCurrentPass bool
ErrNewPass bool
ErrEmail bool
}{
dataPageBase: s.newPageBase("Account", w, r),

CurrentVotes: currentVotes,
TotalVotes: totalVotes,
TotalVotes: totalVotes,
AvailableVotes: totalVotes - len(activeVotes),

ActiveVotes: activeVotes,
WatchedVotes: watchedVotes,
}
data.AvailableVotes = totalVotes - len(data.CurrentVotes)

if r.Method == "POST" {
err := r.ParseForm()
Expand Down Expand Up @@ -69,7 +110,7 @@ func (s *Server) handlerUser(w http.ResponseWriter, r *http.Request) {
data.PassError = append(data.PassError, "Passwords do not match")
}

if !data.IsErrored() {
if !(data.ErrCurrentPass || data.ErrNewPass || data.ErrEmail) {
// Change pass
data.SuccessMessage = "Password successfully changed"
user.Password = s.hashPassword(newPass1_raw)
Expand Down

0 comments on commit ef4f7bc

Please sign in to comment.