This commit is contained in:
guwandyk committed 2026-08-04 12:50:20 +05:00
1 parent 4194cef934
commit 363127fe9e
3350 files changed
+1284220 -1

No files matched your search

@@ -0,0 +1,167 @@
package db
import (
"bankline/internal/admin/currency"
"bankline/internal/listing"
"bankline/pkg/logging"
"context"
"fmt"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type repository struct {
client *pgxpool.Pool
logger *logging.Logger
}
func NewRepository(client *pgxpool.Pool, logger *logging.Logger) currency.Repository {
return &repository{
client: client,
logger: logger,
}
}
var sortColumns = map[string]string{
"id": "id",
"createdAt": "created_at",
}
func (r repository) GetAllData(ctx context.Context, params listing.Params) (currency.DataCurrencyDTO, error) {
ns := []currency.CurrencyDTO{}
var (
dt currency.DataCurrencyDTO
err error
rows pgx.Rows
)
where := ""
args := []interface{}{}
if params.Search != "" {
where = ` where title::text ILIKE $1 or content_tm ILIKE $1 or content_ru ILIKE $1 or content_en ILIKE $1 `
args = append(args, params.SearchPattern())
}
qC := `select count(*) from currency` + where + `;`
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
q := ` select
id, title,
content_tm, content_ru, content_en,
path, created_at
from currency` +
where +
params.OrderClause(sortColumns, "created_at DESC")
if params.Paginated() {
q += fmt.Sprintf("limit $%d offset $%d;", len(args)+1, len(args)+2)
args = append(args, params.Limit, params.Offset())
} else {
q += ";"
}
rows, err = r.client.Query(ctx, q, args...)
if err != nil {
return dt, nil
}
defer rows.Close()
for rows.Next() {
var n currency.CurrencyDTO
errN := rows.Scan(
&n.Id, &n.Title,
&n.ContentTm, &n.ContentRu, &n.ContentEn,
&n.Path, &n.CreatedAt,
)
if errN != nil {
r.logger.Error("errN :::", errN)
continue
}
ns = append(ns, n)
}
dt.Currencies = ns
return dt, nil
}
func (r repository) GetData(ctx context.Context, id int) (currency.CurrencyDTO, error) {
var (
ns currency.CurrencyDTO
)
qC := `select id, title, content_tm, content_ru, content_en, path, created_at from currency where id = $1 ;`
err := r.client.QueryRow(ctx, qC, id).Scan(
&ns.Id, &ns.Title,
&ns.ContentTm, &ns.ContentRu, &ns.ContentEn,
&ns.Path, &ns.CreatedAt,
)
if err != nil && err.Error() != "no rows in result set" {
return ns, err
}
return ns, nil
}
func (r repository) AddData(ctx context.Context, req currency.AddCurrencyDTO) (currency.CurrencyIdDTO, error) {
var (
n currency.CurrencyIdDTO
)
q := `INSERT INTO currency (title, content_tm, content_ru, content_en, path)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, title, path;`
err := r.client.QueryRow(ctx, q,
req.Title, req.ContentTm, req.ContentRu, req.ContentEn, req.Path,
).Scan(
&n.Id, &n.Title, &n.Path,
)
if err != nil && err.Error() != "no rows in result set" {
return n, err
}
return n, nil
}
func (r repository) UpdateData(ctx context.Context, req currency.UpdateCurrencyDTO, id int) (currency.CurrencyIdDTO, error) {
var (
q string
n currency.CurrencyIdDTO
)
q = ` update currency set
title = $1, content_tm = $2, content_ru = $3, content_en = $4, path = $5
where id = $6
RETURNING id, title, path;`
err := r.client.QueryRow(ctx, q,
req.Title, req.ContentTm, req.ContentRu, req.ContentEn, req.Path,
id,
).Scan(
&n.Id, &n.Title, &n.Path,
)
if err != nil && err.Error() != "no rows in result set" {
return n, err
}
return n, nil
}
func (r repository) DeleteData(ctx context.Context, id int) error {
var (
q string
)
q = ` delete from currency where id = $1 ;`
_, err := r.client.Exec(ctx, q, id)
if err != nil && err.Error() != "no rows in result set" {
return err
}
return nil
}
+46
View File
@@ -0,0 +1,46 @@
package currency
import "time"
type DataCurrencyDTO struct {
Currencies []CurrencyDTO `json:"currencies"`
Count int `json:"count"`
}
type Language struct {
Tm string `json:"tm"`
Ru string `json:"ru"`
En string `json:"en"`
}
type CurrencyDTO struct {
Id int `json:"Id"`
Title Language `json:"title"`
ContentTm string `json:"contentTm"`
ContentRu string `json:"contentRu"`
ContentEn string `json:"contentEn"`
Path string `json:"path"`
CreatedAt time.Time `json:"createdAt"`
}
type AddCurrencyDTO struct {
Title Language `json:"title" binding:"required"`
ContentTm string `json:"contentTm"`
ContentRu string `json:"contentRu"`
ContentEn string `json:"contentEn"`
Path string `json:"path" binding:"required"`
}
type UpdateCurrencyDTO struct {
Title Language `json:"title" binding:"required"`
ContentTm string `json:"contentTm"`
ContentRu string `json:"contentRu"`
ContentEn string `json:"contentEn"`
Path string `json:"path" binding:"required"`
}
type CurrencyIdDTO struct {
Id int `json:"Id"`
Title Language `json:"title"`
Path string `json:"path"`
}
+203
View File
@@ -0,0 +1,203 @@
package currency
import (
"bankline/internal/appresult"
"bankline/internal/handlers"
"bankline/internal/listing"
"bankline/pkg/logging"
"context"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
const (
getAllURL = ""
getOneURL = "/:id"
)
type handler struct {
repository Repository
logger *logging.Logger
}
func NewHandler(repository Repository, logger *logging.Logger) handlers.Handler {
return &handler{
repository: repository,
logger: logger,
}
}
func (h *handler) Register(router *gin.RouterGroup) {
router.GET(getAllURL, h.GetAll)
router.GET(getOneURL, h.GetOne)
router.POST(getAllURL, h.Create)
router.PUT(getOneURL, h.Update)
router.DELETE(getOneURL, h.Delete)
}
// GetAll godoc
// @Description all data currencies
// @Tags currencies
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param limit query int false "limit"
// @Param page query int false "page"
// @Param search query string false "search term"
// @Param sort query string false "sort column (id, createdAt)"
// @Param order query string false "sort order (asc, desc)"
// @Success 200 {object} DataCurrencyDTO
// @Router /admin/currency [get]
func (h *handler) GetAll(c *gin.Context) {
result, err := h.repository.GetAllData(context.Background(), listing.FromQuery(c))
if err != nil {
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
successResult := appresult.Success
successResult.Data = result
c.JSON(http.StatusOK, successResult)
return
}
// GetOne godoc
// @Description one data currency
// @Tags currencies
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "currency ID"
// @Success 200 {object} CurrencyDTO
// @Router /admin/currency/{id} [get]
func (h *handler) GetOne(c *gin.Context) {
currencyId := c.Param("id")
id, err := strconv.Atoi(currencyId)
if err != nil {
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
return
}
result, err := h.repository.GetData(context.Background(), id)
if err != nil {
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
successResult := appresult.Success
successResult.Data = result
c.JSON(http.StatusOK, successResult)
return
}
// Create godoc
// @Description create currency
// @Tags currencies
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param currency body AddCurrencyDTO true "Create JSON"
// @Success 200 {string} string
// @Router /admin/currency [post]
func (h *handler) Create(c *gin.Context) {
var (
err error
reqDTO AddCurrencyDTO
)
if err = c.ShouldBindJSON(&reqDTO); err != nil {
c.JSON(http.StatusNotFound, appresult.ErrMissingParam)
return
}
res, err := h.repository.AddData(context.Background(), reqDTO)
if err != nil {
c.JSON(http.StatusInternalServerError, appresult.ErrNotAcceptable)
return
}
successResult := appresult.Success
successResult.Data = res
c.JSON(http.StatusOK, successResult)
return
}
// Update godoc
// @Description update currency
// @Tags currencies
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "currency ID"
// @Param currency body UpdateCurrencyDTO true "Create JSON"
// @Success 200 {object} CurrencyIdDTO
// @Router /admin/currency/{id} [put]
func (h *handler) Update(c *gin.Context) {
var (
err error
reqDTO UpdateCurrencyDTO
res CurrencyIdDTO
)
currencyId := c.Param("id")
id, err := strconv.Atoi(currencyId)
if err != nil {
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
return
}
if err = c.ShouldBindJSON(&reqDTO); err != nil {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
if id != 0 {
res, err = h.repository.UpdateData(context.Background(), reqDTO, id)
}
if err != nil {
return
}
successResult := appresult.Success
successResult.Data = res
c.JSON(http.StatusOK, successResult)
return
}
// Delete godoc
// @Description delete currency
// @Tags currencies
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "currency ID"
// @Success 200 {string} string
// @Router /admin/currency/{id} [delete]
func (h *handler) Delete(c *gin.Context) {
currencyId := c.Param("id")
id, err := strconv.Atoi(currencyId)
if err != nil {
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
return
}
err = h.repository.DeleteData(context.Background(), id)
if err != nil {
c.JSON(http.StatusInternalServerError, appresult.ErrMissingParam)
return
}
successResult := appresult.Success
successResult.Data = ""
c.JSON(http.StatusOK, successResult)
return
}
@@ -0,0 +1,15 @@
package currency
import (
"bankline/internal/listing"
"context"
)
type Repository interface {
GetAllData(ctx context.Context, params listing.Params) (DataCurrencyDTO, error)
GetData(ctx context.Context, id int) (CurrencyDTO, error)
AddData(ctx context.Context, dto AddCurrencyDTO) (CurrencyIdDTO, error)
UpdateData(ctx context.Context, dto UpdateCurrencyDTO, id int) (CurrencyIdDTO, error)
DeleteData(ctx context.Context, id int) error
}