/
evaluations.go
117 lines (108 loc) · 3.65 KB
/
evaluations.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package actions
import (
"github.com/IEEESBITBA/Curso-de-Python-Sistemas/models"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/pop/v5"
"github.com/pkg/errors"
)
// EvaluationIndex default implementation.
func EvaluationIndex(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
evals := &models.Evaluations{}
if err := tx.Order("created_at ASC").All(evals); err != nil {
return c.Error(404, err)
}
// sort.Sort(evals)
c.Set("evaluations", evals)
c.Logger().Debugf("Finishing EvaluationIndex with c.Data():%v", c.Data())
return c.Render(200, r.HTML("curso/eval-index.plush.html"))
}
// CursoEvaluationCreateGet renders evaluation creation page
func CursoEvaluationCreateGet(c buffalo.Context) error {
c.Set("evaluation", models.Evaluation{})
return c.Render(200, r.HTML("curso/eval-create.plush.html"))
}
// CursoEvaluationCreatePost handles creation of evaluation
func CursoEvaluationCreatePost(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
eval := &models.Evaluation{}
if err := c.Bind(eval); err != nil {
return errors.WithStack(err)
}
// Validate the data from the html form
verrs, err := tx.ValidateAndCreate(eval)
if err != nil {
return errors.WithStack(err)
}
c.Set("evaluation", eval)
if verrs.HasAny() {
c.Flash().Add("danger", T.Translate(c, "curso-python-evaluation-add-fail"))
return c.Render(422, r.HTML("topics/create.plush.html"))
}
u := c.Value("current_user").(*models.User)
c.Logger().Infof("evaluation create %s, by %s", eval.Title, u.Email)
c.Flash().Add("success", T.Translate(c, "curso-python-evaluation-add-success"))
return c.Render(200, r.HTML("curso/eval-get.plush.html"))
}
// CursoEvaluationEditGet handles the rendering of the evaluation edit page
func CursoEvaluationEditGet(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
eval := &models.Evaluation{}
eid := c.Param("evalid")
q := tx.Where("id = ?", eid)
if err := q.First(eval); err != nil {
return c.Error(404, err)
}
c.Set("evaluation", eval)
return c.Render(200, r.HTML("curso/eval-create.plush.html"))
}
// CursoEvaluationEditPost handles the editing of an already existing evaluation
func CursoEvaluationEditPost(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
eid := c.Param("evalid")
eval := &models.Evaluation{}
q := tx.Where("id = ?", eid)
if err := q.First(eval); err != nil {
return errors.WithStack(err)
}
uid := eval.ID
if err := c.Bind(eval); err != nil {
return errors.WithStack(err)
}
eval.ID = uid
// Validate the data from the html form
if err := tx.Update(eval); err != nil {
return errors.WithStack(err)
}
c.Flash().Add("success", T.Translate(c, "edit-success"))
return c.Redirect(302, "evaluationGetPath()", render.Data{"evalid": eval.ID})
}
// CursoEvaluationDelete handles deletion event of evaluation
func CursoEvaluationDelete(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
eid := c.Param("evalid")
eval := &models.Evaluation{}
q := tx.Where("id = ?", eid)
if err := q.First(eval); err != nil {
return errors.WithStack(err)
}
eval.Deleted = true
if err := tx.Update(eval); err != nil {
return errors.WithStack(err)
}
c.Flash().Add("success", T.Translate(c, "delete-success"))
return c.Redirect(302, "evaluationPath()")
}
// CursoEvaluationGet handles rendering of an evaluation
func CursoEvaluationGet(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
eval := &models.Evaluation{}
eid := c.Param("evalid")
q := tx.Where("id = ?", eid)
if err := q.First(eval); err != nil {
return c.Error(404, err)
}
c.Set("evaluation", eval)
return c.Render(200, r.HTML("curso/eval-get.plush.html"))
}