Use pointer instead of struct
This commit is contained in:
parent
853477e54a
commit
6d8f38d749
18 changed files with 187 additions and 142 deletions
|
@ -17,14 +17,14 @@ type Student struct {
|
|||
DelegatedDomain *string `json:"delegated_domain,omitempty"`
|
||||
}
|
||||
|
||||
func GetStudents() (students []Student, err error) {
|
||||
func GetStudents() (students []*Student, err error) {
|
||||
if rows, errr := DBQuery("SELECT S.id_student, S.login, MAX(L.time), L.ip, L.mac, S.associatedDomain, S.delegatedDomain FROM students S INNER JOIN (SELECT a.id_student, a.time, a.ip, a.mac FROM student_login a INNER JOIN (SELECT id_student, MAX(time) AS time FROM student_login GROUP BY id_student) b ON a.id_student = b.id_student AND a.time = b.time) L ON S.id_student = L.id_student GROUP BY id_student"); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var s Student
|
||||
s := &Student{}
|
||||
if err = rows.Scan(&s.Id, &s.Login, &s.Time, &s.IP, &s.MAC, &s.AssociatedDomain, &s.DelegatedDomain); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -38,12 +38,14 @@ func GetStudents() (students []Student, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func GetStudent(id int) (s Student, err error) {
|
||||
func GetStudent(id int) (s *Student, err error) {
|
||||
s = new(Student)
|
||||
err = DBQueryRow("SELECT S.id_student, S.login, MAX(L.time), L.ip, L.mac, S.associatedDomain, S.delegatedDomain FROM students S INNER JOIN (SELECT a.id_student, a.time, a.ip, a.mac FROM student_login a INNER JOIN (SELECT id_student, MAX(time) AS time FROM student_login GROUP BY id_student) b ON a.id_student = b.id_student AND a.time = b.time) L ON S.id_student = L.id_student WHERE S.id_student=?", id).Scan(&s.Id, &s.Login, &s.Time, &s.IP, &s.MAC, &s.AssociatedDomain, &s.DelegatedDomain)
|
||||
return
|
||||
}
|
||||
|
||||
func GetStudentByLogin(login string) (s Student, err error) {
|
||||
func GetStudentByLogin(login string) (s *Student, err error) {
|
||||
s = new(Student)
|
||||
err = DBQueryRow("SELECT S.id_student, S.login, MAX(L.time), L.ip, L.mac, S.associatedDomain, S.delegatedDomain FROM students S INNER JOIN (SELECT a.id_student, a.time, a.ip, a.mac FROM student_login a INNER JOIN (SELECT id_student, MAX(time) AS time FROM student_login GROUP BY id_student) b ON a.id_student = b.id_student AND a.time = b.time) L ON S.id_student = L.id_student WHERE login=?", login).Scan(&s.Id, &s.Login, &s.Time, &s.IP, &s.MAC, &s.AssociatedDomain, &s.DelegatedDomain)
|
||||
return
|
||||
}
|
||||
|
@ -54,22 +56,22 @@ func StudentExists(login string) bool {
|
|||
return err == nil && z == 1
|
||||
}
|
||||
|
||||
func NewStudent(login string) (Student, error) {
|
||||
func NewStudent(login string) (*Student, error) {
|
||||
t := time.Now()
|
||||
if res, err := DBExec("INSERT INTO students (login, time) VALUES (?, ?)", login, t); err != nil {
|
||||
return Student{}, err
|
||||
return nil, err
|
||||
} else if sid, err := res.LastInsertId(); err != nil {
|
||||
return Student{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return Student{sid, login, &t, nil, nil, nil, nil}, nil
|
||||
return &Student{sid, login, &t, nil, nil, nil, nil}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s Student) GetPKey() []byte {
|
||||
func (s *Student) GetPKey() []byte {
|
||||
return hmac.New(sha512.New512_224, []byte(SharedSecret)).Sum([]byte(s.Login))
|
||||
}
|
||||
|
||||
func (s Student) Update() (int64, error) {
|
||||
func (s *Student) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE students SET login = ?, time = ?, associatedDomain = ?, delegatedDomain = ? WHERE id_student = ?", s.Login, s.Time, s.AssociatedDomain, s.DelegatedDomain, s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
@ -79,7 +81,7 @@ func (s Student) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) Delete() (int64, error) {
|
||||
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 {
|
||||
|
@ -103,18 +105,20 @@ type UnlockedChallenge struct {
|
|||
Id int64 `json:"id,omitempty"`
|
||||
IdStudent int64 `json:"id_student"`
|
||||
Challenge int `json:"challenge,omitempty"`
|
||||
Time time.Time `json:"time"`
|
||||
Time *time.Time `json:"time,omitempty"`
|
||||
Value interface{} `json:"value,omitempty"`
|
||||
LastCheck *time.Time `json:"last_check,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s Student) GetStates() (ucs []UnlockedChallenge, err error) {
|
||||
func (s *Student) GetStates() (ucs []*UnlockedChallenge, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_st, challenge, time FROM student_challenges WHERE id_student = ?", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var u UnlockedChallenge
|
||||
u := &UnlockedChallenge{}
|
||||
u.IdStudent = s.Id
|
||||
if err = rows.Scan(&u.Id, &u.Challenge, &u.Time); err != nil {
|
||||
return
|
||||
|
@ -129,14 +133,36 @@ func (s Student) GetStates() (ucs []UnlockedChallenge, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) GetStatesByChallenge() (ucs []UnlockedChallenge, err error) {
|
||||
func (s *Student) GetChallengeErrors() (ucs []*ErroredChallenge, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_st, challenge, time, error FROM student_challenge_errors WHERE id_student = ?", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
u := &ErroredChallenge{}
|
||||
u.IdStudent = s.Id
|
||||
if err = rows.Scan(&u.Id, &u.Challenge, &u.Time, &u.Error); err != nil {
|
||||
return
|
||||
}
|
||||
ucs = append(ucs, u)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Student) GetStatesByChallenge() (ucs []*UnlockedChallenge, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_st, challenge, MIN(time), value FROM student_challenges WHERE id_student = ? GROUP BY challenge, id_student", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var u UnlockedChallenge
|
||||
u := &UnlockedChallenge{}
|
||||
u.IdStudent = s.Id
|
||||
if err = rows.Scan(&u.Id, &u.Challenge, &u.Time, &u.Value); err != nil {
|
||||
return
|
||||
|
@ -151,7 +177,7 @@ func (s Student) GetStatesByChallenge() (ucs []UnlockedChallenge, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) UnlockChallenge(challenge int, value string) (uc UnlockedChallenge, err error) {
|
||||
func (s *Student) UnlockChallenge(challenge int, value string) (uc *UnlockedChallenge, err error) {
|
||||
if uc, err = s.UnlockNewChallenge(challenge, value); err != nil {
|
||||
if uc, err = s.UpdateUnlockedChallenge(challenge, value); err != nil {
|
||||
return
|
||||
|
@ -163,21 +189,23 @@ func (s Student) UnlockChallenge(challenge int, value string) (uc UnlockedChalle
|
|||
return
|
||||
}
|
||||
|
||||
func (s Student) UnlockNewChallenge(challenge int, value string) (UnlockedChallenge, error) {
|
||||
func (s *Student) UnlockNewChallenge(challenge int, value string) (*UnlockedChallenge, error) {
|
||||
if res, err := DBExec("INSERT INTO student_challenges (id_student, challenge, time, value) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), value); err != nil {
|
||||
return UnlockedChallenge{}, err
|
||||
return nil, err
|
||||
} else if utid, err := res.LastInsertId(); err != nil {
|
||||
return UnlockedChallenge{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return UnlockedChallenge{utid, s.Id, challenge, time.Now(), value}, err
|
||||
now := time.Now()
|
||||
return &UnlockedChallenge{utid, s.Id, challenge, &now, value, nil, ""}, err
|
||||
}
|
||||
}
|
||||
|
||||
func (s Student) UpdateUnlockedChallenge(challenge int, value string) (UnlockedChallenge, error) {
|
||||
func (s *Student) UpdateUnlockedChallenge(challenge int, value string) (*UnlockedChallenge, error) {
|
||||
if _, err := DBExec("UPDATE student_challenges SET time = ?, value = ? WHERE id_student = ? AND challenge = ?", time.Now(), value, s.Id, challenge); err != nil {
|
||||
return UnlockedChallenge{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return UnlockedChallenge{0, s.Id, challenge, time.Now(), value}, err
|
||||
now := time.Now()
|
||||
return &UnlockedChallenge{0, s.Id, challenge, &now, value, nil, ""}, err
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -189,7 +217,7 @@ type ErroredChallenge struct {
|
|||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s Student) RegisterChallengeError(challenge int, err error) error {
|
||||
func (s *Student) RegisterChallengeError(challenge int, err error) error {
|
||||
if _, errr := DBExec("INSERT INTO student_challenge_errors (id_student, challenge, time, error) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), err.Error()); errr == nil {
|
||||
return nil
|
||||
} else if _, errr := DBExec("UPDATE student_challenge_errors SET time = ?, error = ? WHERE id_student = ? AND challenge = ?", time.Now(), err.Error(), s.Id, challenge); errr != nil {
|
||||
|
@ -199,7 +227,7 @@ func (s Student) RegisterChallengeError(challenge int, err error) error {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) RegisterAccess(ip, mac string) error {
|
||||
func (s *Student) RegisterAccess(ip, mac string) error {
|
||||
if res, err := DBExec("INSERT INTO student_login (id_student, ip, mac, time) VALUES (?, ?, ?, ?)", s.Id, ip, mac, time.Now()); err != nil {
|
||||
return err
|
||||
} else if _, err := res.LastInsertId(); err != nil {
|
||||
|
|
Reference in a new issue