Skip to content

Commit ca25348

Browse files
committed
fix: some update sample revisions
1 parent 60abf62 commit ca25348

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

internal/api/update_sample.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ func (i UpdateSampleInput) Validate() error {
4545
}
4646

4747
func (h *handler) UpdateSample(c *gin.Context) {
48-
49-
sampleID, err := strconv.Atoi(c.Param("id"))
5048
var input UpdateSampleInput
5149

50+
sampleID, err := strconv.Atoi(c.Param("id"))
5251
if err != nil {
5352
c.JSON(400, M{
5453
"message": "invalid sample id",

internal/backend/update_sample.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import (
44
"context"
55
"errors"
66
"io"
7-
"strconv"
87

9-
"github.com/gin-gonic/gin"
108
"github.com/tunema-org/sound-function/internal/jwt"
9+
"github.com/tunema-org/sound-function/internal/repository"
1110
"github.com/tunema-org/sound-function/model"
1211
)
1312

@@ -23,13 +22,7 @@ type UpdateSampleParams struct {
2322
TagIDs []int
2423
}
2524

26-
27-
func (b *Backend) UpdateSample(ctx context.Context, accessToken string, sampleID params UpdateSampleParams) (int, error) {
28-
sampleID, err := strconv.Atoi(c.Param("id"))
29-
if err != nil {
30-
return 0, err
31-
}
32-
25+
func (b *Backend) UpdateSample(ctx context.Context, accessToken string, sampleID int, params UpdateSampleParams) (int, error) {
3326
_, claims, err := jwt.Verify(accessToken, b.cfg.JWTSecretKey)
3427
if err != nil {
3528
return 0, err
@@ -40,15 +33,17 @@ func (b *Backend) UpdateSample(ctx context.Context, accessToken string, sampleID
4033
return 0, errors.New("invalid claims")
4134
}
4235

43-
//msih error
44-
err := b.repo.UpdateSample(ctx, int(sampleID), model.Sample{
45-
UserID: int(userID),
46-
Name: params.Name,
47-
BPM: params.BPM,
48-
Key: params.Key,
49-
KeyScale: params.KeyScale,
50-
Time: params.Time,
51-
Price: params.Price,
36+
err = b.repo.UpdateSample(ctx, sampleID, repository.UpdateSampleParams{
37+
Sample: model.Sample{
38+
UserID: int(userID),
39+
Name: params.Name,
40+
BPM: params.BPM,
41+
Key: params.Key,
42+
KeyScale: params.KeyScale,
43+
Time: params.Time,
44+
Price: params.Price,
45+
},
46+
TagIDs: params.TagIDs,
5247
})
5348
if err != nil {
5449
return 0, err

internal/repository/update_sample.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,36 @@ package repository
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/jackc/pgx/v5"
87
"github.com/tunema-org/sound-function/model"
98
)
109

1110
type UpdateSampleParams struct {
1211
Sample model.Sample
12+
TagIDs []int
1313
}
1414

15-
func (r *Repository) UpdateSample(ctx context.Context, params UpdateSampleParams) (int, error) {
15+
func (r *Repository) UpdateSample(ctx context.Context, sampleID int, params UpdateSampleParams) error {
1616
tx, err := r.db.BeginTx(ctx, pgx.TxOptions{})
1717
if err != nil {
18-
return 0, err
18+
return err
1919
}
2020

2121
updateSampleQuery := `UPDATE samples SET name = $1, bpm = $2, key = $4, key_scale = $5, time = $6, price = $7 WHERE id = $8`
2222

23-
var SampleID int
24-
err = tx.QueryRow(ctx, updateSampleQuery,
23+
_, err = tx.Exec(ctx, updateSampleQuery,
2524
params.Sample.Name,
2625
params.Sample.BPM,
2726
params.Sample.Key,
2827
params.Sample.KeyScale,
2928
params.Sample.Time,
3029
params.Sample.Price,
31-
).Scan(&SampleID)
30+
)
3231
if err != nil {
3332
tx.Rollback(ctx)
34-
return 0, err
33+
return err
3534
}
3635

37-
fmt.Println("Sample Updated")
38-
39-
return 0, err
36+
return tx.Commit(ctx)
4037
}

0 commit comments

Comments
 (0)