changed column name
This commit is contained in:
@@ -23,7 +23,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) loansdocume
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
const documentColumns = `d.id, d.title, d.content_en, d.content_ru, d.content_tm, d.status, d.created_at, d.updated_at`
|
||||
const documentColumns = `d.id, d.title, d.content_en, d.content_ru, d.content_tm, d.active, d.created_at, d.updated_at`
|
||||
|
||||
const documentReturningColumns = `id, title, content_en, content_ru, content_tm, active, created_at, updated_at`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter loansdocument.ListFilter) (loansdocument.ListLoanDocumentDTO, error) {
|
||||
fromClause, whereClause, args := buildListQuery(filter)
|
||||
@@ -75,9 +77,9 @@ func buildListQuery(filter loansdocument.ListFilter) (fromClause, whereClause st
|
||||
)`, n))
|
||||
}
|
||||
|
||||
if filter.Status != nil {
|
||||
args = append(args, *filter.Status)
|
||||
conditions = append(conditions, fmt.Sprintf("d.status = $%d", len(args)))
|
||||
if filter.Active != nil {
|
||||
args = append(args, *filter.Active)
|
||||
conditions = append(conditions, fmt.Sprintf("d.active = $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
@@ -109,17 +111,17 @@ func (r *repository) Create(ctx context.Context, dto loansdocument.CreateLoanDoc
|
||||
return loansdocument.LoanDocumentDTO{}, err
|
||||
}
|
||||
|
||||
status := true
|
||||
if dto.Status != nil {
|
||||
status = *dto.Status
|
||||
active := true
|
||||
if dto.Active != nil {
|
||||
active = *dto.Active
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO loan_required_documents (title, content_en, content_ru, content_tm, status)
|
||||
INSERT INTO loan_required_documents (title, content_en, content_ru, content_tm, active)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING %s`, documentColumns)
|
||||
RETURNING %s`, documentReturningColumns)
|
||||
|
||||
return scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, status))
|
||||
return scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, active))
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id int64, dto loansdocument.UpdateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) {
|
||||
@@ -130,27 +132,27 @@ func (r *repository) Update(ctx context.Context, id int64, dto loansdocument.Upd
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE loan_required_documents SET
|
||||
title = $1, content_en = $2, content_ru = $3, content_tm = $4, status = $5,
|
||||
title = $1, content_en = $2, content_ru = $3, content_tm = $4, active = $5,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $6
|
||||
RETURNING %s`, documentColumns)
|
||||
RETURNING %s`, documentReturningColumns)
|
||||
|
||||
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, dto.Status, id))
|
||||
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, dto.Active, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return loansdocument.LoanDocumentDTO{}, err
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) UpdateStatus(ctx context.Context, id int64, status bool) (loansdocument.LoanDocumentDTO, error) {
|
||||
func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (loansdocument.LoanDocumentDTO, error) {
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE loan_required_documents SET
|
||||
status = $1,
|
||||
active = $1,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $2
|
||||
RETURNING %s`, documentColumns)
|
||||
RETURNING %s`, documentReturningColumns)
|
||||
|
||||
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, status, id))
|
||||
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, active, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return loansdocument.LoanDocumentDTO{}, err
|
||||
}
|
||||
@@ -178,7 +180,7 @@ func scanLoanDocument(row scannable) (loansdocument.LoanDocumentDTO, error) {
|
||||
|
||||
if err := row.Scan(
|
||||
&item.Id, &titleJSON, &item.ContentEn, &item.ContentRu, &item.ContentTm,
|
||||
&item.Status, &item.CreatedAt, &item.UpdatedAt,
|
||||
&item.Active, &item.CreatedAt, &item.UpdatedAt,
|
||||
); err != nil {
|
||||
return loansdocument.LoanDocumentDTO{}, err
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ type LoanDocumentDTO struct {
|
||||
ContentEn string `json:"contentEn"`
|
||||
ContentRu string `json:"contentRu"`
|
||||
ContentTm string `json:"contentTm"`
|
||||
Status bool `json:"status"`
|
||||
Active bool `json:"active"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
@@ -24,7 +24,7 @@ type CreateLoanDocumentDTO struct {
|
||||
ContentEn string `json:"contentEn" binding:"required"`
|
||||
ContentRu string `json:"contentRu" binding:"required"`
|
||||
ContentTm string `json:"contentTm" binding:"required"`
|
||||
Status *bool `json:"status"`
|
||||
Active *bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdateLoanDocumentDTO struct {
|
||||
@@ -32,16 +32,16 @@ type UpdateLoanDocumentDTO struct {
|
||||
ContentEn string `json:"contentEn" binding:"required"`
|
||||
ContentRu string `json:"contentRu" binding:"required"`
|
||||
ContentTm string `json:"contentTm" binding:"required"`
|
||||
Status bool `json:"status"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdateLoanDocumentStatusDTO struct {
|
||||
Status bool `json:"status"`
|
||||
type UpdateLoanDocumentActiveDTO struct {
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
DocumentName 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 document_name query string false "Filter by 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 loan document status
|
||||
// updateActive godoc
|
||||
// @Summary Update loan document active
|
||||
// @Description Enables or disables a loan document
|
||||
// @Tags loan-document
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Loan document ID"
|
||||
// @Param body body UpdateLoanDocumentStatusDTO true "Status payload"
|
||||
// @Param body body UpdateLoanDocumentActiveDTO true "Active payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /loans-documents/{id}/status [put]
|
||||
func (h *handler) updateStatus(c *gin.Context) {
|
||||
// @Router /loans-documents/{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 UpdateLoanDocumentStatusDTO
|
||||
var dto UpdateLoanDocumentActiveDTO
|
||||
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 loan document status failed: %v", err)
|
||||
h.logger.Errorf("update loan document active failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
@@ -281,12 +281,12 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.DocumentName = 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) (LoanDocumentDTO, error)
|
||||
Create(ctx context.Context, dto CreateLoanDocumentDTO) (LoanDocumentDTO, error)
|
||||
Update(ctx context.Context, id int64, dto UpdateLoanDocumentDTO) (LoanDocumentDTO, error)
|
||||
UpdateStatus(ctx context.Context, id int64, status bool) (LoanDocumentDTO, error)
|
||||
UpdateActive(ctx context.Context, id int64, active bool) (LoanDocumentDTO, error)
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user