summaryrefslogtreecommitdiff
path: root/forms.go
blob: 8d573bcd1a3c18f025ca08e8d0a3b9f23384ee83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Copyright 2017-2021 Jan Dittberner

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this program 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 main

import (
	"fmt"
	"strconv"
	"strings"
	"time"
)

const (
	minimumContentLen = 3
	minimumTitleLen   = 3
)

const (
	hoursInADay  = 24
	dueThreeDays = 3
	dueOneWeek   = 7
	dueTwoWeeks  = 14
	dueFourWeeks = 28
)

var validDueDurations = map[string]time.Duration{
	"+3 days":  time.Hour * hoursInADay * dueThreeDays,
	"+7 days":  time.Hour * hoursInADay * dueOneWeek,
	"+14 days": time.Hour * hoursInADay * dueTwoWeeks,
	"+28 days": time.Hour * hoursInADay * dueFourWeeks,
}

type NewDecisionForm struct {
	Title    string
	Content  string
	VoteType string
	Due      string
	Errors   map[string]string
}

func (f *NewDecisionForm) Validate() (bool, *Decision) {
	f.Errors = make(map[string]string)

	data := &Decision{}

	data.Title = strings.TrimSpace(f.Title)
	if len(data.Title) < minimumTitleLen {
		f.Errors["Title"] = fmt.Sprintf("Please enter at least %d characters for Title.", minimumTitleLen)
	}

	data.Content = strings.TrimSpace(f.Content)
	if len(strings.Fields(data.Content)) < minimumContentLen {
		f.Errors["Content"] = fmt.Sprintf("Please enter at least %d words as Text.", minimumContentLen)
	}

	if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
		f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type.", err)
	} else {
		data.VoteType = VoteType(uint8(voteType))
	}

	if dueDuration, ok := validDueDurations[f.Due]; !ok {
		f.Errors["Due"] = "Please choose a valid due date."
	} else {
		year, month, day := time.Now().UTC().Add(dueDuration).Date()
		data.Due = time.Date(year, month, day, 23, 59, 59, 0, time.UTC)
	}

	return len(f.Errors) == 0, data
}

type EditDecisionForm struct {
	Title    string
	Content  string
	VoteType string
	Due      string
	Decision *Decision
	Errors   map[string]string
}

func (f *EditDecisionForm) Validate() (bool, *Decision) {
	f.Errors = make(map[string]string)

	data := f.Decision

	data.Title = strings.TrimSpace(f.Title)
	if len(data.Title) < minimumTitleLen {
		f.Errors["Title"] = fmt.Sprintf("Please enter at least %d characters for Title.", minimumTitleLen)
	}

	data.Content = strings.TrimSpace(f.Content)
	if len(strings.Fields(data.Content)) < minimumContentLen {
		f.Errors["Content"] = fmt.Sprintf("Please enter at least %d words as Text.", minimumContentLen)
	}

	if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
		f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type.", err)
	} else {
		data.VoteType = VoteType(uint8(voteType))
	}

	if dueDuration, ok := validDueDurations[f.Due]; !ok {
		f.Errors["Due"] = "Please choose a valid due date."
	} else {
		year, month, day := time.Now().UTC().Add(dueDuration).Date()
		data.Due = time.Date(year, month, day, 23, 59, 59, 0, time.UTC)
	}

	return len(f.Errors) == 0, data
}

type ProxyVoteForm struct {
	Voter         string
	Vote          string
	Justification string
	Errors        map[string]string
}

func (f *ProxyVoteForm) Validate() (bool, *Voter, *Vote, string) {
	f.Errors = make(map[string]string)

	const minimumJustificationLen = 3

	var (
		voter         *Voter
		err           error
		voterID, vote int64
	)

	data := &Vote{}

	if voterID, err = strconv.ParseInt(f.Voter, 10, 64); err != nil {
		f.Errors["Voter"] = fmt.Sprintf("Please choose a valid voter: %v.", err)
	} else if voter, err = GetVoterByID(voterID); err != nil {
		f.Errors["Voter"] = fmt.Sprintf("Please choose a valid voter: %v.", err)
	} else {
		data.VoterID = voter.ID
	}

	if vote, err = strconv.ParseInt(f.Vote, 10, 8); err != nil {
		f.Errors["Vote"] = fmt.Sprintf("Please choose a valid vote: %v.", err)
	} else if voteChoice, ok := VoteChoices[vote]; !ok {
		f.Errors["Vote"] = "Please choose a valid vote."
	} else {
		data.Vote = voteChoice
	}

	justification := strings.TrimSpace(f.Justification)
	if len(justification) < minimumJustificationLen {
		f.Errors["Justification"] = "Please enter at least 3 characters for justification."
	}

	return len(f.Errors) == 0, voter, data, justification
}