Compare commits

...

8 commits
v3.4.2 ... main

7 changed files with 64 additions and 21 deletions

View file

@ -1,4 +1,4 @@
FROM golang:1.21.4 as build
FROM golang:1.23.1 as build
LABEL author="vlbeaudoin"
@ -11,6 +11,7 @@ ADD data/ data/
ADD handlers/ handlers/
ADD models/ models/
ADD responses/ responses/
ADD sql/ sql/
ADD web/ web/
RUN CGO_ENABLED=0 go build -a -o bottin-agenda .

View file

@ -10,9 +10,9 @@ Nécessite une installation fonctionnelle et accessible de [agecem/bottin](https
Remplir .env avec données d'environnement:
```
BOTTINAGENDA_POSTGRES_DATABASE=bottin-agenda
BOTTINAGENDA_POSTGRES_PASSWORD=bottin-agenda
BOTTINAGENDA_POSTGRES_USER=bottin-agenda
BOTTINAGENDA_DB_DATABASE=bottin-agenda
BOTTINAGENDA_DB_PASSWORD=bottin-agenda
BOTTINAGENDA_DB_USER=bottin-agenda
```
Déployer avec docker-compose:

View file

@ -5,6 +5,7 @@ import (
"fmt"
"git.agecem.com/agecem/bottin-agenda/models"
"git.agecem.com/agecem/bottin-agenda/sql"
_ "github.com/jackc/pgx/stdlib"
"github.com/jmoiron/sqlx"
"github.com/spf13/viper"
@ -59,7 +60,7 @@ func NewDataClient(connection PostgresConnection) (*DataClient, error) {
}
func (d *DataClient) Seed() (int64, error) {
result, err := d.DB.Exec(models.Schema)
result, err := d.DB.Exec(sql.Schema())
if err != nil {
return 0, err
}
@ -89,7 +90,24 @@ func (d *DataClient) InsertTransactions(transactions []models.Transaction) ([]mo
return rowsInserted, errors.New("Impossible d'insérer une transaction sans membre_id")
}
rows, err := tx.NamedQuery("INSERT INTO transactions (membre_id, given_at, is_perpetual) VALUES (:membre_id, current_timestamp, :is_perpetual) RETURNING id, membre_id, is_perpetual;", &transaction)
rows, err := tx.NamedQuery(`
INSERT INTO
transactions (
membre_id,
given_at,
is_perpetual
)
VALUES
(
:membre_id,
current_timestamp,
:is_perpetual
)
RETURNING
id,
membre_id,
is_perpetual;
`, &transaction)
if err != nil {
return rowsInserted, err
}
@ -116,8 +134,16 @@ func (d *DataClient) InsertTransactions(transactions []models.Transaction) ([]mo
func (d *DataClient) GetTransaction(membreID string, is_perpetual bool) (models.Transaction, error) {
var transaction models.Transaction
//err := d.DB.NamedQuery("SELECT * FROM transactions WHERE membre_id=:membre_id AND is_perpetual=:is_perpetual LIMIT 1;"
err := d.DB.Get(&transaction, "SELECT * FROM transactions WHERE membre_id = $1 AND is_perpetual = $2 LIMIT 1;", membreID, is_perpetual)
err := d.DB.Get(&transaction, `
SELECT
*
FROM
transactions
WHERE
membre_id = $1 AND
is_perpetual = $2
LIMIT 1;
`, membreID, is_perpetual)
if err != nil {
return transaction, err
}
@ -131,7 +157,13 @@ func (d *DataClient) GetTransaction(membreID string, is_perpetual bool) (models.
func (d *DataClient) ListTransactions() ([]models.Transaction, error) {
var transactions []models.Transaction
if err := d.DB.Select(&transactions, "SELECT * FROM transactions LIMIT 20000;"); err != nil {
if err := d.DB.Select(&transactions, `
SELECT
*
FROM
transactions
LIMIT 20000;
`); err != nil {
return transactions, err
}

View file

@ -3,9 +3,9 @@ services:
db:
image: 'docker.io/library/postgres:16.1'
environment:
POSTGRES_DATABASE: "${BOTTINAGENDA_POSTGRES_DATABASE}"
POSTGRES_PASSWORD: "${BOTTINAGENDA_POSTGRES_PASSWORD}"
POSTGRES_USER: "${BOTTINAGENDA_POSTGRES_USER}"
POSTGRES_DATABASE: "${BOTTINAGENDA_DB_DATABASE:?}"
POSTGRES_PASSWORD: "${BOTTINAGENDA_DB_PASSWORD:?}"
POSTGRES_USER: "${BOTTINAGENDA_DB_USER:?}"
volumes:
- 'db-data:/var/lib/postgresql/data'
restart: 'unless-stopped'
@ -14,6 +14,7 @@ services:
depends_on:
- db
build: .
env_file: '.env'
image: 'git.agecem.com/agecem/bottin-agenda:latest'
ports:
- '1313:1313'
@ -26,6 +27,7 @@ services:
depends_on:
- api
build: .
env_file: '.env'
image: 'git.agecem.com/agecem/bottin-agenda:latest'
ports:
- '2313:2313'

View file

@ -2,15 +2,6 @@ package models
import "time"
var Schema = `
CREATE TABLE IF NOT EXISTS transactions (
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
membre_id VARCHAR(7),
given_at TIMESTAMP,
is_perpetual BOOLEAN
);
`
type Transaction struct {
ID string `db:"id" json:"id"`
MembreID string `db:"membre_id" json:"membre_id"`

7
sql/schema.sql Normal file
View file

@ -0,0 +1,7 @@
-- Schema
CREATE TABLE IF NOT EXISTS transactions (
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
membre_id VARCHAR(7) NOT NULL,
given_at TIMESTAMP DEFAULT current_timestamp,
is_perpetual BOOLEAN NOT NULL
);

10
sql/sql.go Normal file
View file

@ -0,0 +1,10 @@
package sql
import _ "embed"
//go:embed schema.sql
var schema string
func Schema() string {
return schema
}