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
|
||||
|
||||
@@ -7,25 +7,25 @@ type LocalizedText struct {
|
||||
}
|
||||
|
||||
type PaymentCardDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Price float64 `json:"price"`
|
||||
Title LocalizedText `json:"title"`
|
||||
Content LocalizedText `json:"content"`
|
||||
Active bool `json:"active"`
|
||||
Id int64 `json:"id"`
|
||||
Price string `json:"price"`
|
||||
Title LocalizedText `json:"title"`
|
||||
Notes string `json:"notes"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type CreatePaymentCardDTO struct {
|
||||
Price float64 `json:"price" binding:"required"`
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
Content LocalizedText `json:"content" binding:"required"`
|
||||
Active *bool `json:"active"`
|
||||
Price string `json:"price" binding:"required"`
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
Notes string `json:"notes"`
|
||||
Active *bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdatePaymentCardDTO struct {
|
||||
Price float64 `json:"price" binding:"required"`
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
Content LocalizedText `json:"content" binding:"required"`
|
||||
Active bool `json:"active"`
|
||||
Price string `json:"price" binding:"required"`
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
Notes string `json:"notes"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdatePaymentCardActiveDTO struct {
|
||||
|
||||
@@ -23,9 +23,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) reasonscard
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
const reasonColumns = `r.id, r.title, r.price, r.content, r.created_at, r.updated_at`
|
||||
const reasonColumns = `r.id, r.price, r.name, r.notes, r.active, r.created_at, r.updated_at`
|
||||
|
||||
const reasonReturningColumns = `id, title, price, content, created_at, updated_at`
|
||||
const reasonReturningColumns = `id, price, name, notes, active, created_at, updated_at`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter reasonscard.ListFilter) (reasonscard.ListReasonCardDTO, error) {
|
||||
fromClause, whereClause, args := buildListQuery(filter)
|
||||
@@ -65,14 +65,14 @@ func (r *repository) List(ctx context.Context, filter reasonscard.ListFilter) (r
|
||||
}
|
||||
|
||||
func buildListQuery(filter reasonscard.ListFilter) (fromClause, whereClause string, args []interface{}) {
|
||||
fromClause = ` FROM reasons_cards r`
|
||||
fromClause = ` FROM card_states r`
|
||||
|
||||
var conditions []string
|
||||
if filter.ReasonName != "" {
|
||||
args = append(args, likePattern(filter.ReasonName))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
r.title->>'en' ILIKE $%[1]d OR r.title->>'ru' ILIKE $%[1]d OR r.title->>'tm' ILIKE $%[1]d
|
||||
r.name->>'en' ILIKE $%[1]d OR r.name->>'ru' ILIKE $%[1]d OR r.name->>'tm' ILIKE $%[1]d
|
||||
)`, n))
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func likePattern(term string) string {
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (reasonscard.ReasonCardDTO, error) {
|
||||
query := fmt.Sprintf(`SELECT %s FROM reasons_cards r WHERE r.id = $1`, reasonColumns)
|
||||
query := fmt.Sprintf(`SELECT %s FROM card_states r WHERE r.id = $1`, reasonColumns)
|
||||
item, err := scanReasonCard(r.client.QueryRow(ctx, query, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return reasonscard.ReasonCardDTO{}, err
|
||||
@@ -100,33 +100,38 @@ func (r *repository) GetByID(ctx context.Context, id int64) (reasonscard.ReasonC
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto reasonscard.CreateReasonCardDTO) (reasonscard.ReasonCardDTO, error) {
|
||||
title, content, err := marshalJSON(dto.Title, dto.Content)
|
||||
name, err := marshalName(dto.Title)
|
||||
if err != nil {
|
||||
return reasonscard.ReasonCardDTO{}, err
|
||||
}
|
||||
|
||||
active := true
|
||||
if dto.Active != nil {
|
||||
active = *dto.Active
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO reasons_cards (title, price, content)
|
||||
VALUES ($1, $2, $3)
|
||||
INSERT INTO card_states (price, name, notes, active)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING %s`, reasonReturningColumns)
|
||||
|
||||
return scanReasonCard(r.client.QueryRow(ctx, query, title, dto.Price, content))
|
||||
return scanReasonCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, active))
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id int64, dto reasonscard.UpdateReasonCardDTO) (reasonscard.ReasonCardDTO, error) {
|
||||
title, content, err := marshalJSON(dto.Title, dto.Content)
|
||||
name, err := marshalName(dto.Title)
|
||||
if err != nil {
|
||||
return reasonscard.ReasonCardDTO{}, err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE reasons_cards SET
|
||||
title = $1, price = $2, content = $3,
|
||||
UPDATE card_states SET
|
||||
price = $1, name = $2, notes = $3, active = $4,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $4
|
||||
WHERE id = $5
|
||||
RETURNING %s`, reasonReturningColumns)
|
||||
|
||||
item, err := scanReasonCard(r.client.QueryRow(ctx, query, title, dto.Price, content, id))
|
||||
item, err := scanReasonCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, dto.Active, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return reasonscard.ReasonCardDTO{}, err
|
||||
}
|
||||
@@ -134,7 +139,7 @@ func (r *repository) Update(ctx context.Context, id int64, dto reasonscard.Updat
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM reasons_cards WHERE id = $1`, id)
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM card_states WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -144,16 +149,8 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func marshalJSON(title, content reasonscard.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 reasonscard.LocalizedText) ([]byte, error) {
|
||||
return json.Marshal(title)
|
||||
}
|
||||
|
||||
type scannable interface {
|
||||
@@ -162,17 +159,14 @@ type scannable interface {
|
||||
|
||||
func scanReasonCard(row scannable) (reasonscard.ReasonCardDTO, error) {
|
||||
var item reasonscard.ReasonCardDTO
|
||||
var titleJSON, contentJSON []byte
|
||||
var nameJSON []byte
|
||||
|
||||
if err := row.Scan(
|
||||
&item.Id, &titleJSON, &item.Price, &contentJSON, &item.CreatedAt, &item.UpdatedAt,
|
||||
&item.Id, &item.Price, &nameJSON, &item.Notes, &item.Active, &item.CreatedAt, &item.UpdatedAt,
|
||||
); err != nil {
|
||||
return reasonscard.ReasonCardDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
|
||||
return reasonscard.ReasonCardDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(contentJSON, &item.Content); err != nil {
|
||||
if err := json.Unmarshal(nameJSON, &item.Title); err != nil {
|
||||
return reasonscard.ReasonCardDTO{}, err
|
||||
}
|
||||
return item, nil
|
||||
|
||||
@@ -11,22 +11,25 @@ type LocalizedText struct {
|
||||
type ReasonCardDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Title LocalizedText `json:"title"`
|
||||
Price float64 `json:"price"`
|
||||
Content LocalizedText `json:"content"`
|
||||
Price string `json:"price"`
|
||||
Notes string `json:"notes"`
|
||||
Active bool `json:"active"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type CreateReasonCardDTO struct {
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
Price float64 `json:"price" binding:"required"`
|
||||
Content LocalizedText `json:"content" binding:"required"`
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
Price string `json:"price" binding:"required"`
|
||||
Notes string `json:"notes"`
|
||||
Active *bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdateReasonCardDTO struct {
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
Price float64 `json:"price" binding:"required"`
|
||||
Content LocalizedText `json:"content" binding:"required"`
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
Price string `json:"price" binding:"required"`
|
||||
Notes string `json:"notes"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
|
||||
Reference in New Issue
Block a user