301 lines
6.3 KiB
Go
301 lines
6.3 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.27.0
|
|
// source: incidents.sql
|
|
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
|
|
"dynatron.me/x/stillbox/internal/jsontypes"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const addToIncident = `-- name: AddToIncident :exec
|
|
WITH inp AS (
|
|
SELECT
|
|
UNNEST($2::UUID[]) id,
|
|
UNNEST($3::JSONB[]) notes
|
|
) INSERT INTO incidents_calls(
|
|
incident_id,
|
|
call_id,
|
|
calls_tbl_id,
|
|
call_date,
|
|
notes
|
|
)
|
|
SELECT
|
|
$1::UUID,
|
|
inp.id,
|
|
inp.id,
|
|
c.call_date,
|
|
inp.notes
|
|
FROM inp
|
|
JOIN calls c ON c.id = inp.id
|
|
`
|
|
|
|
func (q *Queries) AddToIncident(ctx context.Context, incidentID uuid.UUID, callIds []uuid.UUID, notes [][]byte) error {
|
|
_, err := q.db.Exec(ctx, addToIncident, incidentID, callIds, notes)
|
|
return err
|
|
}
|
|
|
|
const createIncident = `-- name: CreateIncident :one
|
|
INSERT INTO incidents (
|
|
id,
|
|
name,
|
|
description,
|
|
start_time,
|
|
end_time,
|
|
location,
|
|
metadata
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7
|
|
)
|
|
RETURNING id, name, description, start_time, end_time, location, metadata
|
|
`
|
|
|
|
type CreateIncidentParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Description *string `json:"description"`
|
|
StartTime pgtype.Timestamptz `json:"start_time"`
|
|
EndTime pgtype.Timestamptz `json:"end_time"`
|
|
Location []byte `json:"location"`
|
|
Metadata jsontypes.Metadata `json:"metadata"`
|
|
}
|
|
|
|
func (q *Queries) CreateIncident(ctx context.Context, arg CreateIncidentParams) (Incident, error) {
|
|
row := q.db.QueryRow(ctx, createIncident,
|
|
arg.ID,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.StartTime,
|
|
arg.EndTime,
|
|
arg.Location,
|
|
arg.Metadata,
|
|
)
|
|
var i Incident
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.StartTime,
|
|
&i.EndTime,
|
|
&i.Location,
|
|
&i.Metadata,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteIncident = `-- name: DeleteIncident :exec
|
|
DELETE FROM incidents CASCADE WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteIncident(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteIncident, id)
|
|
return err
|
|
}
|
|
|
|
const getIncident = `-- name: GetIncident :one
|
|
SELECT
|
|
i.id,
|
|
i.name,
|
|
i.description,
|
|
i.start_time,
|
|
i.end_time,
|
|
i.location,
|
|
i.metadata
|
|
FROM incidents i
|
|
WHERE i.id = $1
|
|
`
|
|
|
|
func (q *Queries) GetIncident(ctx context.Context, id uuid.UUID) (Incident, error) {
|
|
row := q.db.QueryRow(ctx, getIncident, id)
|
|
var i Incident
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.StartTime,
|
|
&i.EndTime,
|
|
&i.Location,
|
|
&i.Metadata,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const incidentCalls = `-- name: IncidentCalls :many
|
|
SELECT
|
|
ic.incident_id, call_date,
|
|
ic.call_id,
|
|
ic.notes
|
|
FROM incidents_calls ic
|
|
`
|
|
|
|
type IncidentCallsRow struct {
|
|
IncidentID uuid.UUID `json:"incident_id"`
|
|
CallDate pgtype.Timestamptz `json:"call_date"`
|
|
CallID uuid.UUID `json:"call_id"`
|
|
Notes []byte `json:"notes"`
|
|
}
|
|
|
|
// INCOMPLETE
|
|
func (q *Queries) IncidentCalls(ctx context.Context) ([]IncidentCallsRow, error) {
|
|
rows, err := q.db.Query(ctx, incidentCalls)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []IncidentCallsRow
|
|
for rows.Next() {
|
|
var i IncidentCallsRow
|
|
if err := rows.Scan(
|
|
&i.IncidentID,
|
|
&i.CallDate,
|
|
&i.CallID,
|
|
&i.Notes,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listIncidentsCount = `-- name: ListIncidentsCount :one
|
|
SELECT COUNT(*)
|
|
FROM incidents i
|
|
WHERE
|
|
CASE WHEN $1::TIMESTAMPTZ IS NOT NULL THEN
|
|
i.start_time >= $1 ELSE TRUE END AND
|
|
CASE WHEN $2::TIMESTAMPTZ IS NOT NULL THEN
|
|
i.start_time <= $2 ELSE TRUE END
|
|
`
|
|
|
|
func (q *Queries) ListIncidentsCount(ctx context.Context, start pgtype.Timestamptz, end pgtype.Timestamptz) (int64, error) {
|
|
row := q.db.QueryRow(ctx, listIncidentsCount, start, end)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const listIncidentsP = `-- name: ListIncidentsP :many
|
|
SELECT
|
|
i.id,
|
|
i.name,
|
|
i.description,
|
|
i.start_time,
|
|
i.end_time,
|
|
i.location,
|
|
i.metadata
|
|
FROM incidents i
|
|
WHERE
|
|
CASE WHEN $1::TIMESTAMPTZ IS NOT NULL THEN
|
|
i.start_time >= $1 ELSE TRUE END AND
|
|
CASE WHEN $2::TIMESTAMPTZ IS NOT NULL THEN
|
|
i.start_time <= $2 ELSE TRUE END
|
|
ORDER BY
|
|
CASE WHEN $3::TEXT = 'asc' THEN i.start_time END ASC,
|
|
CASE WHEN $3::TEXT = 'desc' THEN i.start_time END DESC
|
|
OFFSET $4 ROWS
|
|
FETCH NEXT $5 ROWS ONLY
|
|
`
|
|
|
|
type ListIncidentsPParams struct {
|
|
Start pgtype.Timestamptz `json:"start"`
|
|
End pgtype.Timestamptz `json:"end"`
|
|
Direction string `json:"direction"`
|
|
Offset int32 `json:"offset"`
|
|
PerPage int32 `json:"per_page"`
|
|
}
|
|
|
|
func (q *Queries) ListIncidentsP(ctx context.Context, arg ListIncidentsPParams) ([]Incident, error) {
|
|
rows, err := q.db.Query(ctx, listIncidentsP,
|
|
arg.Start,
|
|
arg.End,
|
|
arg.Direction,
|
|
arg.Offset,
|
|
arg.PerPage,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Incident
|
|
for rows.Next() {
|
|
var i Incident
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.StartTime,
|
|
&i.EndTime,
|
|
&i.Location,
|
|
&i.Metadata,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateIncident = `-- name: UpdateIncident :one
|
|
UPDATE incidents
|
|
SET
|
|
name = COALESCE($1, name),
|
|
description = COALESCE($2, description),
|
|
start_time = COALESCE($3, start_time),
|
|
end_time = COALESCE($4, end_time),
|
|
location = COALESCE($5, location),
|
|
metadata = COALESCE($6, metadata)
|
|
WHERE
|
|
id = $7
|
|
RETURNING id, name, description, start_time, end_time, location, metadata
|
|
`
|
|
|
|
type UpdateIncidentParams struct {
|
|
Name *string `json:"name"`
|
|
Description *string `json:"description"`
|
|
StartTime pgtype.Timestamptz `json:"start_time"`
|
|
EndTime pgtype.Timestamptz `json:"end_time"`
|
|
Location []byte `json:"location"`
|
|
Metadata jsontypes.Metadata `json:"metadata"`
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateIncident(ctx context.Context, arg UpdateIncidentParams) (Incident, error) {
|
|
row := q.db.QueryRow(ctx, updateIncident,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.StartTime,
|
|
arg.EndTime,
|
|
arg.Location,
|
|
arg.Metadata,
|
|
arg.ID,
|
|
)
|
|
var i Incident
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.StartTime,
|
|
&i.EndTime,
|
|
&i.Location,
|
|
&i.Metadata,
|
|
)
|
|
return i, err
|
|
}
|