Ajouter transactions
Ajouter POST /v2/transactions Valide si une transaction avec la même combinaison membre_id + is_perpetual existe déjà dans une des transactions proposées.
This commit is contained in:
parent
e842982a46
commit
7fa4db7ab9
5 changed files with 145 additions and 8 deletions
|
@ -61,6 +61,8 @@ var apiCmd = &cobra.Command{
|
||||||
|
|
||||||
e.GET("/v2/membres/:membre_id/", handlers.GetMembre)
|
e.GET("/v2/membres/:membre_id/", handlers.GetMembre)
|
||||||
|
|
||||||
|
e.POST("/v2/transactions/", handlers.PostTransactions)
|
||||||
|
|
||||||
// Check bottin is ready
|
// Check bottin is ready
|
||||||
|
|
||||||
bottinHealthResponse, err := bottinConnection.GetHealth()
|
bottinHealthResponse, err := bottinConnection.GetHealth()
|
||||||
|
|
58
data/data.go
58
data/data.go
|
@ -1,6 +1,7 @@
|
||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin-agenda/models"
|
"git.agecem.com/agecem/bottin-agenda/models"
|
||||||
|
@ -71,6 +72,63 @@ func (d *DataClient) Seed() (int64, error) {
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InsertTransactions inserts a slice of Transaction into a database, returning the amount inserted and any error encountered
|
||||||
|
func (d *DataClient) InsertTransactions(transactions []models.Transaction) (int64, error) {
|
||||||
|
var rowsInserted int64
|
||||||
|
|
||||||
|
// Start transaction
|
||||||
|
tx, err := d.DB.Beginx()
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return rowsInserted, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, transaction := range transactions {
|
||||||
|
// Check values
|
||||||
|
if transaction.MembreID == "" {
|
||||||
|
tx.Rollback()
|
||||||
|
return 0, errors.New("Cannot insert transaction with no membre_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := tx.NamedExec("INSERT INTO transactions (membre_id, given_at, is_perpetual) VALUES (:membre_id, current_timestamp, :is_perpetual);", &transaction)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsInserted += rows
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
return rowsInserted, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rowsInserted, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
return transaction, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if transaction.ID == "" {
|
||||||
|
return transaction, fmt.Errorf("No transaction found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return transaction, nil
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// InsertMembres inserts a slice of Membre into a database, returning the amount inserted and any error encountered
|
// InsertMembres inserts a slice of Membre into a database, returning the amount inserted and any error encountered
|
||||||
func (d *DataClient) InsertMembres(membres []models.Membre) (int64, error) {
|
func (d *DataClient) InsertMembres(membres []models.Membre) (int64, error) {
|
||||||
|
|
|
@ -36,13 +36,13 @@ services:
|
||||||
restart: 'unless-stopped'
|
restart: 'unless-stopped'
|
||||||
command: ['bottin-agenda', '--config', '/etc/bottin-agenda/web.yaml', 'web']
|
command: ['bottin-agenda', '--config', '/etc/bottin-agenda/web.yaml', 'web']
|
||||||
|
|
||||||
# adminer:
|
adminer:
|
||||||
# image: adminer
|
image: adminer
|
||||||
# restart: always
|
restart: always
|
||||||
# ports:
|
ports:
|
||||||
# - 8088:8080
|
- 8088:8080
|
||||||
# depends_on:
|
depends_on:
|
||||||
# - db
|
- db
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
db-data:
|
db-data:
|
||||||
|
|
77
handlers/transaction.go
Normal file
77
handlers/transaction.go
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.agecem.com/agecem/bottin-agenda/data"
|
||||||
|
"git.agecem.com/agecem/bottin-agenda/models"
|
||||||
|
"git.agecem.com/agecem/bottin-agenda/responses"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PostTransactions(c echo.Context) error {
|
||||||
|
var statusCode int = http.StatusInternalServerError
|
||||||
|
var response responses.PostTransactionsResponse
|
||||||
|
|
||||||
|
var transactions []models.Transaction
|
||||||
|
|
||||||
|
if err := c.Bind(&transactions); err != nil {
|
||||||
|
statusCode = http.StatusBadRequest
|
||||||
|
response.Message = fmt.Sprintf("Error during c.Bind(): %s", err)
|
||||||
|
|
||||||
|
return c.JSON(statusCode, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := data.NewDataClientFromViper()
|
||||||
|
if err != nil {
|
||||||
|
response.Message = fmt.Sprintf("Error during data.NewDataClientFromViper(): %s", err)
|
||||||
|
|
||||||
|
return c.JSON(statusCode, response)
|
||||||
|
}
|
||||||
|
defer client.DB.Close()
|
||||||
|
|
||||||
|
if len(transactions) == 0 {
|
||||||
|
response.Message = fmt.Sprintf("Nothing to do")
|
||||||
|
statusCode = http.StatusOK
|
||||||
|
|
||||||
|
return c.JSON(statusCode, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for already-existing transactions
|
||||||
|
for _, transaction := range transactions {
|
||||||
|
transaction, err := client.GetTransaction(transaction.MembreID, transaction.IsPerpetual)
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() != "sql: no rows in result set" {
|
||||||
|
response.Message = fmt.Sprintf("Error during client.GetTransaction(): %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if transaction.ID != "" {
|
||||||
|
agendaType := "non-perpetual"
|
||||||
|
if transaction.IsPerpetual {
|
||||||
|
agendaType = "perpetual"
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Message = fmt.Sprintf("Membre %s already received %s", transaction.MembreID, agendaType)
|
||||||
|
|
||||||
|
statusCode = http.StatusBadRequest
|
||||||
|
|
||||||
|
return c.JSON(statusCode, response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := client.InsertTransactions(transactions)
|
||||||
|
if err != nil {
|
||||||
|
response.Message = fmt.Sprintf("Error during client.InsertTransactions(): %s", err)
|
||||||
|
|
||||||
|
return c.JSON(statusCode, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Data.RowsInserted = rows
|
||||||
|
|
||||||
|
statusCode = http.StatusCreated
|
||||||
|
response.Message = "Insert successful"
|
||||||
|
|
||||||
|
return c.JSON(statusCode, response)
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ import "time"
|
||||||
|
|
||||||
var Schema = `
|
var Schema = `
|
||||||
CREATE TABLE transactions (
|
CREATE TABLE transactions (
|
||||||
id PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
|
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
|
||||||
membre_id VARCHAR(7),
|
membre_id VARCHAR(7),
|
||||||
given_at TIMESTAMP,
|
given_at TIMESTAMP,
|
||||||
is_perpetual BOOLEAN
|
is_perpetual BOOLEAN
|
||||||
|
|
Loading…
Reference in a new issue