cacert-boardvoting/internal/models/motions.go

891 lines
20 KiB
Go
Raw Normal View History

2022-05-15 18:10:49 +00:00
/*
Copyright 2017-2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package models
import (
2022-05-21 12:27:46 +00:00
"context"
2022-05-21 11:51:17 +00:00
"database/sql"
"database/sql/driver"
2022-05-21 11:51:17 +00:00
"errors"
2022-05-15 18:10:49 +00:00
"fmt"
"log"
"strings"
2022-05-15 18:10:49 +00:00
"time"
"github.com/jmoiron/sqlx"
)
type VoteType struct {
label string
id uint8
}
2022-05-15 18:10:49 +00:00
const unknownVariant = "unknown"
var (
VoteTypeMotion = &VoteType{label: "motion", id: 0}
VoteTypeVeto = &VoteType{label: "veto", id: 1}
2022-05-15 18:10:49 +00:00
)
func (v *VoteType) String() string {
return v.label
2022-05-15 18:10:49 +00:00
}
func VoteTypeFromString(label string) (*VoteType, error) {
for _, vt := range []*VoteType{VoteTypeMotion, VoteTypeVeto} {
if strings.EqualFold(vt.label, label) {
return vt, nil
}
2022-05-15 18:10:49 +00:00
}
return nil, fmt.Errorf("unknown vote type %s", label)
}
func VoteTypeFromInt(id int64) (*VoteType, error) {
for _, vt := range []*VoteType{VoteTypeMotion, VoteTypeVeto} {
if int64(vt.id) == id {
return vt, nil
}
}
return nil, fmt.Errorf("unknown vote type id %d", id)
2022-05-15 18:10:49 +00:00
}
func (v *VoteType) Scan(src any) error {
value, ok := src.(int64)
if !ok {
return fmt.Errorf("could not cast %v of %T to uint8", src, src)
}
vt, err := VoteTypeFromInt(value)
if err != nil {
return err
}
*v = *vt
return nil
}
func (v *VoteType) Value() (driver.Value, error) {
return int64(v.id), nil
}
func (v *VoteType) QuorumAndMajority() (int, float32) {
2022-05-15 18:10:49 +00:00
const (
majorityDefault = 0.99
majorityMotion = 0.50
quorumDefault = 1
quorumMotion = 3
)
if v == VoteTypeMotion {
return quorumMotion, majorityMotion
}
return quorumDefault, majorityDefault
}
type VoteStatus struct {
Label string
Id int8
}
2022-05-15 18:10:49 +00:00
var (
voteStatusDeclined = &VoteStatus{Label: "declined", Id: -1}
voteStatusPending = &VoteStatus{Label: "pending", Id: 0}
voteStatusApproved = &VoteStatus{Label: "approved", Id: 1}
voteStatusWithdrawn = &VoteStatus{Label: "withdrawn", Id: -2}
2022-05-15 18:10:49 +00:00
)
func VoteStatusFromInt(id int64) (*VoteStatus, error) {
for _, vs := range []*VoteStatus{voteStatusPending, voteStatusApproved, voteStatusWithdrawn, voteStatusDeclined} {
if int64(vs.Id) == id {
return vs, nil
}
}
return nil, fmt.Errorf("unknown vote status id %d", id)
}
func (v *VoteStatus) String() string {
return v.Label
2022-05-15 18:10:49 +00:00
}
func (v *VoteStatus) Scan(src any) error {
value, ok := src.(int64)
if !ok {
return fmt.Errorf("could not cast %v of %T to uint8", src, src)
}
vs, err := VoteStatusFromInt(value)
if err != nil {
return err
2022-05-15 18:10:49 +00:00
}
*v = *vs
return nil
}
func (v *VoteStatus) Value() (driver.Value, error) {
return int64(v.Id), nil
2022-05-15 18:10:49 +00:00
}
type VoteChoice struct {
label string
id int8
}
2022-05-15 18:10:49 +00:00
var (
VoteAye = &VoteChoice{label: "aye", id: 1}
VoteNaye = &VoteChoice{label: "naye", id: -1}
VoteAbstain = &VoteChoice{label: "abstain", id: 0}
2022-05-15 18:10:49 +00:00
)
func VoteChoiceFromString(label string) (*VoteChoice, error) {
for _, vc := range []*VoteChoice{VoteAye, VoteNaye, VoteAbstain} {
if strings.EqualFold(vc.label, label) {
return vc, nil
}
}
return nil, fmt.Errorf("unknown vote choice %s", label)
2022-05-15 18:10:49 +00:00
}
func VoteChoiceFromInt(id int64) (*VoteChoice, error) {
for _, vc := range []*VoteChoice{VoteAye, VoteNaye, VoteAbstain} {
if int64(vc.id) == id {
return vc, nil
}
}
return nil, fmt.Errorf("unknown vote type id %d", id)
}
func (v *VoteChoice) String() string {
return v.label
}
func (v *VoteChoice) Scan(src any) error {
value, ok := src.(int64)
if !ok {
return fmt.Errorf("could not cast %v of %T to uint8", src, src)
2022-05-15 18:10:49 +00:00
}
vc, err := VoteChoiceFromInt(value)
if err != nil {
return err
}
*v = *vc
return nil
}
func (v *VoteChoice) Value() (driver.Value, error) {
return int64(v.id), nil
}
func (v *VoteChoice) Equal(other *VoteChoice) bool {
return v.id == other.id
2022-05-15 18:10:49 +00:00
}
2022-05-21 11:51:17 +00:00
type VoteSums struct {
Ayes, Nayes, Abstains int
}
func (v *VoteSums) VoteCount() int {
return v.Ayes + v.Nayes + v.Abstains
}
func (v *VoteSums) TotalVotes() int {
return v.Ayes + v.Nayes
}
func (v *VoteSums) CalculateResult(quorum int, majority float32) (*VoteStatus, string) {
2022-05-21 11:51:17 +00:00
if v.VoteCount() < quorum {
return voteStatusDeclined, fmt.Sprintf("Needed quorum of %d has not been reached.", quorum)
}
if (float32(v.Ayes) / float32(v.TotalVotes())) < majority {
return voteStatusDeclined, fmt.Sprintf("Needed majority of %0.2f%% has not been reached.", majority)
}
return voteStatusApproved, "Quorum and majority have been reached"
}
type Motion struct {
2022-05-15 18:10:49 +00:00
ID int64 `db:"id"`
Proposed time.Time
Proponent int64 `db:"proponent"`
Title string
Content string
Status *VoteStatus
2022-05-15 18:10:49 +00:00
Due time.Time
Modified time.Time
Tag string
Type *VoteType `db:"votetype"`
2022-05-15 18:10:49 +00:00
}
type ClosedMotion struct {
Decision *Motion
2022-05-21 11:51:17 +00:00
VoteSums *VoteSums
Reasoning string
}
type NewMotion struct {
Decision *Motion
Proposer *User
}
2022-05-26 19:04:47 +00:00
type UpdatedMotion struct {
Decision *Motion
User *User
}
type MotionModel struct {
2022-05-15 18:10:49 +00:00
DB *sqlx.DB
InfoLog *log.Logger
}
// Create a new decision.
func (m *MotionModel) Create(
2022-05-21 12:27:46 +00:00
ctx context.Context,
proponent *User,
voteType *VoteType,
2022-05-15 18:10:49 +00:00
title, content string,
proposed, due time.Time,
) (int64, error) {
d := &Motion{
2022-05-15 18:10:49 +00:00
Proposed: proposed.UTC(),
Proponent: proponent.ID,
Title: title,
Content: content,
Due: due.UTC(),
Type: voteType,
2022-05-15 18:10:49 +00:00
}
2022-05-21 12:27:46 +00:00
result, err := m.DB.NamedExecContext(
ctx,
`INSERT INTO decisions
2022-05-15 18:10:49 +00:00
(proposed, proponent, title, content, votetype, status, due, modified, tag)
VALUES (:proposed, :proponent, :title, :content, :votetype, :status, :due, :proposed,
'm' || STRFTIME('%Y%m%d', :proposed) || '.' || (
2022-05-15 18:10:49 +00:00
SELECT COUNT(*)+1 AS num
FROM decisions
WHERE proposed BETWEEN DATE(:proposed) AND DATE(:proposed, '1 day')
2022-05-21 12:27:46 +00:00
))`,
d,
)
2022-05-15 18:10:49 +00:00
if err != nil {
return 0, fmt.Errorf("creating motion failed: %w", err)
}
id, err := result.LastInsertId()
if err != nil {
return 0, fmt.Errorf("could not get inserted decision id: %w", err)
}
return id, nil
}
func (m *MotionModel) CloseDecisions(ctx context.Context) ([]*ClosedMotion, error) {
2022-05-21 12:27:46 +00:00
tx, err := m.DB.BeginTxx(ctx, nil)
2022-05-21 11:51:17 +00:00
if err != nil {
return nil, fmt.Errorf("could not start transaction: %w", err)
}
defer func(tx *sqlx.Tx) {
_ = tx.Rollback()
}(tx)
rows, err := tx.NamedQuery(`
2022-05-15 18:10:49 +00:00
SELECT decisions.id, decisions.tag, decisions.proponent, decisions.proposed, decisions.title, decisions.content,
decisions.votetype, decisions.status, decisions.due, decisions.modified
FROM decisions
WHERE decisions.status=0 AND :now > due`, struct{ Now time.Time }{Now: time.Now().UTC()})
if err != nil {
2022-05-21 11:51:17 +00:00
return nil, fmt.Errorf("fetching closable decisions failed: %w", err)
2022-05-15 18:10:49 +00:00
}
defer func(rows *sqlx.Rows) {
_ = rows.Close()
}(rows)
decisions := make([]*Motion, 0)
2022-05-15 18:10:49 +00:00
for rows.Next() {
decision := &Motion{}
2022-05-15 18:10:49 +00:00
if err = rows.StructScan(decision); err != nil {
2022-05-21 11:51:17 +00:00
return nil, fmt.Errorf("scanning row failed: %w", err)
2022-05-15 18:10:49 +00:00
}
if rows.Err() != nil {
2022-05-21 11:51:17 +00:00
return nil, fmt.Errorf("row error: %w", err)
2022-05-15 18:10:49 +00:00
}
decisions = append(decisions, decision)
}
results := make([]*ClosedMotion, 0, len(decisions))
2022-05-21 11:51:17 +00:00
var decisionResult *ClosedMotion
2022-05-21 11:51:17 +00:00
2022-05-15 18:10:49 +00:00
for _, decision := range decisions {
m.InfoLog.Printf("found closable decision %s", decision.Tag)
2022-05-21 12:27:46 +00:00
if decisionResult, err = m.CloseDecision(ctx, tx, decision); err != nil {
2022-05-21 11:51:17 +00:00
return nil, fmt.Errorf("closing decision %s failed: %w", decision.Tag, err)
2022-05-15 18:10:49 +00:00
}
2022-05-21 11:51:17 +00:00
results = append(results, decisionResult)
}
if err = tx.Commit(); err != nil {
return nil, fmt.Errorf("could not commit transaction: %w", err)
2022-05-15 18:10:49 +00:00
}
2022-05-21 11:51:17 +00:00
return results, nil
2022-05-15 18:10:49 +00:00
}
func (m *MotionModel) CloseDecision(ctx context.Context, tx *sqlx.Tx, d *Motion) (*ClosedMotion, error) {
quorum, majority := d.Type.QuorumAndMajority()
2022-05-21 11:51:17 +00:00
var (
voteSums *VoteSums
err error
reasoning string
)
2022-05-21 12:27:46 +00:00
if voteSums, err = m.SumsForDecision(ctx, tx, d); err != nil {
2022-05-21 11:51:17 +00:00
return nil, fmt.Errorf("getting vote sums failed: %w", err)
}
d.Status, reasoning = voteSums.CalculateResult(quorum, majority)
2022-05-21 12:27:46 +00:00
result, err := m.DB.NamedExecContext(
ctx,
2022-05-21 11:51:17 +00:00
`UPDATE decisions SET status=:status, modified=CURRENT_TIMESTAMP WHERE id=:id`,
d,
)
if err != nil {
return nil, fmt.Errorf("could not execute update query: %w", err)
}
affectedRows, err := result.RowsAffected()
if err != nil {
return nil, fmt.Errorf("could not get affected rows count: %w", err)
}
if affectedRows != 1 {
return nil, fmt.Errorf("unexpected number of rows %d instead of 1", affectedRows)
}
m.InfoLog.Printf("decision %s closed with result %s: reasoning '%s'", d.Tag, d.Status, reasoning)
return &ClosedMotion{d, voteSums, reasoning}, nil
2022-05-15 18:10:49 +00:00
}
func (m *MotionModel) UnVotedDecisionsForVoter(_ context.Context, _ *User) ([]*Motion, error) {
2022-05-15 18:10:49 +00:00
panic("not implemented")
}
2022-05-21 11:51:17 +00:00
func (m *MotionModel) SumsForDecision(ctx context.Context, tx *sqlx.Tx, d *Motion) (*VoteSums, error) {
2022-05-21 12:27:46 +00:00
voteRows, err := tx.QueryxContext(
ctx,
2022-05-21 11:51:17 +00:00
`SELECT vote, COUNT(vote) FROM votes WHERE decision=$1 GROUP BY vote`,
d.ID,
)
if err != nil {
return nil, fmt.Errorf("fetching vote sums for motion %s failed: %w", d.Tag, err)
}
defer func(voteRows *sqlx.Rows) {
_ = voteRows.Close()
}(voteRows)
sums := &VoteSums{}
for voteRows.Next() {
var (
vote *VoteChoice
2022-05-21 11:51:17 +00:00
count int
)
if err = voteRows.Err(); err != nil {
return nil, fmt.Errorf("could not fetch vote sums for motion %s: %w", d.Tag, err)
}
if err = voteRows.Scan(&vote, &count); err != nil {
return nil, fmt.Errorf("could not parse row for vote sums of motion %s: %w", d.Tag, err)
}
switch vote {
case VoteAye:
2022-05-21 11:51:17 +00:00
sums.Ayes = count
case VoteNaye:
2022-05-21 11:51:17 +00:00
sums.Nayes = count
case VoteAbstain:
2022-05-21 11:51:17 +00:00
sums.Abstains = count
}
}
return sums, nil
}
func (m *MotionModel) NextPendingDecisionDue(ctx context.Context) (*time.Time, error) {
2022-05-21 12:27:46 +00:00
row := m.DB.QueryRowContext(
ctx,
`SELECT due FROM decisions WHERE status=0 ORDER BY due LIMIT 1`,
nil,
)
2022-05-21 11:51:17 +00:00
if row == nil {
return nil, errors.New("no row returned")
}
if err := row.Err(); err != nil {
return nil, fmt.Errorf("could not retrieve row for next pending decision: %w", err)
}
var due time.Time
if err := row.Scan(&due); err != nil {
if errors.Is(err, sql.ErrNoRows) {
m.InfoLog.Print("no pending decisions")
return nil, nil
}
return nil, fmt.Errorf("parsing result failed: %w", err)
}
return &due, nil
}
type MotionForDisplay struct {
ID int64 `db:"id"`
Tag string
Proponent int64
Proposer string
Proposed time.Time
Title string
Content string
Type *VoteType `db:"votetype"`
Status *VoteStatus
Due time.Time
Modified time.Time
Sums VoteSums
Votes []*VoteForDisplay
}
type VoteForDisplay struct {
Name string
Vote *VoteChoice
}
type MotionListOptions struct {
Limit int
UnvotedOnly bool
Before, After *time.Time
VoterID int64
}
func (m *MotionModel) TimestampRange(ctx context.Context, options *MotionListOptions) (*time.Time, *time.Time, error) {
var row *sqlx.Row
if options.UnvotedOnly {
row = m.DB.QueryRowxContext(
ctx,
`SELECT MIN(proposed), MAX(proposed)
FROM decisions
WHERE due >= ?
AND NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?)`,
time.Now().UTC(),
options.VoterID,
)
} else {
row = m.DB.QueryRowxContext(
ctx,
`SELECT MIN(proposed), MAX(proposed) FROM decisions`,
)
}
if err := row.Err(); err != nil {
return nil, nil, fmt.Errorf("could not query for motion timestamps: %w", err)
}
var (
first, last sql.NullString
firstTs, lastTs *time.Time
err error
)
if err := row.Scan(&first, &last); err != nil {
return nil, nil, fmt.Errorf("could not scan timestamps: %w", err)
}
if !first.Valid || !last.Valid {
return nil, nil, nil
}
if firstTs, err = parseSqlite3TimeStamp(first.String); err != nil {
return nil, nil, err
}
if lastTs, err = parseSqlite3TimeStamp(last.String); err != nil {
return nil, nil, err
}
return firstTs, lastTs, nil
}
func (m *MotionModel) GetMotions(ctx context.Context, options *MotionListOptions) ([]*MotionForDisplay, error) {
var (
rows *sqlx.Rows
err error
)
switch {
case options.Before != nil:
rows, err = m.GetMotionRowsBefore(ctx, options)
case options.After != nil:
rows, err = m.GetMotionRowsAfter(ctx, options)
default:
rows, err = m.GetFirstMotionRows(ctx, options)
}
if err != nil {
return nil, err
}
defer func(rows *sqlx.Rows) {
_ = rows.Close()
}(rows)
motions := make([]*MotionForDisplay, 0, options.Limit)
for rows.Next() {
var decision MotionForDisplay
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("could not fetch row: %w", err)
}
if err = rows.StructScan(&decision); err != nil {
return nil, fmt.Errorf("could not scan result: %w", err)
}
motions = append(motions, &decision)
}
if len(motions) > 0 {
err = m.FillVoteSums(ctx, motions)
if err != nil {
return nil, err
}
}
return motions, nil
}
func (m *MotionModel) FillVoteSums(ctx context.Context, decisions []*MotionForDisplay) error {
decisionIds := make([]int64, len(decisions))
decisionMap := make(map[int64]*MotionForDisplay, len(decisions))
for idx, decision := range decisions {
decisionIds[idx] = decision.ID
decisionMap[decision.ID] = decision
}
query, args, err := sqlx.In(
`SELECT v.decision, v.vote, COUNT(*) FROM votes v WHERE v.decision IN (?) GROUP BY v.decision, v.vote`,
decisionIds,
)
if err != nil {
return fmt.Errorf("could not create IN query: %w", err)
}
rows, err := m.DB.QueryContext(ctx, query, args...)
if err != nil {
return fmt.Errorf("could not execute query: %w", err)
}
defer func(rows *sql.Rows) {
_ = rows.Close()
}(rows)
for rows.Next() {
if err = rows.Err(); err != nil {
return fmt.Errorf("could not fetch row: %w", err)
}
var (
decisionID int64
vote *VoteChoice
count int
)
err = rows.Scan(&decisionID, &vote, &count)
if err != nil {
return fmt.Errorf("could not scan row: %w", err)
}
switch {
case vote.Equal(VoteAye):
decisionMap[decisionID].Sums.Ayes = count
case vote.Equal(VoteNaye):
decisionMap[decisionID].Sums.Nayes = count
case vote.Equal(VoteAbstain):
decisionMap[decisionID].Sums.Abstains = count
}
}
return nil
}
func (m *MotionModel) GetMotionRowsBefore(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
rows, err := m.DB.QueryxContext(
ctx,
`SELECT decisions.id,
decisions.tag,
decisions.proponent,
voters.name AS proposer,
decisions.proposed,
decisions.title,
decisions.content,
decisions.votetype,
decisions.status,
decisions.due,
decisions.modified
FROM decisions
JOIN voters ON decisions.proponent = voters.id
WHERE decisions.proposed < $1
ORDER BY proposed DESC
LIMIT $2`,
options.Before,
options.Limit,
)
if err != nil {
return nil, fmt.Errorf("could not query motions before %s: %w", options.Before, err)
}
return rows, nil
}
func (m *MotionModel) GetMotionRowsAfter(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
rows, err := m.DB.QueryxContext(
ctx,
`WITH display_decision AS (SELECT decisions.id,
decisions.tag,
decisions.proponent,
voters.name AS proposer,
decisions.proposed,
decisions.title,
decisions.content,
decisions.votetype,
decisions.status,
decisions.due,
decisions.modified
FROM decisions
JOIN voters ON decisions.proponent = voters.id
WHERE decisions.proposed > $1
ORDER BY proposed
LIMIT $2)
SELECT *
FROM display_decision
ORDER BY proposed DESC`,
options.After,
options.Limit,
)
if err != nil {
return nil, fmt.Errorf("could not query motions after %s: %w", options.After, err)
}
return rows, nil
}
func (m *MotionModel) GetFirstMotionRows(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
var (
rows *sqlx.Rows
err error
)
if options.UnvotedOnly {
rows, err = m.DB.QueryxContext(
ctx,
`SELECT decisions.id,
decisions.tag,
decisions.proponent,
voters.name AS proposer,
decisions.proposed,
decisions.title,
decisions.content,
decisions.votetype,
decisions.status,
decisions.due,
decisions.modified
FROM decisions
JOIN voters ON decisions.proponent = voters.id
WHERE NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?)
AND due >= ?
ORDER BY decisions.proposed DESC
LIMIT ?`,
options.VoterID,
time.Now().UTC(),
options.Limit,
)
} else {
rows, err = m.DB.QueryxContext(
ctx,
`SELECT decisions.id,
decisions.tag,
decisions.proponent,
voters.name AS proposer,
decisions.proposed,
decisions.title,
decisions.content,
decisions.votetype,
decisions.status,
decisions.due,
decisions.modified
FROM decisions
JOIN voters ON decisions.proponent = voters.id
ORDER BY proposed DESC
LIMIT ?`,
options.Limit,
)
}
if err != nil {
return nil, fmt.Errorf("could not query motions: %w", err)
}
return rows, nil
}
func (m *MotionModel) GetMotionByTag(ctx context.Context, tag string, withVotes bool) (*MotionForDisplay, error) {
row := m.DB.QueryRowxContext(
ctx,
`SELECT decisions.id,
decisions.tag,
decisions.proponent,
voters.name AS proposer,
decisions.proposed,
decisions.title,
decisions.content,
decisions.votetype,
decisions.status,
decisions.due,
decisions.modified
FROM decisions
JOIN voters ON decisions.proponent = voters.id
WHERE decisions.tag = ?`,
tag,
)
if err := row.Err(); err != nil {
return nil, fmt.Errorf("could not query motion: %w", err)
}
var result MotionForDisplay
if err := row.StructScan(&result); err != nil {
return nil, fmt.Errorf("could not fill motion from query result: %w", err)
}
if err := m.FillVoteSums(ctx, []*MotionForDisplay{&result}); err != nil {
return nil, fmt.Errorf("could not get vote sums: %w", err)
}
2022-05-26 19:04:47 +00:00
if result.ID != 0 && withVotes {
if err := m.FillVotes(ctx, &result); err != nil {
return nil, fmt.Errorf("could not get votes for %s: %w", result.Tag, err)
}
}
return &result, nil
}
func (m *MotionModel) FillVotes(ctx context.Context, md *MotionForDisplay) error {
rows, err := m.DB.QueryxContext(ctx,
`SELECT voters.name, votes.vote
FROM voters
JOIN votes ON votes.voter = voters.id
WHERE votes.decision = ?
ORDER BY voters.name`,
md.ID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil
}
return fmt.Errorf("could not fetch rows: %w", err)
}
defer func(rows *sqlx.Rows) {
_ = rows.Close()
}(rows)
for rows.Next() {
if err := rows.Err(); err != nil {
return fmt.Errorf("could not get row: %w", err)
}
var voteDisplay VoteForDisplay
if err := rows.StructScan(&voteDisplay); err != nil {
return fmt.Errorf("could not scan row: %w", err)
}
md.Votes = append(md.Votes, &voteDisplay)
}
return nil
}
func (m *MotionModel) GetByID(ctx context.Context, id int64) (*Motion, error) {
row := m.DB.QueryRowxContext(ctx, `SELECT * FROM decisions WHERE id=?`, id)
if err := row.Err(); err != nil {
return nil, fmt.Errorf("could not fetch tag for id %d: %w", id, err)
}
var motion Motion
if err := row.StructScan(&motion); err != nil {
return nil, fmt.Errorf("could not get tag from row: %w", err)
}
return &motion, nil
}
2022-05-26 19:04:47 +00:00
func (m *MotionModel) Update(ctx context.Context, id int64, voteType *VoteType, title string, content string, due time.Time) error {
_, err := m.DB.ExecContext(
ctx,
`UPDATE decisions SET title=?, content=?, votetype=?, due=?, modified=? WHERE id=?`,
title, content, voteType, due.UTC(), time.Now().UTC(), id,
2022-05-26 19:04:47 +00:00
)
if err != nil {
return fmt.Errorf("could not update decision: %w", err)
}
return nil
}