wip
This commit is contained in:
121
mangezmieux-backend/internal/acl/sql/sql_resource.go
Normal file
121
mangezmieux-backend/internal/acl/sql/sql_resource.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"github.com/lib/pq"
|
||||
"mangezmieux-backend/internal/acl/model"
|
||||
model2 "mangezmieux-backend/internal/model"
|
||||
"mangezmieux-backend/internal/postgres"
|
||||
)
|
||||
|
||||
func (sqlDAO dao) AddResource(resourceName string, metadata model2.Metadata) (*model.Resource, error) {
|
||||
var Id uuid.UUID
|
||||
q := `
|
||||
INSERT INTO mangezmieux.resource
|
||||
(name, creation_date, creation_user)
|
||||
VALUES
|
||||
($1, $2, $3)
|
||||
RETURNING
|
||||
Id`
|
||||
|
||||
err := sqlDAO.client.QueryRow(q, resourceName, metadata.CreationDate, metadata.CreationUser).Scan(&Id)
|
||||
|
||||
var errPq *pq.Error
|
||||
if errors.As(err, &errPq) {
|
||||
return nil, postgres.HandlePgError(errPq)
|
||||
}
|
||||
|
||||
resource, err := sqlDAO.GetResource(Id)
|
||||
if errors.As(err, &errPq) {
|
||||
return nil, postgres.HandlePgError(errPq)
|
||||
}
|
||||
|
||||
return resource, nil
|
||||
}
|
||||
|
||||
func (sqlDAO dao) DeleteResource(Id uuid.UUID) error {
|
||||
q := `
|
||||
DELETE FROM mangezmieux.resource
|
||||
WHERE Id = $1
|
||||
`
|
||||
|
||||
_, err := sqlDAO.client.Exec(q, Id.String())
|
||||
var errPq *pq.Error
|
||||
if errors.As(err, &errPq) {
|
||||
return postgres.HandlePgError(errPq)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (sqlDAO dao) GetResource(Id uuid.UUID) (*model.Resource, error) {
|
||||
q := `
|
||||
SELECT Id, name, creation_date, last_update_date
|
||||
FROM mangezmieux.resource r
|
||||
WHERE r.Id = $1
|
||||
`
|
||||
row := sqlDAO.client.QueryRow(q, Id.String())
|
||||
|
||||
resource := &model.Resource{}
|
||||
err := row.Scan(&resource.Id, &resource.Name, &resource.CreationDate, &resource.LastUpdateDate)
|
||||
var errPq *pq.Error
|
||||
if errors.As(err, &errPq) {
|
||||
return nil, postgres.HandlePgError(errPq)
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, postgres.NewDAOError(postgres.ErrTypeNotFound, err)
|
||||
}
|
||||
return resource, nil
|
||||
}
|
||||
|
||||
func (sqlDAO dao) GetResourceByName(name string) (*model.Resource, error) {
|
||||
q := `
|
||||
SELECT Id, name, creation_date, last_update_date
|
||||
FROM mangezmieux.resource r
|
||||
WHERE r.name = $1
|
||||
`
|
||||
row := sqlDAO.client.QueryRow(q, name)
|
||||
|
||||
resource := &model.Resource{}
|
||||
err := row.Scan(&resource.Id, &resource.Name, &resource.CreationDate, &resource.LastUpdateDate)
|
||||
var errPq *pq.Error
|
||||
if errors.As(err, &errPq) {
|
||||
return nil, postgres.HandlePgError(errPq)
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, postgres.NewDAOError(postgres.ErrTypeNotFound, err)
|
||||
}
|
||||
return resource, nil
|
||||
}
|
||||
|
||||
func (sqlDAO dao) GetAllResource() ([]*model.Resource, error) {
|
||||
q := `
|
||||
SELECT Id, name, creation_date, last_update_date
|
||||
FROM mangezmieux.resource r
|
||||
`
|
||||
rows, err := sqlDAO.client.Query(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rows.Err() != nil {
|
||||
return nil, rows.Err()
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
resources := make([]*model.Resource, 0)
|
||||
for rows.Next() {
|
||||
resource := &model.Resource{}
|
||||
err := rows.Scan(&resource.Id, &resource.Name, &resource.CreationDate, &resource.LastUpdateDate)
|
||||
var errPq *pq.Error
|
||||
if errors.As(err, &errPq) {
|
||||
return nil, postgres.HandlePgError(errPq)
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, postgres.NewDAOError(postgres.ErrTypeNotFound, err)
|
||||
}
|
||||
resources = append(resources, resource)
|
||||
}
|
||||
return resources, nil
|
||||
}
|
||||
Reference in New Issue
Block a user