admin: claims now reference exercices

This commit is contained in:
nemunaire 2020-01-20 09:24:24 +01:00
commit 2e3f7c6894
6 changed files with 76 additions and 20 deletions

View file

@ -363,12 +363,14 @@ CREATE TABLE IF NOT EXISTS claims(
id_claim INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
subject VARCHAR(255) NOT NULL,
id_team INTEGER,
id_exercice INTEGER,
id_assignee INTEGER,
creation TIMESTAMP NOT NULL,
state ENUM('new', 'need-info', 'confirmed', 'in-progress', 'need-review', 'closed', 'invalid') NOT NULL,
priority ENUM('low', 'medium', 'high', 'critical') NOT NULL,
FOREIGN KEY(id_assignee) REFERENCES claim_assignees(id_assignee),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
FOREIGN KEY(id_team) REFERENCES teams(id_team),
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
`); err != nil {
return err

View file

@ -10,6 +10,7 @@ type Claim struct {
Id int64 `json:"id"`
Subject string `json:"subject"`
IdTeam *int64 `json:"id_team"`
IdExercice *int64 `json:"id_exercice"`
IdAssignee *int64 `json:"id_assignee"`
Creation time.Time `json:"creation"`
State string `json:"state"`
@ -18,21 +19,21 @@ type Claim struct {
// GetClaim retrieves the claim with the given identifier.
func GetClaim(id int64) (c Claim, err error) {
err = DBQueryRow("SELECT id_claim, subject, id_team, id_assignee, creation, state, priority FROM claims WHERE id_claim = ?", id).Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdAssignee, &c.Creation, &c.State, &c.Priority)
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) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_assignee, creation, state, priority FROM claims"); err != nil {
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims"); err != nil {
return
}
defer rows.Close()
for rows.Next() {
var c Claim
if err = rows.Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdAssignee, &c.Creation, &c.State, &c.Priority); err != nil {
if err = rows.Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdExercice, &c.IdAssignee, &c.Creation, &c.State, &c.Priority); err != nil {
return
}
res = append(res, c)
@ -45,14 +46,14 @@ func GetClaims() (res []Claim, err error) {
// GetClaims returns a list of all Claim registered for the Team.
func (t Team) GetClaims() (res []Claim, err error) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_claim, subject, IdTeam, id_assignee, creation, state, priority FROM claims WHERE IdTeam = ?", t.Id); err != nil {
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE IdTeam = ?", t.Id); err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var c Claim
if err = rows.Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdAssignee, &c.Creation, &c.State, &c.Priority); err != nil {
if err = rows.Scan(&c.Id, &c.Subject, &c.IdTeam, &c.IdExercice, &c.IdAssignee, &c.Creation, &c.State, &c.Priority); err != nil {
return
}
res = append(res, c)
@ -63,7 +64,7 @@ func (t Team) 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, 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
@ -71,6 +72,13 @@ func NewClaim(subject string, team *Team, assignee *ClaimAssignee, priority stri
tid = &team.Id
}
var eid *int64
if exercice == nil {
eid = nil
} else {
eid = &exercice.Id
}
var aid *int64
if assignee == nil {
aid = nil
@ -78,12 +86,12 @@ func NewClaim(subject string, team *Team, assignee *ClaimAssignee, priority stri
aid = &assignee.Id
}
if res, err := DBExec("INSERT INTO claims (subject, id_team, id_assignee, creation, state, priority) VALUES (?, ?, ?, ?, ?, ?)", subject, tid, aid, time.Now(), "new", priority); err != nil {
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
} else if cid, err := res.LastInsertId(); err != nil {
return Claim{}, err
} else {
return Claim{cid, subject, tid, aid, time.Now(), "new", priority}, nil
return Claim{cid, subject, tid, eid, aid, time.Now(), "new", priority}, nil
}
}
@ -103,9 +111,25 @@ 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) {
if c.IdExercice == nil {
return nil, nil
} else if e, err := GetExercice(*c.IdExercice); err != nil {
return nil, err
} else {
return &e, nil
}
}
// SetExercice defines the Exercice that is linked to this issue.
func (c Claim) SetExercice(e Exercice) {
c.IdExercice = &e.Id
}
// Update applies modifications back to the database.
func (c Claim) Update() (int64, error) {
if res, err := DBExec("UPDATE claims SET subject = ?, id_team = ?, id_assignee = ?, creation = ?, state = ?, priority = ? WHERE id_claim = ?", c.Subject, c.IdTeam, c.IdAssignee, c.Creation, c.State, c.Priority, c.Id); err != nil {
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 {
return 0, err