token-validator: start project
This commit is contained in:
parent
8268fed28c
commit
0fbe142626
7 changed files with 413 additions and 0 deletions
99
token-validator/students.go
Normal file
99
token-validator/students.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/students/", apiHandler(
|
||||
func(httprouter.Params,[]byte) (interface{}, error) {
|
||||
return getStudents() }))
|
||||
router.POST("/api/students/", apiHandler(createStudent))
|
||||
}
|
||||
|
||||
|
||||
type Student struct {
|
||||
Id int64 `json:"id"`
|
||||
Login string `json:"login"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
func getStudents() (students []Student, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_student, login, time FROM students"); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var s Student
|
||||
if err = rows.Scan(&s.Id, &s.Login, &s.Time); err != nil {
|
||||
return
|
||||
}
|
||||
students = append(students, s)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func getStudent(id int64) (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 NewStudent(login string) (Student, error) {
|
||||
if res, err := DBExec("INSERT INTO students (login, time) VALUES (?, ?)", login, time.Now()); err != nil {
|
||||
return Student{}, err
|
||||
} else if sid, err := res.LastInsertId(); err != nil {
|
||||
return Student{}, err
|
||||
} else {
|
||||
return Student{sid, login, time.Now()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s Student) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE students SET login = ?, time = ? WHERE id_student = ?", s.Login, s.Time, s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (s Student) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM students WHERE id_student = ?", s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func ClearStudents() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM students"); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func createStudent(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var std Student
|
||||
if err := json.Unmarshal(body, &std); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewStudent(strings.TrimSpace(std.Login))
|
||||
}
|
||||
Reference in a new issue