-
Notifications
You must be signed in to change notification settings - Fork 0
/
applicants.go
64 lines (57 loc) · 1.51 KB
/
applicants.go
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
package applicants
import (
"database/sql"
"errors"
"net/http"
sq "github.com/bokwoon95/go-structured-query/postgres"
"github.com/bokwoon95/nusskylabx/helpers/timeutil"
"github.com/bokwoon95/nusskylabx/tables"
"github.com/bokwoon95/nusskylabx/app/db"
"github.com/bokwoon95/nusskylabx/app/skylab"
"github.com/bokwoon95/nusskylabx/helpers/headers"
)
type Applicants struct {
skylb skylab.Skylab
d db.DB
}
func New(skylb skylab.Skylab) Applicants {
return Applicants{
skylb: skylb,
d: db.New(skylb),
}
}
func (apt Applicants) CheckIfOpen(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apt.skylb.Log.TraceRequest(r)
headers.DoNotCache(w)
var startAt, endAt sql.NullTime
p := tables.PERIODS()
err := sq.
From(p).
Where(
p.COHORT.EqString(apt.skylb.CurrentCohort()),
p.STAGE.EqString(skylab.StageApplication),
p.MILESTONE.EqString(skylab.MilestoneNull),
).
SelectRowx(func(row *sq.Row) {
startAt = row.NullTime(p.START_AT)
endAt = row.NullTime(p.END_AT)
}).
Fetch(apt.skylb.DB)
if err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
http.Redirect(w, r, "/applicant/application/closed", http.StatusMovedPermanently)
default:
apt.skylb.InternalServerError(w, r, err)
}
return
}
status := timeutil.ResolveTimestatus(startAt, endAt)
if !status.IsOpen {
http.Redirect(w, r, "/applicant/application/closed", http.StatusMovedPermanently)
return
}
next.ServeHTTP(w, r)
})
}