/
update_problem_extra.go
48 lines (43 loc) · 1007 Bytes
/
update_problem_extra.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
package api
import (
"context"
"github.com/imos/icfpc2019/go/util/db"
"github.com/imos/icfpc2019/go/util/pb"
"github.com/pkg/errors"
)
func updateProblemExtraHandler(
ctx context.Context, apiReq *pb.Api_Request, apiResp *pb.Api_Response,
) error {
req := apiReq.GetUpdateProblemExtra()
resp := &pb.Api_Response_UpdateProblemExtra{}
if req == nil {
return nil
}
if req.GetProblemDataImage() == nil || len(req.GetProblemDataImage()) == 0 {
return errors.New("problem_data_image is missing")
}
tx, err := db.DB().BeginTxx(ctx, nil)
if err != nil {
return errors.WithStack(err)
}
if err := func() error {
image := req.GetProblemDataImage()
if image == nil {
image = []byte{}
}
_, err := tx.ExecContext(ctx,
`UPDATE problem_data
SET problem_data_image = ?
WHERE problem_id = ?`,
image, req.GetProblemId())
if err != nil {
return err
}
return nil
}(); err != nil {
tx.Rollback()
return err
}
apiResp.UpdateProblemExtra = resp
return tx.Commit()
}