wip
This commit is contained in:
161
mangezmieux-backend/internal/acl/sql/sql_user_role.go
Normal file
161
mangezmieux-backend/internal/acl/sql/sql_user_role.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"mangezmieux-backend/internal/acl/model"
|
||||
model2 "mangezmieux-backend/internal/model"
|
||||
"mangezmieux-backend/internal/postgres"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
func (sqlDAO dao) GetUserRole(id uuid.UUID) (*model.UserRole, error) {
|
||||
q := `
|
||||
SELECT id, role_id, user_id, creation_date, last_update_date
|
||||
FROM mangezmieux.user_role r
|
||||
WHERE r.id = $1
|
||||
`
|
||||
row := sqlDAO.client.QueryRow(q, id.String())
|
||||
|
||||
userRole := &model.UserRole{}
|
||||
err := row.Scan(&userRole.Id, &userRole.RoleId, &userRole.UserId, &userRole.CreationDate, &userRole.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 userRole, nil
|
||||
}
|
||||
|
||||
func (sqlDAO dao) GetUserRoleByUserAndRole(userId, roleId uuid.UUID) (*model.UserRole, error) {
|
||||
q := `
|
||||
SELECT id, role_id, user_id, creation_date, last_update_date
|
||||
FROM mangezmieux.user_role r
|
||||
WHERE r.role_id = $1
|
||||
AND r.user_id = $2
|
||||
`
|
||||
row := sqlDAO.client.QueryRow(q, roleId.String(), userId.String())
|
||||
|
||||
userRole := &model.UserRole{}
|
||||
err := row.Scan(&userRole.Id, &userRole.RoleId, &userRole.UserId, &userRole.CreationDate, &userRole.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 userRole, nil
|
||||
}
|
||||
|
||||
func (sqlDAO dao) GetUserRoleByUser(userId uuid.UUID) ([]*model.UserRole, error) {
|
||||
q := `
|
||||
SELECT id, role_id, user_id, creation_date, last_update_date
|
||||
FROM mangezmieux.user_role r
|
||||
WHERE r.user_id = $1
|
||||
`
|
||||
rows, err := sqlDAO.client.Query(q, userId.String())
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return make([]*model.UserRole, 0), nil
|
||||
}
|
||||
var errPq *pq.Error
|
||||
if errors.As(err, &errPq) {
|
||||
|
||||
return nil, postgres.HandlePgError(errPq)
|
||||
}
|
||||
|
||||
if rows.Err() != nil {
|
||||
return nil, rows.Err()
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
userRoles := make([]*model.UserRole, 0)
|
||||
for rows.Next() {
|
||||
userRole := &model.UserRole{}
|
||||
err := rows.Scan(&userRole.Id, &userRole.RoleId, &userRole.UserId, &userRole.CreationDate, &userRole.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)
|
||||
}
|
||||
userRoles = append(userRoles, userRole)
|
||||
}
|
||||
return userRoles, nil
|
||||
}
|
||||
|
||||
func (sqlDAO dao) GetUserRoleByRole(role uuid.UUID) ([]*model.UserRole, error) {
|
||||
q := `
|
||||
SELECT id, role_id, user_id, creation_date, last_update_date
|
||||
FROM mangezmieux.user_role r
|
||||
WHERE r.role_id = $1
|
||||
`
|
||||
rows, err := sqlDAO.client.Query(q, role.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rows.Err() != nil {
|
||||
return nil, rows.Err()
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
userRoles := make([]*model.UserRole, 0)
|
||||
for rows.Next() {
|
||||
userRole := &model.UserRole{}
|
||||
err := rows.Scan(&userRole.Id, &userRole.RoleId, &userRole.UserId, &userRole.CreationDate, &userRole.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)
|
||||
}
|
||||
userRoles = append(userRoles, userRole)
|
||||
}
|
||||
return userRoles, nil
|
||||
}
|
||||
|
||||
func (sqlDAO dao) AddUserRole(userId, roleId uuid.UUID, metadata model2.Metadata) (*model.UserRole, error) {
|
||||
var Id uuid.UUID
|
||||
q := `
|
||||
INSERT INTO mangezmieux.user_role
|
||||
(user_id, role_id, creation_date, creation_user)
|
||||
VALUES
|
||||
($1,$2,$3,$4)
|
||||
RETURNING
|
||||
Id`
|
||||
|
||||
err := sqlDAO.client.QueryRow(q, userId.String(), roleId.String(), metadata.CreationDate, metadata.CreationUser).Scan(&Id)
|
||||
|
||||
var errPq *pq.Error
|
||||
if errors.As(err, &errPq) {
|
||||
return nil, postgres.HandlePgError(errPq)
|
||||
}
|
||||
|
||||
userRole, err := sqlDAO.GetUserRole(Id)
|
||||
if errors.As(err, &errPq) {
|
||||
return nil, postgres.HandlePgError(errPq)
|
||||
}
|
||||
|
||||
return userRole, nil
|
||||
}
|
||||
|
||||
func (sqlDAO dao) DeleteUserRole(id uuid.UUID) error {
|
||||
q := `
|
||||
DELETE FROM mangezmieux.user_role
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user