Use pointer instead of struct
This commit is contained in:
parent
853477e54a
commit
6d8f38d749
18 changed files with 187 additions and 142 deletions
|
|
@ -11,40 +11,41 @@ type Session struct {
|
|||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
func GetSession(id []byte) (s Session, err error) {
|
||||
func GetSession(id []byte) (s *Session, err error) {
|
||||
s = new(Session)
|
||||
err = DBQueryRow("SELECT id_session, id_student, time FROM student_sessions WHERE id_session=?", id).Scan(&s.Id, &s.IdStudent, &s.Time)
|
||||
return
|
||||
}
|
||||
|
||||
func NewSession() (Session, error) {
|
||||
func NewSession() (*Session, error) {
|
||||
session_id := make([]byte, 255)
|
||||
if _, err := rand.Read(session_id); err != nil {
|
||||
return Session{}, err
|
||||
return nil, err
|
||||
} else if _, err := DBExec("INSERT INTO student_sessions (id_session, time) VALUES (?, ?)", session_id, time.Now()); err != nil {
|
||||
return Session{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return Session{session_id, nil, time.Now()}, nil
|
||||
return &Session{session_id, nil, time.Now()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (student Student) NewSession() (Session, error) {
|
||||
func (student *Student) NewSession() (*Session, error) {
|
||||
session_id := make([]byte, 255)
|
||||
if _, err := rand.Read(session_id); err != nil {
|
||||
return Session{}, err
|
||||
return nil, err
|
||||
} else if _, err := DBExec("INSERT INTO student_sessions (id_session, id_student, time) VALUES (?, ?, ?)", session_id, student.Id, time.Now()); err != nil {
|
||||
return Session{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return Session{session_id, &student.Id, time.Now()}, nil
|
||||
return &Session{session_id, &student.Id, time.Now()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s Session) SetStudent(student Student) (Session, error) {
|
||||
func (s *Session) SetStudent(student *Student) (*Session, error) {
|
||||
s.IdStudent = &student.Id
|
||||
_, err := s.Update()
|
||||
return s, err
|
||||
}
|
||||
|
||||
func (s Session) Update() (int64, error) {
|
||||
func (s *Session) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE student_sessions SET id_student = ?, time = ? WHERE id_session = ?", s.IdStudent, s.Time, s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
@ -54,7 +55,7 @@ func (s Session) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Session) Delete() (int64, error) {
|
||||
func (s *Session) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM student_sessions WHERE id_session = ?", s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
|
|||
Reference in a new issue