Add db
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package download_request
|
||||
|
||||
import "downloader/internal/core/entities"
|
||||
import (
|
||||
"context"
|
||||
"downloader/internal/core/entities"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
Create(download *entities.Download) (*entities.Download, error)
|
||||
GetById(id string) (*entities.Download, error)
|
||||
Update(download *entities.Download) error
|
||||
Get(active bool) ([]*entities.Download, error)
|
||||
Create(ctx context.Context, download *entities.Download) (*entities.Download, error)
|
||||
GetById(ctx context.Context, id string) (*entities.Download, error)
|
||||
Update(ctx context.Context, download *entities.Download) error
|
||||
Get(ctx context.Context, active bool) ([]*entities.Download, error)
|
||||
}
|
||||
|
29
api/internal/core/ports/download_request/sql/download.go
Normal file
29
api/internal/core/ports/download_request/sql/download.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/uptrace/bun"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Download struct {
|
||||
bun.BaseModel `bun:"table:downloads,alias:d"`
|
||||
ID string `bun:"id,pk,type:uuid,default:uuid_generate_v4()"`
|
||||
Status string `bun:"status,notnull"`
|
||||
Link string `bun:"link,notnull"`
|
||||
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp"`
|
||||
UpdatedAt time.Time `bun:"updated_at,nullzero,notnull,default:current_timestamp"`
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*Download)(nil)
|
||||
|
||||
func (u *Download) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
u.CreatedAt = time.Now()
|
||||
u.UpdatedAt = time.Now()
|
||||
case *bun.UpdateQuery:
|
||||
u.UpdatedAt = time.Now()
|
||||
}
|
||||
return nil
|
||||
}
|
102
api/internal/core/ports/download_request/sql/repository.go
Normal file
102
api/internal/core/ports/download_request/sql/repository.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"downloader/internal/core/entities"
|
||||
"github.com/uptrace/bun"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
db *bun.DB
|
||||
logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func NewDownloadRequestSqlRepository(db *bun.DB, logger *zap.SugaredLogger) *repository {
|
||||
return &repository{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (r repository) Create(ctx context.Context, download *entities.Download) (*entities.Download, error) {
|
||||
insertDownload := &Download{
|
||||
ID: download.ID,
|
||||
Status: download.Status,
|
||||
Link: download.Link,
|
||||
}
|
||||
|
||||
_, err := r.db.NewInsert().
|
||||
Model(insertDownload).
|
||||
Returning("*").
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &entities.Download{
|
||||
ID: insertDownload.ID,
|
||||
Status: insertDownload.Status,
|
||||
Link: insertDownload.Link,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r repository) GetById(ctx context.Context, id string) (*entities.Download, error) {
|
||||
download := &Download{
|
||||
ID: id,
|
||||
}
|
||||
|
||||
err := r.db.NewSelect().
|
||||
Model(download).
|
||||
WherePK().
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &entities.Download{
|
||||
ID: download.ID,
|
||||
Status: download.Status,
|
||||
Link: download.Link,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r repository) Update(ctx context.Context, download *entities.Download) error {
|
||||
updateDownload := &Download{
|
||||
ID: download.ID,
|
||||
Status: download.Status,
|
||||
Link: download.Link,
|
||||
}
|
||||
|
||||
_, err := r.db.NewUpdate().
|
||||
Model(updateDownload).
|
||||
ExcludeColumn("created_at").
|
||||
WherePK().
|
||||
Exec(ctx)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (r repository) Get(ctx context.Context, active bool) ([]*entities.Download, error) {
|
||||
var downloads []Download
|
||||
err := r.db.NewSelect().
|
||||
Model(&downloads).
|
||||
Column("id", "status", "link").
|
||||
Limit(20).
|
||||
Order("created_at ASC").
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var responseDownloads []*entities.Download
|
||||
for _, download := range downloads {
|
||||
responseDownloads = append(responseDownloads, &entities.Download{
|
||||
ID: download.ID,
|
||||
Status: download.Status,
|
||||
Link: download.Link,
|
||||
})
|
||||
}
|
||||
|
||||
return responseDownloads, nil
|
||||
}
|
Reference in New Issue
Block a user