server/libfic/member.go

99 lines
3.0 KiB
Go
Raw Normal View History

2016-01-13 19:25:25 +00:00
package fic
2016-01-07 17:43:02 +00:00
import ()
2018-03-09 18:07:08 +00:00
// Member represents a team member
2016-01-07 17:43:02 +00:00
type Member struct {
Id int64 `json:"id"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Nickname string `json:"nickname"`
Company string `json:"company"`
2016-01-07 17:43:02 +00:00
}
2018-03-09 18:07:08 +00:00
// GetMembers retrieves the members of the Team
2021-11-22 14:35:07 +00:00
func (t *Team) GetMembers() ([]*Member, error) {
if rows, err := DBQuery("SELECT id_member, firstname, lastname, nickname, company FROM team_members WHERE id_team = ?", t.Id); err != nil {
2016-01-07 17:43:02 +00:00
return nil, err
} else {
defer rows.Close()
2021-11-22 14:35:07 +00:00
var members []*Member
2016-01-07 17:43:02 +00:00
for rows.Next() {
2021-11-22 14:35:07 +00:00
m := &Member{}
if err := rows.Scan(&m.Id, &m.Firstname, &m.Lastname, &m.Nickname, &m.Company); err != nil {
2016-01-07 17:43:02 +00:00
return nil, err
}
members = append(members, m)
}
if err := rows.Err(); err != nil {
return nil, err
}
return members, nil
}
}
2018-05-11 23:08:37 +00:00
// AddMember creates and fills a new struct Member and registers it into the database.
2021-11-22 14:35:07 +00:00
func (t *Team) AddMember(firstname string, lastname string, nickname string, company string) (*Member, error) {
if res, err := DBExec("INSERT INTO team_members (id_team, firstname, lastname, nickname, company) VALUES (?, ?, ?, ?, ?)", t.Id, firstname, lastname, nickname, company); err != nil {
2021-11-22 14:35:07 +00:00
return nil, err
2016-01-07 17:43:02 +00:00
} else if mid, err := res.LastInsertId(); err != nil {
2021-11-22 14:35:07 +00:00
return nil, err
2016-01-07 17:43:02 +00:00
} else {
2021-11-22 14:35:07 +00:00
return &Member{mid, firstname, lastname, nickname, company}, nil
2016-01-07 17:43:02 +00:00
}
}
2018-05-11 23:08:37 +00:00
// GainMember associates (or registers, it if it doesn't exists yet) a member to the team.
2021-11-22 14:35:07 +00:00
func (t *Team) GainMember(m *Member) error {
2017-12-21 21:18:18 +00:00
if m.Id == 0 {
if res, err := DBExec("INSERT INTO team_members (id_team, firstname, lastname, nickname, company) VALUES (?, ?, ?, ?, ?)", t.Id, m.Firstname, m.Lastname, m.Nickname, m.Company); err != nil {
return err
} else if _, err := res.LastInsertId(); err != nil {
return err
} else {
return nil
}
} else if res, err := DBExec("UPDATE team_members SET id_team = ? WHERE id_member = ?", t.Id, m.Id); err != nil {
2016-01-07 17:43:02 +00:00
return err
} else if _, err := res.RowsAffected(); err != nil {
return err
} else {
return nil
}
}
2018-05-11 23:08:37 +00:00
// Update applies modifications back to the database.
2021-11-22 14:35:07 +00:00
func (m *Member) Update() (int64, error) {
if res, err := DBExec("UPDATE team_members SET firstname = ?, lastname = ?, nickname = ?, company = ? WHERE id_member = ?", m.Firstname, m.Lastname, m.Nickname, m.Company, m.Id); err != nil {
2016-01-07 17:43:02 +00:00
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
2018-05-11 23:08:37 +00:00
// Delete the member from the database.
2021-11-22 14:35:07 +00:00
func (m *Member) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM team_members WHERE id_member = ?", m.Id); err != nil {
2016-01-07 17:43:02 +00:00
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
2017-01-28 18:36:28 +00:00
2018-05-11 23:08:37 +00:00
// ClearMembers deletes members in the team.
2021-11-22 14:35:07 +00:00
func (t *Team) ClearMembers() (int64, error) {
2017-01-28 18:36:28 +00:00
if res, err := DBExec("DELETE FROM team_members WHERE id_team = ?", t.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}