update methods
This commit is contained in:
parent
6d12c55785
commit
71d13bb217
5 changed files with 277 additions and 59 deletions
|
|
@ -3,6 +3,7 @@ package merch
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"strings"
|
||||
|
|
@ -14,10 +15,15 @@ type Repository interface {
|
|||
createMerch(ctx context.Context, merch *Merch, extra []ExtraData) error
|
||||
|
||||
// getMany returns list of only main merch record, without origins extra data
|
||||
getMany(ctx context.Context, userId string) ([]merchDTO, error)
|
||||
getMany(ctx context.Context, userId int64) ([]merchDTO, error)
|
||||
|
||||
getMerchIdByUuid(ctx context.Context, userId int64, uuid string) (int64, error)
|
||||
|
||||
updateMerch(ctx context.Context, userId int64, merch *updateMerchDTO) (*merchDTO, error)
|
||||
updateExtraData(ctx context.Context, merchId int64, insertData []ExtraData) ([]ExtraData, error)
|
||||
|
||||
// deleteOneMerchRecord sets deleted_at in merch + extra tables
|
||||
deleteOneMerchRecord(ctx context.Context, userId, merchUuid string, delTime time.Time) error
|
||||
deleteOneMerchRecord(ctx context.Context, userId int64, merchUuid string, delTime time.Time) error
|
||||
|
||||
Origins
|
||||
}
|
||||
|
|
@ -135,7 +141,7 @@ func (r *repo) createMerch(ctx context.Context, merch *Merch, extra []ExtraData)
|
|||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *repo) getMany(ctx context.Context, userId string) ([]merchDTO, error) {
|
||||
func (r *repo) getMany(ctx context.Context, userId int64) ([]merchDTO, error) {
|
||||
q := `SELECT created_at, updated_at, merch_uuid, name FROM merch WHERE deleted_at IS NULL AND user_id = $1`
|
||||
|
||||
rows, err := r.db.Query(ctx, q, userId)
|
||||
|
|
@ -158,7 +164,22 @@ func (r *repo) getMany(ctx context.Context, userId string) ([]merchDTO, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
func (r *repo) deleteOneMerchRecord(ctx context.Context, userId, merchUuid string, delTime time.Time) error {
|
||||
func (r *repo) getMerchIdByUuid(ctx context.Context, userId int64, uuid string) (int64, error) {
|
||||
q := `SELECT id FROM merch WHERE deleted_at IS NULL AND merch_uuid = $1 AND user_id = $2`
|
||||
|
||||
var merchId int64
|
||||
if err := r.db.QueryRow(ctx, q, uuid, userId).Scan(&merchId); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, nil
|
||||
} else {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return merchId, nil
|
||||
}
|
||||
|
||||
func (r *repo) deleteOneMerchRecord(ctx context.Context, userId int64, merchUuid string, delTime time.Time) error {
|
||||
tx, err := r.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -178,3 +199,79 @@ func (r *repo) deleteOneMerchRecord(ctx context.Context, userId, merchUuid strin
|
|||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *repo) updateMerch(ctx context.Context, userId int64, merch *updateMerchDTO) (*merchDTO, error) {
|
||||
q := `UPDATE merch SET name = $1 WHERE merch_uuid = $2 AND user_id = $3
|
||||
RETURNING created_at, updated_at, merch_uuid, name`
|
||||
|
||||
var result merchDTO
|
||||
if err := r.db.
|
||||
QueryRow(ctx, q, merch.Name, merch.MerchUuid, userId).
|
||||
Scan(&result.CreatedAt, &result.UpdatedAt, &result.MerchUuid, &result.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
//q := `
|
||||
// UPDATE merch_extra_data AS med
|
||||
// SET url = COALESCE(NULLIF(src.new_url, ''), med.url)
|
||||
// FROM UNNEST(
|
||||
// $1::text[],
|
||||
// $2::bigint[]
|
||||
// ) AS src(new_url, origin_id)
|
||||
// WHERE med.merch_id = $3
|
||||
// AND med.origin_id = src.origin_id
|
||||
// RETURNING med.created_at, med.updated_at, med.merch_id, med.origin_id, med.url
|
||||
// `
|
||||
|
||||
func (r *repo) updateExtraData(ctx context.Context, merchId int64, insertData []ExtraData) ([]ExtraData, error) {
|
||||
q := `
|
||||
INSERT INTO merch_extra_data (merch_id, origin_id, url, created_at, updated_at)
|
||||
SELECT $1, src.origin_id, src.new_url, src.created_at, src.updated_at
|
||||
FROM UNNEST(
|
||||
$2::text[],
|
||||
$3::bigint[],
|
||||
$4::timestamptz[],
|
||||
$5::timestamptz[])
|
||||
AS src(new_url, origin_id, created_at, updated_at)
|
||||
ON CONFLICT (merch_id, origin_id) DO UPDATE SET url = COALESCE(NULLIF(EXCLUDED.url, ''), merch_extra_data.url), updated_at = NOW()
|
||||
RETURNING created_at, updated_at, merch_id, origin_id, url;
|
||||
`
|
||||
|
||||
var (
|
||||
urls []string
|
||||
origins []int64
|
||||
createdAt []time.Time
|
||||
updatedAt []sql.NullTime
|
||||
)
|
||||
|
||||
for _, data := range insertData {
|
||||
urls = append(urls, data.URL)
|
||||
origins = append(origins, data.OriginId)
|
||||
createdAt = append(createdAt, data.CreatedAt)
|
||||
updatedAt = append(updatedAt, data.UpdatedAt)
|
||||
}
|
||||
|
||||
rows, err := r.db.Query(ctx, q, merchId, urls, origins, createdAt, updatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []ExtraData
|
||||
for rows.Next() {
|
||||
var m ExtraData
|
||||
if err = rows.Scan(&m.CreatedAt, &m.UpdatedAt, &m.MerchId, &m.OriginId, &m.URL); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, m)
|
||||
}
|
||||
|
||||
rows.Close() //must be before rows.Err check!
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue