changed column name
This commit is contained in:
@@ -23,7 +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.status`
|
||||
const cardColumns = `c.id, c.price, c.title, c.content, c.active`
|
||||
|
||||
const cardReturningColumns = `id, price, title, content, active`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter paymentcard.ListFilter) (paymentcard.ListPaymentCardDTO, error) {
|
||||
fromClause, whereClause, args := buildListQuery(filter)
|
||||
@@ -75,9 +77,9 @@ func buildListQuery(filter paymentcard.ListFilter) (fromClause, whereClause stri
|
||||
)`, n))
|
||||
}
|
||||
|
||||
if filter.Status != nil {
|
||||
args = append(args, *filter.Status)
|
||||
conditions = append(conditions, fmt.Sprintf("c.status = $%d", len(args)))
|
||||
if filter.Active != nil {
|
||||
args = append(args, *filter.Active)
|
||||
conditions = append(conditions, fmt.Sprintf("c.active = $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
@@ -109,17 +111,17 @@ func (r *repository) Create(ctx context.Context, dto paymentcard.CreatePaymentCa
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
|
||||
status := true
|
||||
if dto.Status != nil {
|
||||
status = *dto.Status
|
||||
active := true
|
||||
if dto.Active != nil {
|
||||
active = *dto.Active
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO payment_cards (price, title, content, status)
|
||||
INSERT INTO payment_cards (price, title, content, active)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING %s`, cardColumns)
|
||||
RETURNING %s`, cardReturningColumns)
|
||||
|
||||
return scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, title, content, status))
|
||||
return scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, title, content, active))
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id int64, dto paymentcard.UpdatePaymentCardDTO) (paymentcard.PaymentCardDTO, error) {
|
||||
@@ -130,24 +132,24 @@ func (r *repository) Update(ctx context.Context, id int64, dto paymentcard.Updat
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE payment_cards SET
|
||||
price = $1, title = $2, content = $3, status = $4
|
||||
price = $1, title = $2, content = $3, active = $4
|
||||
WHERE id = $5
|
||||
RETURNING %s`, cardColumns)
|
||||
RETURNING %s`, cardReturningColumns)
|
||||
|
||||
item, err := scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, title, content, dto.Status, id))
|
||||
item, err := scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, title, content, dto.Active, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) UpdateStatus(ctx context.Context, id int64, status bool) (paymentcard.PaymentCardDTO, error) {
|
||||
func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (paymentcard.PaymentCardDTO, error) {
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE payment_cards SET status = $1
|
||||
UPDATE payment_cards SET active = $1
|
||||
WHERE id = $2
|
||||
RETURNING %s`, cardColumns)
|
||||
RETURNING %s`, cardReturningColumns)
|
||||
|
||||
item, err := scanPaymentCard(r.client.QueryRow(ctx, query, status, id))
|
||||
item, err := scanPaymentCard(r.client.QueryRow(ctx, query, active, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
@@ -186,7 +188,7 @@ func scanPaymentCard(row scannable) (paymentcard.PaymentCardDTO, error) {
|
||||
var titleJSON, contentJSON []byte
|
||||
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.Price, &titleJSON, &contentJSON, &item.Status,
|
||||
&item.Id, &item.Price, &titleJSON, &contentJSON, &item.Active,
|
||||
); err != nil {
|
||||
return paymentcard.PaymentCardDTO{}, err
|
||||
}
|
||||
|
||||
@@ -11,30 +11,30 @@ type PaymentCardDTO struct {
|
||||
Price float64 `json:"price"`
|
||||
Title LocalizedText `json:"title"`
|
||||
Content LocalizedText `json:"content"`
|
||||
Status bool `json:"status"`
|
||||
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"`
|
||||
Status *bool `json:"status"`
|
||||
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"`
|
||||
Status bool `json:"status"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdatePaymentCardStatusDTO struct {
|
||||
Status bool `json:"status"`
|
||||
type UpdatePaymentCardActiveDTO struct {
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
CardName string
|
||||
Status *bool
|
||||
Active *bool
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group.GET("/:id", h.getByID)
|
||||
group.POST("", h.create)
|
||||
group.PUT("/:id", h.update)
|
||||
group.PUT("/:id/status", h.updateStatus)
|
||||
group.PUT("/:id/active", h.updateActive)
|
||||
group.DELETE("/:id", h.delete)
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param card_name query string false "Filter by card title (en, ru, tm)"
|
||||
// @Param status query bool false "Filter by status"
|
||||
// @Param active query bool false "Filter by active"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
@@ -169,40 +169,40 @@ func (h *handler) update(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// updateStatus godoc
|
||||
// @Summary Update payment card status
|
||||
// updateActive godoc
|
||||
// @Summary Update payment card active
|
||||
// @Description Enables or disables a payment card
|
||||
// @Tags payment-card
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Payment card ID"
|
||||
// @Param body body UpdatePaymentCardStatusDTO true "Status payload"
|
||||
// @Param body body UpdatePaymentCardActiveDTO true "Active payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /payment-cards/{id}/status [put]
|
||||
func (h *handler) updateStatus(c *gin.Context) {
|
||||
// @Router /payment-cards/{id}/active [put]
|
||||
func (h *handler) updateActive(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdatePaymentCardStatusDTO
|
||||
var dto UpdatePaymentCardActiveDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.UpdateStatus(c.Request.Context(), id, dto.Status)
|
||||
item, err := h.repository.UpdateActive(c.Request.Context(), id, dto.Active)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
h.logger.Errorf("update payment card status failed: %v", err)
|
||||
h.logger.Errorf("update payment card active failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
@@ -281,12 +281,12 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.CardName = raw
|
||||
}
|
||||
|
||||
if raw := c.Query("status"); raw != "" {
|
||||
status, err := strconv.ParseBool(raw)
|
||||
if raw := c.Query("active"); raw != "" {
|
||||
active, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Status = &status
|
||||
filter.Active = &active
|
||||
}
|
||||
|
||||
return filter, true
|
||||
|
||||
@@ -7,6 +7,6 @@ type Repository interface {
|
||||
GetByID(ctx context.Context, id int64) (PaymentCardDTO, error)
|
||||
Create(ctx context.Context, dto CreatePaymentCardDTO) (PaymentCardDTO, error)
|
||||
Update(ctx context.Context, id int64, dto UpdatePaymentCardDTO) (PaymentCardDTO, error)
|
||||
UpdateStatus(ctx context.Context, id int64, status bool) (PaymentCardDTO, error)
|
||||
UpdateActive(ctx context.Context, id int64, active bool) (PaymentCardDTO, error)
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user