changed arhitecture
This commit is contained in:
132
internal/identity/roleusers/db/postgresql.go
Normal file
132
internal/identity/roleusers/db/postgresql.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/identity/roleusers"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client postgresql.Client
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) roleusers.Repository {
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
const roleUserFromClause = ` FROM role_user ru
|
||||
JOIN roles r ON r.id = ru.role_id
|
||||
JOIN users u ON u.id = ru.user_id`
|
||||
|
||||
const roleUserSelectColumns = `ru.id, ru.role_id, r.name, ru.user_id, u.name, ru.created_at, ru.updated_at`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter roleusers.ListFilter) (roleusers.ListRoleUserDTO, error) {
|
||||
whereClause, args := buildListQuery(filter)
|
||||
|
||||
var count int64
|
||||
if err := r.client.QueryRow(ctx, `SELECT COUNT(*)`+roleUserFromClause+whereClause, args...).Scan(&count); err != nil {
|
||||
return roleusers.ListRoleUserDTO{}, err
|
||||
}
|
||||
|
||||
query := `SELECT ` + roleUserSelectColumns + roleUserFromClause + whereClause + ` ORDER BY ru.id DESC`
|
||||
rows, err := r.client.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
return roleusers.ListRoleUserDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []roleusers.RoleUserDTO
|
||||
for rows.Next() {
|
||||
item, err := scanRoleUser(rows)
|
||||
if err != nil {
|
||||
return roleusers.ListRoleUserDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return roleusers.ListRoleUserDTO{}, err
|
||||
}
|
||||
|
||||
return roleusers.ListRoleUserDTO{Count: count, Items: items}, nil
|
||||
}
|
||||
|
||||
func buildListQuery(filter roleusers.ListFilter) (whereClause string, args []interface{}) {
|
||||
var conditions []string
|
||||
|
||||
if filter.UserId != nil {
|
||||
args = append(args, *filter.UserId)
|
||||
conditions = append(conditions, fmt.Sprintf("ru.user_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.RoleName != "" {
|
||||
args = append(args, likePattern(filter.RoleName))
|
||||
conditions = append(conditions, fmt.Sprintf("r.name ILIKE $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
return "", args
|
||||
}
|
||||
|
||||
return " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
func likePattern(term string) string {
|
||||
term = strings.ReplaceAll(term, `\`, `\\`)
|
||||
term = strings.ReplaceAll(term, `%`, `\%`)
|
||||
term = strings.ReplaceAll(term, `_`, `\_`)
|
||||
return "%" + term + "%"
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto roleusers.CreateRoleUserDTO) (roleusers.RoleUserDTO, error) {
|
||||
query := `
|
||||
WITH inserted AS (
|
||||
INSERT INTO role_user (role_id, user_id)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, role_id, user_id, created_at, updated_at
|
||||
)
|
||||
SELECT inserted.id, inserted.role_id, r.name, inserted.user_id, u.name, inserted.created_at, inserted.updated_at
|
||||
FROM inserted
|
||||
JOIN roles r ON r.id = inserted.role_id
|
||||
JOIN users u ON u.id = inserted.user_id`
|
||||
|
||||
row := r.client.QueryRow(ctx, query, dto.RoleId, dto.UserId)
|
||||
return scanRoleUser(row)
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM role_user WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return pgx.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type scannable interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
func scanRoleUser(row scannable) (roleusers.RoleUserDTO, error) {
|
||||
var item roleusers.RoleUserDTO
|
||||
|
||||
err := row.Scan(
|
||||
&item.Id, &item.RoleId, &item.RoleName, &item.UserId, &item.UserName, &item.CreatedAt, &item.UpdatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return roleusers.RoleUserDTO{}, err
|
||||
}
|
||||
if err != nil {
|
||||
return roleusers.RoleUserDTO{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
Reference in New Issue
Block a user