token-validator: handle remote-validator authentication + new student features
This commit is contained in:
parent
0fbe142626
commit
fa45457bb7
2 changed files with 61 additions and 2 deletions
|
|
@ -12,7 +12,14 @@ func init() {
|
|||
router.GET("/api/students/", apiHandler(
|
||||
func(httprouter.Params,[]byte) (interface{}, error) {
|
||||
return getStudents() }))
|
||||
router.POST("/api/students/", apiHandler(createStudent))
|
||||
router.POST("/api/students/", remoteValidatorHandler(apiHandler(createStudent)))
|
||||
router.GET("/api/students/:sid/", apiHandler(studentHandler(
|
||||
func(std Student, _ []byte) (interface{}, error) {
|
||||
return std, nil })))
|
||||
router.PUT("/api/students/:sid/", remoteValidatorHandler(apiHandler(studentHandler(updateStudent))))
|
||||
router.DELETE("/api/students/:sid/", remoteValidatorHandler(apiHandler(studentHandler(
|
||||
func(std Student, _ []byte) (interface{}, error) {
|
||||
return std.Delete() }))))
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -43,11 +50,16 @@ func getStudents() (students []Student, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func getStudent(id int64) (s Student, err error) {
|
||||
func getStudent(id int) (s Student, err error) {
|
||||
err = DBQueryRow("SELECT id_student, login, time FROM students WHERE id_student=?", id).Scan(&s.Id, &s.Login, &s.Time)
|
||||
return
|
||||
}
|
||||
|
||||
func getStudentByLogin(login string) (s Student, err error) {
|
||||
err = DBQueryRow("SELECT id_student, login, time FROM students WHERE login=?", login).Scan(&s.Id, &s.Login, &s.Time)
|
||||
return
|
||||
}
|
||||
|
||||
func NewStudent(login string) (Student, error) {
|
||||
if res, err := DBExec("INSERT INTO students (login, time) VALUES (?, ?)", login, time.Now()); err != nil {
|
||||
return Student{}, err
|
||||
|
|
@ -97,3 +109,14 @@ func createStudent(_ httprouter.Params, body []byte) (interface{}, error) {
|
|||
|
||||
return NewStudent(strings.TrimSpace(std.Login))
|
||||
}
|
||||
|
||||
func updateStudent(current Student, body []byte) (interface{}, error) {
|
||||
var new Student
|
||||
if err := json.Unmarshal(body, &new); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
current.Login = new.Login
|
||||
current.Time = new.Time
|
||||
return current.Update()
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue