ch logika
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
guwandyk committed 2026-09-18 11:53:37 +05:00
1 parent e0d03730de
commit d3e188affe
28 files changed
+405 -656

No files matched your search

+18 -1
View File
@@ -12,6 +12,7 @@ import (
"online-back/pkg/logging"
"online-back/pkg/repository/genericrepo"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
@@ -125,7 +126,11 @@ func (r *repository) Create(ctx context.Context, dto currency.CreateCurrencyDTO)
LEFT JOIN users cu ON cu.id = c.created_by
LEFT JOIN users uu ON uu.id = c.updated_by`
return scanCurrencyWithUsers(r.client.QueryRow(ctx, query, from, to, value, dto.CreatedBy))
item, err := scanCurrencyWithUsers(r.client.QueryRow(ctx, query, from, to, value, dto.CreatedBy))
if isUniqueViolation(err) {
return currency.CurrencyDTO{}, currency.ErrDuplicate
}
return item, err
}
func (r *repository) Update(ctx context.Context, id int64, dto currency.UpdateCurrencyDTO) (currency.CurrencyDTO, error) {
@@ -148,6 +153,9 @@ func (r *repository) Update(ctx context.Context, id int64, dto currency.UpdateCu
if errors.Is(err, pgx.ErrNoRows) {
return currency.CurrencyDTO{}, err
}
if isUniqueViolation(err) {
return currency.CurrencyDTO{}, currency.ErrDuplicate
}
return item, err
}
@@ -155,6 +163,15 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
return r.generic.Delete(ctx, id)
}
// isUniqueViolation reports whether err is a Postgres unique-constraint
// violation (e.g. the currencies_from_to_idx partial unique index rejecting
// a duplicate active currency_from/currency_to pair), so callers can map it
// to a clean 409 instead of letting it fall through as a generic 500.
func isUniqueViolation(err error) bool {
var pgErr *pgconn.PgError
return errors.As(err, &pgErr) && pgErr.Code == "23505"
}
func normalizeRate(from, to string, value currency.RateValue) (string, string, string) {
return strings.ToUpper(strings.TrimSpace(from)),
strings.ToUpper(strings.TrimSpace(to)),
+10
View File
@@ -110,6 +110,7 @@ func (h *handler) getByID(c *gin.Context) {
// @Param body body CreateCurrencyDTO true "Currency rate payload"
// @Success 201 {object} CurrencyDTO
// @Failure 400 {object} appresult.AppError
// @Failure 409 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
// @Router /currencies [post]
func (h *handler) create(c *gin.Context) {
@@ -122,6 +123,10 @@ func (h *handler) create(c *gin.Context) {
dto.CreatedBy = currentUserID(c)
item, err := h.repository.Create(c.Request.Context(), dto)
if errors.Is(err, ErrDuplicate) {
c.JSON(http.StatusConflict, appresult.ErrDuplicate)
return
}
if err != nil {
h.logger.Errorf("create currency failed: %v", err)
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
@@ -143,6 +148,7 @@ func (h *handler) create(c *gin.Context) {
// @Success 200 {object} CurrencyDTO
// @Failure 400 {object} appresult.AppError
// @Failure 404 {object} appresult.AppError
// @Failure 409 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
// @Router /currencies/{id} [put]
func (h *handler) update(c *gin.Context) {
@@ -165,6 +171,10 @@ func (h *handler) update(c *gin.Context) {
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
return
}
if errors.Is(err, ErrDuplicate) {
c.JSON(http.StatusConflict, appresult.ErrDuplicate)
return
}
if err != nil {
h.logger.Errorf("update currency failed: %v", err)
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
+9 -1
View File
@@ -1,6 +1,14 @@
package currency
import "context"
import (
"context"
"errors"
)
// ErrDuplicate is returned by Repository.Create/Update when the
// currency_from/currency_to pair already exists among active (non-deleted)
// rows, so the handler can respond with a clean 409 instead of a raw 500.
var ErrDuplicate = errors.New("currency pair already exists")
type Repository interface {
List(ctx context.Context, filter ListFilter) (ListCurrencyDTO, error)