Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
Get poll id from option id (#174)
Browse files Browse the repository at this point in the history
* Get poll id from option id

* # Update error messages

An error message should tell what you did when it failed,
it should not be the function name where the error
happend.

# Renamed function name

A function name should not start with "get".

# Fetch poll_id with GetIfExist

This seems nicer as fetching the second field inside the error check.

Co-authored-by: Oskar Hahn <mail@oshahn.de>
  • Loading branch information
FinnStutzenstein and ostcar committed Mar 15, 2021
1 parent 8caf04b commit e30d357
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions internal/collection/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (p *poll) readOption(ctx context.Context, userID int, fqfields []perm.FQFie
}

return p.fields(fqfields, result, restricted, func(fqfield perm.FQField) (int, error) {
var pollID int
if err := p.dp.Get(ctx, fmt.Sprintf("option/%d/poll_id", fqfield.ID), &pollID); err != nil {
return 0, fmt.Errorf("getting poll id: %w", err)
pollID, err := pollIDFromOption(ctx, p.dp, fqfield.ID)
if err != nil {
return 0, fmt.Errorf("fetch poll id: %w", err)
}
return p.pollPerm(ctx, userID, pollID)
})
Expand All @@ -66,9 +66,9 @@ func (p *poll) readVote(ctx context.Context, userID int, fqfields []perm.FQField
return false, fmt.Errorf("getting option id: %w", err)
}

var pollID int
if err := p.dp.Get(ctx, fmt.Sprintf("option/%d/poll_id", optionID), &pollID); err != nil {
return false, fmt.Errorf("getting poll id: %w", err)
pollID, err := pollIDFromOption(ctx, p.dp, optionID)
if err != nil {
return false, fmt.Errorf("fetch poll id: %w", err)
}

perms, err := p.pollPerm(ctx, userID, pollID)
Expand Down Expand Up @@ -288,9 +288,9 @@ func canSeePolls(ctx context.Context, dp dataprovider.DataProvider, perms *perm.

func canSeePollOptions(ctx context.Context, dp dataprovider.DataProvider, perms *perm.Permission, userID int, ids []int) (bool, error) {
for _, id := range ids {
var pollID int
if err := dp.Get(ctx, fmt.Sprintf("option/%d/poll_id", id), &pollID); err != nil {
return false, fmt.Errorf("getting poll id: %w", err)
pollID, err := pollIDFromOption(ctx, dp, id)
if err != nil {
return false, fmt.Errorf("fetch poll id: %w", err)
}

var contentObject string
Expand All @@ -308,3 +308,24 @@ func canSeePollOptions(ctx context.Context, dp dataprovider.DataProvider, perms
}
return false, nil
}

// pollIDFromOption returns the poll id from an option.
//
// An option is linked to the poll via poll_id xor
// used_as_global_option_in_poll_id.
func pollIDFromOption(ctx context.Context, dp dataprovider.DataProvider, optionID int) (int, error) {
var pollID int
if err := dp.GetIfExist(ctx, fmt.Sprintf("option/%d/poll_id", optionID), &pollID); err != nil {
return 0, fmt.Errorf("poll id from field `poll_id`: %w", err)
}

if pollID != 0 {
// Option has the field `poll_id`
return pollID, nil
}

if err := dp.Get(ctx, fmt.Sprintf("option/%d/used_as_global_option_in_poll_id", optionID), &pollID); err != nil {
return 0, fmt.Errorf("poll id from field `used_as_global_option_in_poll_id`: %w", err)
}
return pollID, nil
}

0 comments on commit e30d357

Please sign in to comment.