debut ajout compte joint

This commit is contained in:
2022-05-13 01:38:03 +02:00
parent cc6aa27d5d
commit 19a642b4d4
14 changed files with 785 additions and 1 deletions

View File

@@ -0,0 +1,116 @@
package jointaccount
import (
"fmt"
"nos-comptes/internal/storage/dao"
"nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/utils"
"github.com/lib/pq"
)
type Database struct {
*postgresql.DatabasePostgreSQL
}
func (db *Database) GetAllJointaccountOfUser(id string) ([]*Jointaccount, error) {
q := `
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
FROM public.jointaccount a
WHERE a.user_id = $1
`
rows, err := db.Session.Query(q, id)
if err != nil {
return nil, err
}
defer rows.Close()
as := make([]*Jointaccount, 0)
for rows.Next() {
a := Jointaccount{}
err := rows.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
if err != nil {
return nil, err
}
as = append(as, &a)
}
return as, nil
}
func (db *Database) GetJointaccountWithNameForUser(name string, id string) (*Jointaccount, error) {
q := `
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
FROM public.jointaccount a
WHERE a.user_id = $1
AND a.name = $2
`
row, err := db.Session.Query(q, id, name)
if !row.Next() {
return nil, dao.NewDAOError(dao.ErrTypeNotFound, fmt.Errorf("No row found"))
}
a := Jointaccount{}
row.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
if row.Next() {
return nil, fmt.Errorf("Impossibru")
}
if errPq, ok := err.(*pq.Error); ok {
return nil, postgresql.HandlePgError(errPq)
}
if err != nil {
utils.GetLogger().Info(err)
return nil, err
}
return &a, nil
}
func (db *Database) CreateJointaccount(jointaccount *Jointaccount) error {
q := `
INSERT INTO public.jointaccount
(Name, Provider, user_id)
VALUES
($1, $2, $3)
RETURNING id, created_at
`
err := db.Session.
QueryRow(q, jointaccount.Name, jointaccount.Provider, jointaccount.UserId).
Scan(&jointaccount.ID, &jointaccount.CreatedAt)
if errPq, ok := err.(*pq.Error); ok {
return postgresql.HandlePgError(errPq)
}
return err
}
func (db *Database) DeleteJointaccountOfAnUser(userId, jointaccountId string) error {
query := `
DELETE FROM jointaccount
WHERE user_id = $1
AND id = $2;`
_, err := db.Session.Exec(query, userId, jointaccountId)
return err
}
func (db *Database) GetASpecificJointaccountForUser(userId, jointaccountId string) (*Jointaccount, error) {
q := `
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
FROM public.jointaccount a
WHERE a.user_id = $1
AND a.id = $2
`
row := db.Session.QueryRow(q, userId, jointaccountId)
a := Jointaccount{}
err := row.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
if errPq, ok := err.(*pq.Error); ok {
return nil, postgresql.HandlePgError(errPq)
}
if err != nil {
utils.GetLogger().Info(err)
return nil, err
}
return &a, nil
}
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {
return &Database{db}
}