Files
noscomptes-back/internal/jointexpense/database.go

87 lines
2.3 KiB
Go

package jointexpense
import (
"nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/utils"
"github.com/lib/pq"
)
type Database struct {
*postgresql.DatabasePostgreSQL
}
func (db *Database) CreateJointexpense(jointexpense *Jointexpense) error {
q := `
INSERT INTO public.jointexpense
(jointaccount_id, value, type_jointexpense, jointexpense_date, libelle)
VALUES
($1, $2, $3, $4, $5)
RETURNING id, created_at
`
err := db.Session.
QueryRow(q, jointexpense.JointaccountId, jointexpense.Value, jointexpense.TypeJointexpense, jointexpense.JointexpenseDate, jointexpense.Libelle).
Scan(&jointexpense.ID, &jointexpense.CreatedAt)
if err != nil {
utils.GetLogger().Info(err)
}
if errPq, ok := err.(*pq.Error); ok {
return postgresql.HandlePgError(errPq)
}
return err
}
func (db Database) GetJointexpensesOfAnJointaccountBetween(id, from, to string) ([]*Jointexpense, error) {
q := `
SELECT a.id, a.jointaccount_id, a.value, a.type_jointexpense, a.jointexpense_date, a.created_at, a.updated_at, a.libelle
FROM public.jointexpense a
WHERE a.jointaccount_id = $1
AND a.jointexpense_date BETWEEN $2 and $3
`
rows, err := db.Session.Query(q, id, from, to)
if err != nil {
return nil, err
}
defer rows.Close()
es := make([]*Jointexpense, 0)
for rows.Next() {
e := Jointexpense{}
err := rows.Scan(&e.ID, &e.JointjointaccountId, &e.Value, &e.TypeJointexpense, &e.JointexpenseDate, &e.CreatedAt, &e.UpdatedAt, &e.Libelle)
if err != nil {
return nil, err
}
es = append(es, &e)
}
return es, nil
}
func (db Database) GetAllJointexpensesOfAnJointaccount(id string) ([]*Jointexpense, error) {
q := `
SELECT a.id, a.jointaccount_id, a.value, a.type_jointexpense, a.jointexpense_date, a.created_at, a.updated_at, a.libelle
FROM public.jointexpense a
WHERE a.jointaccount_id = $1
`
rows, err := db.Session.Query(q, id)
if err != nil {
return nil, err
}
defer rows.Close()
es := make([]*Jointexpense, 0)
for rows.Next() {
e := Jointexpense{}
err := rows.Scan(&e.ID, &e.JointaccountId, &e.Value, &e.TypeJointexpense, &e.JointexpenseDate, &e.CreatedAt, &e.UpdatedAt, &e.Libelle)
if err != nil {
return nil, err
}
es = append(es, &e)
}
return es, nil
}
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {
return &Database{db}
}