change table column name
This commit is contained in:
@@ -23,9 +23,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) paymentcard
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
const cardColumns = `c.id, c.price, c.title, c.content, c.active`
|
||||
const cardColumns = `c.id, c.price, c.name, c.notes, c.active`
|
||||
|
||||
const cardReturningColumns = `id, price, title, content, active`
|
||||
const cardReturningColumns = `id, price, name, notes, active`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter paymentcard.ListFilter) (paymentcard.ListPaymentCardDTO, error) {
|
||||
fromClause, whereClause, args := buildListQuery(filter)
|
||||
@@ -65,7 +65,7 @@ func (r *repository) List(ctx context.Context, filter paymentcard.ListFilter) (p
|
||||
}
|
||||
|
||||
func buildListQuery(filter paymentcard.ListFilter) (fromClause, whereClause string, args []interface{}) {
|
||||
fromClause = ` FROM payment_cards c`
|
||||
fromClause = ` FROM card_types c`
|
||||
|
||||
var conditions []string
|
||||
|
||||
@@ -73,7 +73,7 @@ func buildListQuery(filter paymentcard.ListFilter) (fromClause, whereClause stri
|
||||
args = append(args, likePattern(filter.CardName))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
c.title->>'en' ILIKE $%[1]d OR c.title->>'ru' ILIKE $%[1]d OR c.title->>'tm' ILIKE $%[1]d
|
||||
c.name->>'en' ILIKE $%[1]d OR c.name->>'ru' ILIKE $%[1]d OR c.name->>'tm' ILIKE $%[1]d
|
||||
)`, n))
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ func likePattern(term string) string {
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (paymentcard.PaymentCardDTO, error) {
|
||||
query := fmt.Sprintf(`SELECT %s FROM payment_cards c WHERE c.id = $1`, cardColumns)
|
||||
query := fmt.Sprintf(`SELECT %s FROM card_types c WHERE c.id = $1`, cardColumns)
|
||||
item, err := scanPaymentCard(r.client.QueryRow(ctx, query, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
@@ -106,7 +106,7 @@ func (r *repository) GetByID(ctx context.Context, id int64) (paymentcard.Payment
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto paymentcard.CreatePaymentCardDTO) (paymentcard.PaymentCardDTO, error) {
|
||||
title, content, err := marshalJSON(dto.Title, dto.Content)
|
||||
name, err := marshalName(dto.Title)
|
||||
if err != nil {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
@@ -117,26 +117,26 @@ func (r *repository) Create(ctx context.Context, dto paymentcard.CreatePaymentCa
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO payment_cards (price, title, content, active)
|
||||
INSERT INTO card_types (price, name, notes, active)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING %s`, cardReturningColumns)
|
||||
|
||||
return scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, title, content, active))
|
||||
return scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, active))
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id int64, dto paymentcard.UpdatePaymentCardDTO) (paymentcard.PaymentCardDTO, error) {
|
||||
title, content, err := marshalJSON(dto.Title, dto.Content)
|
||||
name, err := marshalName(dto.Title)
|
||||
if err != nil {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE payment_cards SET
|
||||
price = $1, title = $2, content = $3, active = $4
|
||||
UPDATE card_types SET
|
||||
price = $1, name = $2, notes = $3, active = $4
|
||||
WHERE id = $5
|
||||
RETURNING %s`, cardReturningColumns)
|
||||
|
||||
item, err := scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, title, content, dto.Active, id))
|
||||
item, err := scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, dto.Active, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
@@ -145,7 +145,7 @@ func (r *repository) Update(ctx context.Context, id int64, dto paymentcard.Updat
|
||||
|
||||
func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (paymentcard.PaymentCardDTO, error) {
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE payment_cards SET active = $1
|
||||
UPDATE card_types SET active = $1
|
||||
WHERE id = $2
|
||||
RETURNING %s`, cardReturningColumns)
|
||||
|
||||
@@ -157,7 +157,7 @@ func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (p
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM payment_cards WHERE id = $1`, id)
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM card_types WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -167,16 +167,8 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func marshalJSON(title, content paymentcard.LocalizedText) ([]byte, []byte, error) {
|
||||
titleJSON, err := json.Marshal(title)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
contentJSON, err := json.Marshal(content)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return titleJSON, contentJSON, nil
|
||||
func marshalName(title paymentcard.LocalizedText) ([]byte, error) {
|
||||
return json.Marshal(title)
|
||||
}
|
||||
|
||||
type scannable interface {
|
||||
@@ -185,17 +177,14 @@ type scannable interface {
|
||||
|
||||
func scanPaymentCard(row scannable) (paymentcard.PaymentCardDTO, error) {
|
||||
var item paymentcard.PaymentCardDTO
|
||||
var titleJSON, contentJSON []byte
|
||||
var nameJSON []byte
|
||||
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.Price, &titleJSON, &contentJSON, &item.Active,
|
||||
&item.Id, &item.Price, &nameJSON, &item.Notes, &item.Active,
|
||||
); err != nil {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(contentJSON, &item.Content); err != nil {
|
||||
if err := json.Unmarshal(nameJSON, &item.Title); err != nil {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
return item, nil
|
||||
|
||||
Reference in New Issue
Block a user