about summary refs log tree commit diff
path: root/src/payload-processor/processor/db.go
blob: 607f97d13644d4827b5a8c433f86656da84e6669 (plain) (blame)
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
package processor

import (
	"context"

	"github.com/Baitinq/fs-tracer-backend/lib"
	"github.com/jmoiron/sqlx"
)

//go:generate mockgen -source=$GOFILE -package=$GOPACKAGE -destination=mock_$GOFILE
type DB interface {
	InsertFiles(ctx context.Context, files []lib.File, user_id string) error
}

type DBImpl struct {
	db *sqlx.DB
}

var _ DB = (*DBImpl)(nil)

func NewDB(db *sqlx.DB) DB {
	return &DBImpl{db: db}
}

func (db DBImpl) InsertFiles(ctx context.Context, files []lib.File, user_id string) error {
	for _, file := range files {
		file.User_id = user_id
		_, err := db.db.NamedExecContext(ctx, "INSERT INTO public.file (user_id, absolute_path, contents, timestamp) VALUES (:user_id, :absolute_path, :contents, :timestamp)", file)
		return err
	}
	return nil
}