diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-23 11:16:38 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-23 11:16:38 +0100 |
commit | 0f951dcf029d4af284467543a3afdf5bf6581a20 (patch) | |
tree | a48384210cdc168e3bd3ccff6d6d516eeed9e748 /internal/models | |
parent | 8b084e9fe7a5f3a04c32daf9a24f7f2cf67300f9 (diff) |
switched to Django
Diffstat (limited to 'internal/models')
-rw-r--r-- | internal/models/engagement.go | 54 | ||||
-rw-r--r-- | internal/models/errors.go | 7 | ||||
-rw-r--r-- | internal/models/operation.go | 52 | ||||
-rw-r--r-- | internal/models/organisations.go | 85 | ||||
-rw-r--r-- | internal/models/person.go | 51 |
5 files changed, 0 insertions, 249 deletions
diff --git a/internal/models/engagement.go b/internal/models/engagement.go deleted file mode 100644 index 87e0786..0000000 --- a/internal/models/engagement.go +++ /dev/null @@ -1,54 +0,0 @@ -package models - -import ( - "database/sql" - "fmt" - "strconv" - "time" -) - -type EngagementStrategy struct { - ID int - ValidFrom time.Time - ValidTo time.Time - Operation Operation -} - -func (es *EngagementStrategy) FormatForTable() string { - startYear := strconv.Itoa(es.ValidFrom.Year()) - endYear := strconv.Itoa(es.ValidTo.Year()) - return fmt.Sprintf("%s-%s", startYear, endYear) -} - -type EngagementStrategyModel struct { - DB *sql.DB -} - -func (m *EngagementStrategyModel) GetForOperation(id int) ([]EngagementStrategy, error) { - stmt := `SELECT id, valid_from, valid_to FROM engagement_strategies -WHERE operation_id = ?` - rows, err := m.DB.Query(stmt, id) - if err != nil { - return nil, err - } - - defer rows.Close() - - var esses []EngagementStrategy - - for rows.Next() { - var es EngagementStrategy - err = rows.Scan(&es.ID, &es.ValidFrom, &es.ValidTo) - if err != nil { - return nil, err - } - - esses = append(esses, es) - } - - if err = rows.Err(); err != nil { - return nil, err - } - - return esses, nil -} diff --git a/internal/models/errors.go b/internal/models/errors.go deleted file mode 100644 index a70c7dc..0000000 --- a/internal/models/errors.go +++ /dev/null @@ -1,7 +0,0 @@ -package models - -import ( - "errors" -) - -var ErrNoRecord = errors.New("models: no matching record found") diff --git a/internal/models/operation.go b/internal/models/operation.go deleted file mode 100644 index ecfd31b..0000000 --- a/internal/models/operation.go +++ /dev/null @@ -1,52 +0,0 @@ -package models - -import ( - "database/sql" - "time" -) - -type Operation struct { - ID int - Name string - Description string - Created time.Time - OrganisationName string - EngagementStrategies []EngagementStrategy -} - -type OperationModel struct { - DB *sql.DB -} - -func (m *OperationModel) ListAll() ([]Operation, error) { - - stmt := `SELECT op.id, op.name, op.description, op.created, org.name - FROM operations op - INNER JOIN organisations org ON op.organisation_id=org.id` - - // stmt := `SELECT * FROM operations` - - rows, err := m.DB.Query(stmt) - if err != nil { - return nil, err - } - - defer rows.Close() - - var ops []Operation - - for rows.Next() { - var o Operation - err = rows.Scan(&o.ID, &o.Name, &o.Description, &o.Created, &o.OrganisationName) - if err != nil { - return nil, err - } - - ops = append(ops, o) - } - - if err = rows.Err(); err != nil { - return nil, err - } - return ops, err -} diff --git a/internal/models/organisations.go b/internal/models/organisations.go deleted file mode 100644 index 88f7bc9..0000000 --- a/internal/models/organisations.go +++ /dev/null @@ -1,85 +0,0 @@ -package models - -import ( - "database/sql" - "errors" - "time" -) - -type Organisation struct { - ID int - Name string - Created time.Time -} - -type OrganisationModel struct { - DB *sql.DB -} - -func (m *OrganisationModel) Insert(name string) (int, error) { - stmt := `INSERT INTO organisations (name, created) - VALUEs (?, UTC_TIMESTAMP())` - - result, err := m.DB.Exec(stmt, name) - if err != nil { - return 0, err - } - - id, err := result.LastInsertId() - if err != nil { - return 0, err - } - return int(id), nil -} - -func (m *OrganisationModel) Get(id int) (Organisation, error) { - stmt := `SELECT id, name, created FROM organisations - WHERE id = ?` - - row := m.DB.QueryRow(stmt, id) - - var o Organisation - - err := row.Scan(&o.ID, &o.Name, &o.Created) - if err != nil { - if errors.Is(err, sql.ErrNoRows) { - return Organisation{}, ErrNoRecord - } else { - return Organisation{}, err - } - } - return o, nil -} - -// Ten most recent... -func (m *OrganisationModel) Latest() ([]Organisation, error) { - // Pick out the last 10 - stmt := `SELECT id, name, created FROM organisations - ORDER BY id DESC LIMIT 10` - - rows, err := m.DB.Query(stmt) - if err != nil { - return nil, err - } - - defer rows.Close() - - var organisations []Organisation - - for rows.Next() { - var o Organisation - - err = rows.Scan(&o.ID, &o.Name, &o.Created) - if err != nil { - return nil, err - } - - organisations = append(organisations, o) - } - - if err = rows.Err(); err != nil { - return nil, err - } - - return organisations, err -} diff --git a/internal/models/person.go b/internal/models/person.go deleted file mode 100644 index d2b8585..0000000 --- a/internal/models/person.go +++ /dev/null @@ -1,51 +0,0 @@ -package models - -import ( - "database/sql" - "time" -) - -type Person struct { - ID string - FirstName string - LastName string - OrganisationName string - OrganisationID int - RoleName string - Created time.Time -} - -type PersonModel struct { - DB *sql.DB -} - -func (m *PersonModel) ListAll() ([]Person, error) { - stmt := `SELECT p.id, p.first_name, p.last_name, p.role_name, org.name, org.id - FROM persons p - INNER JOIN organisations org ON p.organisation_id=org.id` - - rows, err := m.DB.Query(stmt) - if err != nil { - return nil, err - } - - defer rows.Close() - - var ps []Person - - for rows.Next() { - var p Person - err = rows.Scan(&p.ID, &p.FirstName, &p.LastName, &p.RoleName, &p.OrganisationName, &p.OrganisationID) - if err != nil { - return nil, err - } - - ps = append(ps, p) - } - - if err = rows.Err(); err != nil { - return nil, err - } - - return ps, err -} |