-
Notifications
You must be signed in to change notification settings - Fork 0
/
bills.go
100 lines (89 loc) · 2.4 KB
/
bills.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
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
package controllers
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/jeff-blank/wg/app"
"github.com/jeff-blank/wg/app/routes"
"github.com/jeff-blank/wg/app/util"
"github.com/revel/revel"
)
type Bills struct {
*revel.Controller
}
const SQL_BILL_BY_ID = `select id, serial, series, denomination, rptkey, residence from bills where id=$1`
func (c Bills) Edit() revel.Result {
id := c.Params.Route.Get("id")
bill, err := getBillById(id)
if err != nil {
revel.AppLog.Error("bills.Edit(): get bill with id '%s': %#v", id, err)
return c.RenderError(err)
}
residences, err := util.GetResidences()
if err != nil {
return c.RenderError(err)
}
currentResidence, err := util.GetCurrentResidence()
if err != nil {
return c.RenderError(err)
}
hitList, err := util.GetHits("and h.bill_id=" + id + " order by entdate desc, b.id desc")
return c.Render(bill, residences, currentResidence, hitList)
}
func (c Bills) Update() revel.Result {
var updateFlash app.HitInfo
denom, _ := strconv.Atoi(c.Params.Get("denom"))
serial, series, err := util.GetSerialSeries(c.Params.Get("serial"), c.Params.Get("series"), denom)
if err != nil {
return c.RenderError(err)
}
revel.AppLog.Debugf("serial='%s'; series='%s'", serial, series)
billId := c.Params.Route.Get("id")
res, err := app.DB.Exec(
`update bills set rptkey=$1, serial=$2, denomination=$3, series=$4, residence=$5 where id=$6`,
c.Params.Get("key"),
serial,
denom,
series,
c.Params.Get("residence"),
billId,
)
if err != nil {
revel.AppLog.Errorf("update bill with id %s: %#v", billId, err)
return c.RenderError(err)
}
n, err := res.RowsAffected()
if err != nil {
return c.RenderError(err)
}
if n != 1 {
err := errors.New(fmt.Sprintf("update affected %d rows (expected 1)", n))
revel.AppLog.Error(err.Error())
return c.RenderError(err)
}
updateFlash.GenericMessage = "Bill information successfully updated"
flashJson, err := json.Marshal(updateFlash)
if err == nil {
c.Flash.Out["info"] = string(flashJson)
}
return c.Redirect(routes.Hits.Index() + "?year=current")
}
func getBillById(id string) (app.Bill, error) {
var bill app.Bill
err := app.DB.QueryRow(SQL_BILL_BY_ID, id).Scan(
&bill.Id,
&bill.Serial,
&bill.Series,
&bill.Denomination,
&bill.Rptkey,
&bill.Residence,
)
if err != nil {
if err.Error() == app.SQL_ERR_NO_ROWS {
return app.Bill{}, nil
}
return app.Bill{}, err
}
return bill, nil
}