fix(account): add account creante and fix incorrect field for getting all
This commit is contained in:
@@ -2,6 +2,8 @@ package account
|
||||
|
||||
import (
|
||||
"nos-comptes/internal/storage/dao/postgresql"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
@@ -12,7 +14,7 @@ func (db *Database) GetAllAccountOfUser(id string) ([]*Account, error) {
|
||||
q := `
|
||||
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
|
||||
FROM public.account a
|
||||
WHERE a.id = $1
|
||||
WHERE a.user_id = $1
|
||||
`
|
||||
rows, err := db.Session.Query(q, id)
|
||||
if err != nil {
|
||||
@@ -23,7 +25,7 @@ func (db *Database) GetAllAccountOfUser(id string) ([]*Account, error) {
|
||||
as := make([]*Account, 0)
|
||||
for rows.Next() {
|
||||
a := Account{}
|
||||
err := rows.Scan(&a.ID, &a.userId, &a.name, &a.provider, &a.CreatedAt, &a.UpdatedAt)
|
||||
err := rows.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -32,6 +34,42 @@ func (db *Database) GetAllAccountOfUser(id string) ([]*Account, error) {
|
||||
return as, nil
|
||||
}
|
||||
|
||||
func (db *Database) GetAccountWithNameForUser(name string, id string) (*Account, error) {
|
||||
q := `
|
||||
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
|
||||
FROM public.account a
|
||||
WHERE a.user_id = $1
|
||||
AND a.name = $2
|
||||
`
|
||||
row := db.Session.QueryRow(q, id, name)
|
||||
|
||||
a := Account{}
|
||||
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)
|
||||
}
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (db *Database) CreateAccount(account *Account) error {
|
||||
q := `
|
||||
INSERT INTO public.account
|
||||
(Name, Provider, user_id)
|
||||
VALUES
|
||||
($1, $2, $3)
|
||||
RETURNING id, created_at
|
||||
`
|
||||
|
||||
err := db.Session.
|
||||
QueryRow(q, account.Name, account.Provider, account.UserId).
|
||||
Scan(&account.ID, &account.CreatedAt)
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return postgresql.HandlePgError(errPq)
|
||||
}
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {
|
||||
return &Database{db}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user