From f3249cc9ed11ab4ed1d789be1d589a69f7092f66 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Thu, 22 Feb 2018 00:45:51 +0100 Subject: [PATCH] token-validator: existing student is not a case of failure --- token-validator/students.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/token-validator/students.go b/token-validator/students.go index a0dda79..e7ca2bf 100644 --- a/token-validator/students.go +++ b/token-validator/students.go @@ -64,6 +64,12 @@ func getStudentByLogin(login string) (s Student, err error) { return } +func studentExists(login string) bool { + var z int + err := DBQueryRow("SELECT 1 FROM students WHERE login=?", login).Scan(&z) + return err != nil && z == 1 +} + func NewStudent(login string) (Student, error) { if res, err := DBExec("INSERT INTO students (login, time) VALUES (?, ?)", login, time.Now()); err != nil { return Student{}, err @@ -114,7 +120,11 @@ func createStudent(_ httprouter.Params, body []byte) (interface{}, error) { return nil, err } - return NewStudent(strings.TrimSpace(std.Login)) + if exist, err := getStudentByLogin(strings.TrimSpace(std.Login)); err != nil { + return NewStudent(strings.TrimSpace(std.Login)) + } else { + return exist, nil + } } func updateStudent(current Student, body []byte) (interface{}, error) {