feat: implement async modpack import with websockets
This commit is contained in:
58
internal/database/job_repository.go
Normal file
58
internal/database/job_repository.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/models"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type JobRepository struct {
|
||||
DB *pgxpool.Pool
|
||||
}
|
||||
|
||||
// CreateJob создает новую задачу со статусом pending
|
||||
func (r *JobRepository) CreateJob(ctx context.Context) (int, error) {
|
||||
var jobID int
|
||||
query := `INSERT INTO modpack_import_jobs (status, progress, created_at, updated_at)
|
||||
VALUES ($1, $2, NOW(), NOW()) RETURNING id`
|
||||
|
||||
err := r.DB.QueryRow(ctx, query, models.JobStatusPending, 0).Scan(&jobID)
|
||||
return jobID, err
|
||||
}
|
||||
|
||||
// UpdateJobStatus обновляет статус и прогресс задачи
|
||||
func (r *JobRepository) UpdateJobStatus(ctx context.Context, jobID int, status models.ImportJobStatus, progress int, errorMessage string) error {
|
||||
query := `UPDATE modpack_import_jobs
|
||||
SET status = $1, progress = $2, error_message = $3, updated_at = NOW()
|
||||
WHERE id = $4`
|
||||
|
||||
_, err := r.DB.Exec(ctx, query, status, progress, errorMessage, jobID)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetJob получает задачу по ID
|
||||
func (r *JobRepository) GetJob(ctx context.Context, jobID int) (*models.ImportJob, error) {
|
||||
job := &models.ImportJob{}
|
||||
query := `SELECT id, status, progress, error_message, created_at, updated_at
|
||||
FROM modpack_import_jobs WHERE id = $1`
|
||||
|
||||
var errMsg *string // Для обработки NULL
|
||||
err := r.DB.QueryRow(ctx, query, jobID).Scan(
|
||||
&job.ID, &job.Status, &job.Progress, &errMsg, &job.CreatedAt, &job.UpdatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil // Или спец ошибка
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if errMsg != nil {
|
||||
job.ErrorMessage = *errMsg
|
||||
}
|
||||
|
||||
return job, nil
|
||||
}
|
||||
Reference in New Issue
Block a user