Inicial
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Staff struct {
|
||||
ID string `json:"id"`
|
||||
UserID *string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
Type string `json:"type"`
|
||||
HourlyRate float64 `json:"hourly_rate"`
|
||||
Active bool `json:"active"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func ListStaff(ctx context.Context, conn *pgxpool.Conn) ([]*Staff, error) {
|
||||
rows, err := conn.Query(ctx,
|
||||
`SELECT id, user_id, name, COALESCE(email,''), COALESCE(phone,''), type,
|
||||
COALESCE(hourly_rate,0), active, created_at
|
||||
FROM staff ORDER BY name`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("staff: list: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var list []*Staff
|
||||
for rows.Next() {
|
||||
var s Staff
|
||||
if err := rows.Scan(&s.ID, &s.UserID, &s.Name, &s.Email, &s.Phone,
|
||||
&s.Type, &s.HourlyRate, &s.Active, &s.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("staff: scan: %w", err)
|
||||
}
|
||||
list = append(list, &s)
|
||||
}
|
||||
return list, rows.Err()
|
||||
}
|
||||
|
||||
func GetStaffByID(ctx context.Context, conn *pgxpool.Conn, id string) (*Staff, error) {
|
||||
row := conn.QueryRow(ctx,
|
||||
`SELECT id, user_id, name, COALESCE(email,''), COALESCE(phone,''), type,
|
||||
COALESCE(hourly_rate,0), active, created_at
|
||||
FROM staff WHERE id = $1`, id)
|
||||
var s Staff
|
||||
err := row.Scan(&s.ID, &s.UserID, &s.Name, &s.Email, &s.Phone,
|
||||
&s.Type, &s.HourlyRate, &s.Active, &s.CreatedAt)
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("staff: get: %w", err)
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func CreateStaff(ctx context.Context, conn *pgxpool.Conn, name, email, phone, staffType string, hourlyRate float64) (*Staff, error) {
|
||||
row := conn.QueryRow(ctx,
|
||||
`INSERT INTO staff (name, email, phone, type, hourly_rate)
|
||||
VALUES ($1, NULLIF($2,''), NULLIF($3,''), $4, $5)
|
||||
RETURNING id, user_id, name, COALESCE(email,''), COALESCE(phone,''), type,
|
||||
COALESCE(hourly_rate,0), active, created_at`,
|
||||
name, email, phone, staffType, hourlyRate)
|
||||
var s Staff
|
||||
if err := row.Scan(&s.ID, &s.UserID, &s.Name, &s.Email, &s.Phone,
|
||||
&s.Type, &s.HourlyRate, &s.Active, &s.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("staff: create: %w", err)
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func UpdateStaff(ctx context.Context, conn *pgxpool.Conn, id, name, email, phone, staffType string, hourlyRate float64, active bool) (*Staff, error) {
|
||||
row := conn.QueryRow(ctx,
|
||||
`UPDATE staff SET name=$2, email=NULLIF($3,''), phone=NULLIF($4,''), type=$5,
|
||||
hourly_rate=$6, active=$7
|
||||
WHERE id=$1
|
||||
RETURNING id, user_id, name, COALESCE(email,''), COALESCE(phone,''), type,
|
||||
COALESCE(hourly_rate,0), active, created_at`,
|
||||
id, name, email, phone, staffType, hourlyRate, active)
|
||||
var s Staff
|
||||
err := row.Scan(&s.ID, &s.UserID, &s.Name, &s.Email, &s.Phone,
|
||||
&s.Type, &s.HourlyRate, &s.Active, &s.CreatedAt)
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("staff: update: %w", err)
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func DeleteStaff(ctx context.Context, conn *pgxpool.Conn, id string) error {
|
||||
tag, err := conn.Exec(ctx, `DELETE FROM staff WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("staff: delete: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return pgx.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user