change table column name
This commit is contained in:
1 parent
ef56625321
commit
41471694d5
11 files changed
+318
-334
No files matched your search
@@ -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