103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
entity "git.ego.freeddns.org/egommerce/api-entities/identity/entity"
|
|
|
|
database "git.ego.freeddns.org/egommerce/go-api-pkg/client/postgresql"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type URLAccessRepository struct {
|
|
db *pgxpool.Pool
|
|
}
|
|
|
|
func NewURLAccessRepository(db *pgxpool.Pool) *URLAccessRepository {
|
|
return &URLAccessRepository{db}
|
|
}
|
|
|
|
func (r *URLAccessRepository) FindByID(id string) (*entity.URLAccess, error) {
|
|
var urlAccess entity.URLAccess
|
|
|
|
sql := `SELECT id, roles, url, service FROM identity.url_access WHERE id=$1 LIMIT 1`
|
|
err := r.db.QueryRow(context.Background(), sql, id).
|
|
Scan(&urlAccess.ID, &urlAccess.Roles, &urlAccess.URL, &urlAccess.Service)
|
|
if err != nil {
|
|
return nil, errors.New("failed to fetch url_access from DB: " + err.Error())
|
|
}
|
|
|
|
return &urlAccess, nil
|
|
}
|
|
|
|
func (r *URLAccessRepository) FindAll() ([]entity.URLAccess, error) {
|
|
sql := "SELECT id, roles, url, service FROM identity.url_access ORDER BY service"
|
|
rows, err := r.db.Query(context.Background(), sql)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []entity.URLAccess
|
|
for rows.Next() {
|
|
var url entity.URLAccess
|
|
if err := rows.Scan(&url.ID, &url.Roles, &url.URL, &url.Service); err != nil {
|
|
return nil, err
|
|
}
|
|
results = append(results, url)
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
func (r *URLAccessRepository) Create(role *entity.URLAccess) (string, error) {
|
|
var id string
|
|
|
|
return id, nil
|
|
}
|
|
|
|
func (r *URLAccessRepository) Update(role *entity.URLAccess) (entity.URLAccess, error) {
|
|
return entity.URLAccess{}, nil
|
|
}
|
|
|
|
func (r *URLAccessRepository) Delete(id int64) (bool, error) {
|
|
return true, nil
|
|
}
|
|
|
|
func (r *URLAccessRepository) FindByURLAndService(url, service string) (*entity.URLAccess, error) {
|
|
var urlAccess entity.URLAccess
|
|
|
|
sql := `SELECT id, roles, url FROM identity.url_access WHERE url=$1 AND service=$2 LIMIT 1`
|
|
err := r.db.QueryRow(context.Background(), sql, url, service).
|
|
Scan(&urlAccess.ID, &urlAccess.Roles, &urlAccess.URL)
|
|
if err != nil {
|
|
return nil, errors.New("failed to fetch url_access from DB: " + err.Error())
|
|
}
|
|
|
|
return &urlAccess, nil
|
|
}
|
|
|
|
func (r *URLAccessRepository) FindByURLAndServiceForRole(url, service, role string) (*entity.URLAccess, error) {
|
|
var entity entity.URLAccess
|
|
|
|
sql := fmt.Sprintf("SELECT id, roles, url, service FROM identity.url_access WHERE url=$1 AND service=$2 AND roles::jsonb @> '[\"%s\"]'::jsonb LIMIT 1", role)
|
|
err := r.db.QueryRow(context.Background(), sql, url, service).
|
|
Scan(&entity.ID, &entity.Roles, &entity.URL, &entity.Service)
|
|
if err != nil {
|
|
if err = database.NoRowsInQuerySet(err); err != nil {
|
|
return nil, errors.New("no url found for: " + url + " and role: " + role)
|
|
}
|
|
|
|
return nil, errors.New("failed to fetch url_access from DB: " + err.Error())
|
|
}
|
|
|
|
return &entity, nil
|
|
}
|
|
|
|
func (r *URLAccessRepository) FindForUser(user *entity.User) {
|
|
|
|
}
|