-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete_job_by_etherum_address.go
56 lines (43 loc) · 1.33 KB
/
delete_job_by_etherum_address.go
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
package commands
import (
"context"
"database/sql"
"fmt"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/exceptions"
"github.com/pkg/errors"
"github.com/DIMO-Network/shared/db"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/db/models"
)
type DeleteJobByEtherumAddressCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewDeleteJobByEtherumAddressCommandHandler(dbs func() *db.ReaderWriter) DeleteJobByEtherumAddressCommandHandler {
return DeleteJobByEtherumAddressCommandHandler{DBS: dbs}
}
type DeleteJobCommandRequest struct {
ID string
}
type DeleteJobCommandResponse struct {
Status bool
}
func (h DeleteJobByEtherumAddressCommandHandler) Execute(ctx context.Context, command *DeleteJobCommandRequest) (*DeleteJobCommandResponse, error) {
dbc, err := models.Jobs(models.JobWhere.ID.EQ(command.ID)).One(ctx, h.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{
Err: err,
}
}
if errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("could not find job id: %s", command.ID),
}
}
}
if _, err := dbc.Delete(ctx, h.DBS().Writer.DB); err != nil {
return nil, &exceptions.InternalError{
Err: err,
}
}
return &DeleteJobCommandResponse{Status: true}, nil
}