Use pointer receiver more offen

This commit is contained in:
nemunaire 2021-11-22 15:35:07 +01:00
parent 6999b4e728
commit c7569b5e54
59 changed files with 688 additions and 672 deletions

View file

@ -20,13 +20,14 @@ type Claim struct {
}
// GetClaim retrieves the claim with the given identifier.
func GetClaim(id int64) (c Claim, err error) {
func GetClaim(id int64) (c *Claim, err error) {
c = &Claim{}
err = DBQueryRow("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE id_claim = ?", id).Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdExercice, &c.IdAssignee, &c.Creation, &c.State, &c.Priority)
return
}
// GetClaims returns a list of all Claim registered in the database.
func GetClaims() (res []Claim, err error) {
func GetClaims() (res []*Claim, err error) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims"); err != nil {
return
@ -34,7 +35,7 @@ func GetClaims() (res []Claim, err error) {
defer rows.Close()
for rows.Next() {
var c Claim
c := &Claim{}
if err = rows.Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdExercice, &c.IdAssignee, &c.Creation, &c.State, &c.Priority); err != nil {
return
}
@ -46,13 +47,14 @@ func GetClaims() (res []Claim, err error) {
}
// GetClaim retrieves the claim with the given identifier and registered for the given Team.
func (t Team) GetClaim(id int64) (c Claim, err error) {
func (t *Team) GetClaim(id int64) (c *Claim, err error) {
c = &Claim{}
err = DBQueryRow("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE id_claim = ? AND id_team = ?", id, t.Id).Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdExercice, &c.IdAssignee, &c.Creation, &c.State, &c.Priority)
return
}
// GetClaims returns a list of all Claim registered for the Team.
func (t Team) GetClaims() (res []Claim, err error) {
func (t *Team) GetClaims() (res []*Claim, err error) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE id_team = ?", t.Id); err != nil {
return nil, err
@ -60,7 +62,7 @@ func (t Team) GetClaims() (res []Claim, err error) {
defer rows.Close()
for rows.Next() {
var c Claim
c := &Claim{}
if err = rows.Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdExercice, &c.IdAssignee, &c.Creation, &c.State, &c.Priority); err != nil {
return
}
@ -72,7 +74,7 @@ func (t Team) GetClaims() (res []Claim, err error) {
}
// GetExercices returns a list of all Claim registered for the Exercice.
func (e Exercice) GetClaims() (res []Claim, err error) {
func (e *Exercice) GetClaims() (res []*Claim, err error) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE id_exercice = ?", e.Id); err != nil {
return nil, err
@ -80,7 +82,7 @@ func (e Exercice) GetClaims() (res []Claim, err error) {
defer rows.Close()
for rows.Next() {
var c Claim
c := &Claim{}
if err = rows.Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdExercice, &c.IdAssignee, &c.Creation, &c.State, &c.Priority); err != nil {
return
}
@ -92,7 +94,7 @@ func (e Exercice) GetClaims() (res []Claim, err error) {
}
// NewClaim creates and fills a new struct Claim and registers it into the database.
func NewClaim(subject string, team *Team, exercice *Exercice, assignee *ClaimAssignee, priority string) (Claim, error) {
func NewClaim(subject string, team *Team, exercice *Exercice, assignee *ClaimAssignee, priority string) (*Claim, error) {
var tid *int64
if team == nil {
tid = nil
@ -115,48 +117,48 @@ func NewClaim(subject string, team *Team, exercice *Exercice, assignee *ClaimAss
}
if res, err := DBExec("INSERT INTO claims (subject, id_team, id_exercice, id_assignee, creation, state, priority) VALUES (?, ?, ?, ?, ?, ?, ?)", subject, tid, eid, aid, time.Now(), "new", priority); err != nil {
return Claim{}, err
return nil, err
} else if cid, err := res.LastInsertId(); err != nil {
return Claim{}, err
return nil, err
} else {
return Claim{cid, subject, tid, eid, aid, time.Now(), "new", priority}, nil
return &Claim{cid, subject, tid, eid, aid, time.Now(), "new", priority}, nil
}
}
// GetTeam returns the Team linked to the issue, if any.
func (c Claim) GetTeam() (*Team, error) {
func (c *Claim) GetTeam() (*Team, error) {
if c.IdTeam == nil {
return nil, nil
} else if t, err := GetTeam(*c.IdTeam); err != nil {
return nil, err
} else {
return &t, nil
return t, nil
}
}
// SetTeam defines the Team that is linked to this issue.
func (c Claim) SetTeam(t Team) {
func (c *Claim) SetTeam(t Team) {
c.IdTeam = &t.Id
}
// GetExercice returns the Exercice linked to the issue, if any.
func (c Claim) GetExercice() (*Exercice, error) {
func (c *Claim) GetExercice() (*Exercice, error) {
if c.IdExercice == nil {
return nil, nil
} else if e, err := GetExercice(*c.IdExercice); err != nil {
return nil, err
} else {
return &e, nil
return e, nil
}
}
// SetExercice defines the Exercice that is linked to this issue.
func (c Claim) SetExercice(e Exercice) {
func (c *Claim) SetExercice(e Exercice) {
c.IdExercice = &e.Id
}
// Update applies modifications back to the database.
func (c Claim) Update() (int64, error) {
func (c *Claim) Update() (int64, error) {
if res, err := DBExec("UPDATE claims SET subject = ?, id_team = ?, id_exercice = ?, id_assignee = ?, creation = ?, state = ?, priority = ? WHERE id_claim = ?", c.Subject, c.IdTeam, c.IdExercice, c.IdAssignee, c.Creation, c.State, c.Priority, c.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -167,7 +169,7 @@ func (c Claim) Update() (int64, error) {
}
// Delete the issue from the database.
func (c Claim) Delete() (int64, error) {
func (c *Claim) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM claims WHERE id_claim = ?", c.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -202,13 +204,13 @@ type ClaimDescription struct {
}
// GetLastUpdate returns the date of the latest message written for the given Claim.
func (c Claim) GetLastUpdate() (res *time.Time, err error) {
func (c *Claim) GetLastUpdate() (res *time.Time, err error) {
err = DBQueryRow("SELECT MAX(date) FROM claim_descriptions WHERE id_claim = ? GROUP BY id_claim", c.Id).Scan(&res)
return
}
// GetDescriptions returns a list of all descriptions stored in the database for the Claim.
func (c Claim) GetDescriptions() (res []ClaimDescription, err error) {
func (c *Claim) GetDescriptions() (res []*ClaimDescription, err error) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_description, id_assignee, content, date, publish FROM claim_descriptions WHERE id_claim = ?", c.Id); err != nil {
return nil, err
@ -216,7 +218,7 @@ func (c Claim) GetDescriptions() (res []ClaimDescription, err error) {
defer rows.Close()
for rows.Next() {
var d ClaimDescription
d := &ClaimDescription{}
if err = rows.Scan(&d.Id, &d.IdAssignee, &d.Content, &d.Date, &d.Publish); err != nil {
return
}
@ -228,24 +230,30 @@ func (c Claim) GetDescriptions() (res []ClaimDescription, err error) {
}
// AddDescription append in the database a new description; then returns the corresponding structure.
func (c Claim) AddDescription(content string, assignee ClaimAssignee, publish bool) (ClaimDescription, error) {
if res, err := DBExec("INSERT INTO claim_descriptions (id_claim, id_assignee, content, date, publish) VALUES (?, ?, ?, ?, ?)", c.Id, assignee.Id, content, time.Now(), publish); err != nil {
return ClaimDescription{}, err
func (c *Claim) AddDescription(content string, assignee *ClaimAssignee, publish bool) (*ClaimDescription, error) {
var assignee_id *int64
if assignee != nil {
assignee_id = &assignee.Id
}
if res, err := DBExec("INSERT INTO claim_descriptions (id_claim, id_assignee, content, date, publish) VALUES (?, ?, ?, ?, ?)", c.Id, assignee_id, content, time.Now(), publish); err != nil {
return nil, err
} else if did, err := res.LastInsertId(); err != nil {
return ClaimDescription{}, err
return nil, err
} else {
return ClaimDescription{did, assignee.Id, content, time.Now(), publish}, nil
return &ClaimDescription{did, assignee.Id, content, time.Now(), publish}, nil
}
}
// GetAssignee retrieves an assignee from its identifier.
func (d ClaimDescription) GetAssignee() (a ClaimAssignee, err error) {
func (d *ClaimDescription) GetAssignee() (a *ClaimAssignee, err error) {
a = &ClaimAssignee{}
err = DBQueryRow("SELECT id_assignee, name FROM claim_assignees WHERE id_assignee = ?", d.IdAssignee).Scan(&a.Id, &a.Name)
return
}
// Update applies modifications back to the database
func (d ClaimDescription) Update() (int64, error) {
func (d *ClaimDescription) Update() (int64, error) {
if res, err := DBExec("UPDATE claim_descriptions SET id_assignee = ?, content = ?, date = ?, publish = ? WHERE id_description = ?", d.IdAssignee, d.Content, d.Date, d.Publish, d.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -256,7 +264,7 @@ func (d ClaimDescription) Update() (int64, error) {
}
// Delete the description in the database.
func (d ClaimDescription) Delete() (int64, error) {
func (d *ClaimDescription) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM claim_descriptions WHERE id_description = ?", d.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -273,13 +281,14 @@ type ClaimAssignee struct {
}
// GetAssignee retrieves an assignee from its identifier.
func GetAssignee(id int64) (a ClaimAssignee, err error) {
func GetAssignee(id int64) (a *ClaimAssignee, err error) {
a = &ClaimAssignee{}
err = DBQueryRow("SELECT id_assignee, name FROM claim_assignees WHERE id_assignee = ?", id).Scan(&a.Id, &a.Name)
return
}
// GetAssignees returns a list of all assignees found in the database.
func GetAssignees() (res []ClaimAssignee, err error) {
func GetAssignees() (res []*ClaimAssignee, err error) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_assignee, name FROM claim_assignees"); err != nil {
return
@ -287,7 +296,7 @@ func GetAssignees() (res []ClaimAssignee, err error) {
defer rows.Close()
for rows.Next() {
var a ClaimAssignee
a := &ClaimAssignee{}
if err = rows.Scan(&a.Id, &a.Name); err != nil {
return
}
@ -299,18 +308,18 @@ func GetAssignees() (res []ClaimAssignee, err error) {
}
// NewClaimAssignee creates and fills a new struct ClaimAssignee and registers it into the database.
func NewClaimAssignee(name string) (ClaimAssignee, error) {
func NewClaimAssignee(name string) (*ClaimAssignee, error) {
if res, err := DBExec("INSERT INTO claim_assignees (name) VALUES (?)", name); err != nil {
return ClaimAssignee{}, err
return nil, err
} else if aid, err := res.LastInsertId(); err != nil {
return ClaimAssignee{}, err
return nil, err
} else {
return ClaimAssignee{aid, name}, nil
return &ClaimAssignee{aid, name}, nil
}
}
// Update applies modifications back to the database
func (a ClaimAssignee) Update() (int64, error) {
func (a *ClaimAssignee) Update() (int64, error) {
if res, err := DBExec("UPDATE claim_assignees SET name = ? WHERE id_assignee = ?", a.Name, a.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -321,7 +330,7 @@ func (a ClaimAssignee) Update() (int64, error) {
}
// Delete the assignee in the database.
func (a ClaimAssignee) Delete() (int64, error) {
func (a *ClaimAssignee) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM claim_assignees WHERE id_assignee = ?", a.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -343,18 +352,18 @@ func ClearAssignees() (int64, error) {
}
// GetAssignee returns the assignee assigned to the claim.
func (c Claim) GetAssignee() (*ClaimAssignee, error) {
func (c *Claim) GetAssignee() (*ClaimAssignee, error) {
if c.IdAssignee == nil {
return nil, nil
} else if a, err := GetAssignee(*c.IdAssignee); err != nil {
return nil, err
} else {
return &a, nil
return a, nil
}
}
// SetAssignee defines the assignee that'll handle the claim.
func (c Claim) SetAssignee(a ClaimAssignee) {
func (c *Claim) SetAssignee(a ClaimAssignee) {
c.IdAssignee = &a.Id
}
@ -375,8 +384,8 @@ type teamIssueFile struct {
Texts []teamIssueText `json:"texts"`
}
func (t Team) MyIssueFile() (ret []teamIssueFile, err error) {
var claims []Claim
func (t *Team) MyIssueFile() (ret []teamIssueFile, err error) {
var claims []*Claim
if claims, err = t.GetClaims(); err == nil {
for _, claim := range claims {
var exercice *string = nil
@ -395,7 +404,7 @@ func (t Team) MyIssueFile() (ret []teamIssueFile, err error) {
}
if descriptions, err := claim.GetDescriptions(); err != nil {
return nil, fmt.Errorf("Error occurs during description retrieval (cid=%d): %w", claim.Id, err)
return nil, fmt.Errorf("error occurs during description retrieval (cid=%d): %w", claim.Id, err)
} else {
tif := teamIssueFile{
Id: claim.Id,
@ -411,7 +420,7 @@ func (t Team) MyIssueFile() (ret []teamIssueFile, err error) {
for _, description := range descriptions {
if description.Publish {
if people, err := description.GetAssignee(); err != nil {
return nil, fmt.Errorf("Error ocurs during assignee retrieval (aid=%d): %w", description.IdAssignee, err)
return nil, fmt.Errorf("error ocurs during assignee retrieval (aid=%d): %w", description.IdAssignee, err)
} else {
tif.Texts = append(tif.Texts, teamIssueText{
Content: description.Content,
@ -426,7 +435,7 @@ func (t Team) MyIssueFile() (ret []teamIssueFile, err error) {
}
}
} else {
err = fmt.Errorf("Error occurs during claim retrieval: %w", err)
err = fmt.Errorf("error occurs during claim retrieval: %w", err)
}
return