Files
online-backend/internal/admin/auth/db/postgresql.go
GuwandykA 77bc4025d3 Add branch_user table and branch-user assignment endpoints
Introduces a branch_user join table linking branches to users, plus
create/delete API endpoints under the auth module for managing branch
assignments. The create response includes the branch's localized name
via a join. Regenerated swagger docs to include the new endpoints.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-08 15:02:05 +05:00

95 lines
2.6 KiB
Go
Executable File

package db
import (
"context"
"encoding/json"
"online-back/internal/admin/auth"
"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) auth.Repository {
return &repository{
client: client,
logger: logger,
}
}
const (
loginQuery = `SELECT id, password FROM users WHERE email = $1`
userByIDQuery = `SELECT id, password FROM users WHERE id = $1`
profileQuery = `SELECT id, fullname, phone_number, avatar, created_at
FROM users WHERE id = $1`
)
func (r *repository) Login(ctx context.Context, dto auth.LoginDTO) (auth.UserCredentials, error) {
var result auth.UserCredentials
err := r.client.QueryRow(ctx, loginQuery, dto.Email).Scan(&result.Id, &result.Password)
return result, err
}
func (r *repository) GetUserByID(ctx context.Context, id int) (auth.UserCredentials, error) {
var result auth.UserCredentials
err := r.client.QueryRow(ctx, userByIDQuery, id).Scan(&result.Id, &result.Password)
return result, err
}
func (r *repository) GetProfile(ctx context.Context, id int) (auth.UserProfileDTO, error) {
var profile auth.UserProfileDTO
err := r.client.QueryRow(ctx, profileQuery, id).Scan(
&profile.Id,
&profile.FullName,
&profile.PhoneNumber,
&profile.Avatar,
&profile.CreatedAt,
)
return profile, err
}
const (
createBranchUserQuery = `
WITH inserted AS (
INSERT INTO branch_user (branch_id, user_id)
VALUES ($1, $2)
RETURNING id, branch_id, user_id, created_at, updated_at
)
SELECT inserted.id, inserted.branch_id, b.name, inserted.user_id, inserted.created_at, inserted.updated_at
FROM inserted
JOIN branches b ON b.id = inserted.branch_id`
deleteBranchUserQuery = `DELETE FROM branch_user WHERE id = $1`
)
func (r *repository) CreateBranchUser(ctx context.Context, dto auth.CreateBranchUserDTO) (auth.BranchUserDTO, error) {
var item auth.BranchUserDTO
var branchNameJSON []byte
err := r.client.QueryRow(ctx, createBranchUserQuery, dto.BranchId, dto.UserId).Scan(
&item.Id, &item.BranchId, &branchNameJSON, &item.UserId, &item.CreatedAt, &item.UpdatedAt,
)
if err != nil {
return auth.BranchUserDTO{}, err
}
if err := json.Unmarshal(branchNameJSON, &item.BranchName); err != nil {
return auth.BranchUserDTO{}, err
}
return item, nil
}
func (r *repository) DeleteBranchUser(ctx context.Context, id int64) error {
tag, err := r.client.Exec(ctx, deleteBranchUserQuery, id)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return pgx.ErrNoRows
}
return nil
}