Brijesh's Git Server — aegis @ main

Successor to whodis

internal/database/ops_developer.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
package database

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	"github.com/google/uuid"

	_ "github.com/jackc/pgx/v5/stdlib"
)

func (s *service) developerEmailExists(ctx context.Context, email string) (bool, error) {
	query := `SELECT 1 FROM developers WHERE email = $1`
	row := s.db.QueryRowContext(ctx, query, email)

	var exists int
	if err := row.Scan(&exists); err != nil {
		if err == sql.ErrNoRows {
			return false, nil
		}
		return false, fmt.Errorf("failed to check if email exists: %v", err)
	}

	return true, nil
}

func (s *service) CreateDeveloper(name, email string) (string, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	exists, err := s.developerEmailExists(ctx, email)
	if err != nil {
		return "", fmt.Errorf("failed to check if email exists: %v", err)
	}
	if exists {
		return "", fmt.Errorf("email already in use")
	}

	id := uuid.New().String()
	query := `INSERT INTO developers (id, name, email) VALUES ($1, $2, $3)`

	_, err = s.db.ExecContext(ctx, query, id, name, email)
	if err != nil {
		return "", fmt.Errorf("failed to create developer: %v", err)
	}

	return id, nil
}

func (s *service) GetDeveloper(id string) (*Developer, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	query := `SELECT id, name, email, created_at FROM developers WHERE id = $1`
	row := s.db.QueryRowContext(ctx, query, id)

	var developer Developer
	if err := row.Scan(&developer.ID, &developer.Name, &developer.Email, &developer.CreatedAt); err != nil {
		return nil, fmt.Errorf("failed to get developer: %v", err)
	}

	return &developer, nil
}

func (s *service) UpdateDeveloper(id, name, email string) error {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	exists, err := s.developerEmailExists(ctx, email)
	if err != nil {
		return fmt.Errorf("failed to check if email exists: %v", err)
	}
	if exists {
		return fmt.Errorf("email already in use")
	}

	query := `UPDATE developers SET name = $2, email = $3 WHERE id = $1`
	_, err = s.db.ExecContext(ctx, query, id, name, email)
	if err != nil {
		return fmt.Errorf("failed to update developer: %v", err)
	}

	return nil
}

func (s *service) DeleteDeveloper(id string) error {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	query := `DELETE FROM developers WHERE id = $1`
	_, err := s.db.ExecContext(ctx, query, id)
	if err != nil {
		return fmt.Errorf("failed to delete developer: %v", err)
	}

	return nil
}