fixed
This commit is contained in:
157
backend/internal/admin/static/db/postgresql.go
Normal file
157
backend/internal/admin/static/db/postgresql.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bankline/internal/admin/static"
|
||||
"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) static.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) (static.DataStaticDTO, error) {
|
||||
|
||||
ns := []static.StaticDTO{}
|
||||
var (
|
||||
dt static.DataStaticDTO
|
||||
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 static` + where + `;`
|
||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
||||
|
||||
q := ` select
|
||||
id, logo, title,
|
||||
content_tm, content_ru, content_en,
|
||||
created_at
|
||||
from static` +
|
||||
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 static.StaticDTO
|
||||
errN := rows.Scan(
|
||||
&n.Id, &n.Logo, &n.Title,
|
||||
&n.ContentTm, &n.ContentRu, &n.ContentEn,
|
||||
&n.CreatedAt,
|
||||
)
|
||||
if errN != nil {
|
||||
r.logger.Error("errN :::", errN)
|
||||
continue
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
dt.Statics = ns
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) GetData(ctx context.Context, id int) (static.StaticDTO, error) {
|
||||
|
||||
var (
|
||||
ns static.StaticDTO
|
||||
)
|
||||
|
||||
qC := `select id, logo, title, content_tm, content_ru, content_en, created_at from static where id = $1 ;`
|
||||
err := r.client.QueryRow(ctx, qC, id).Scan(
|
||||
&ns.Id, &ns.Logo, &ns.Title,
|
||||
&ns.ContentTm, &ns.ContentRu, &ns.ContentEn,
|
||||
&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 static.AddStaticDTO) (static.StaticIdDTO, error) {
|
||||
|
||||
var (
|
||||
n static.StaticIdDTO
|
||||
)
|
||||
|
||||
q := `INSERT INTO static (logo, title, content_tm, content_ru, content_en) VALUES ($1, $2, $3, $4, $5) RETURNING id, logo, title;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.Logo, req.Title, req.ContentTm, req.ContentRu, req.ContentEn).Scan(
|
||||
&n.Id, &n.Logo, &n.Title,
|
||||
)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return n, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) UpdateData(ctx context.Context, req static.UpdateStaticDTO, id int) (static.StaticIdDTO, error) {
|
||||
var (
|
||||
q string
|
||||
n static.StaticIdDTO
|
||||
)
|
||||
|
||||
q = ` update static set logo = $1, title = $2, content_tm = $3, content_ru = $4, content_en = $5 where id = $6 RETURNING id, logo, title;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.Logo, req.Title, req.ContentTm, req.ContentRu, req.ContentEn, id).Scan(
|
||||
&n.Id, &n.Logo, &n.Title,
|
||||
)
|
||||
|
||||
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 static where id = $1 ;`
|
||||
_, err := r.client.Exec(ctx, q, id)
|
||||
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
55
backend/internal/admin/static/dto.go
Normal file
55
backend/internal/admin/static/dto.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package static
|
||||
|
||||
import "time"
|
||||
|
||||
type DataStaticDTO struct {
|
||||
Statics []StaticDTO `json:"statics"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Tm string `json:"tm"`
|
||||
Ru string `json:"ru"`
|
||||
En string `json:"en"`
|
||||
}
|
||||
|
||||
type StaticDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Logo string `json:"logo"`
|
||||
Title Language `json:"title"`
|
||||
ContentTm string `json:"contentTm"`
|
||||
ContentRu string `json:"contentRu"`
|
||||
ContentEn string `json:"contentEn"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type AddStaticDTO struct {
|
||||
Logo string `json:"logo" binding:"required"`
|
||||
Title Language `json:"title" binding:"required"`
|
||||
ContentTm string `json:"contentTm"`
|
||||
ContentRu string `json:"contentRu"`
|
||||
ContentEn string `json:"contentEn"`
|
||||
}
|
||||
|
||||
type UpdateStaticDTO struct {
|
||||
Logo string `json:"logo" binding:"required"`
|
||||
Title Language `json:"title" binding:"required"`
|
||||
ContentTm string `json:"contentTm"`
|
||||
ContentRu string `json:"contentRu"`
|
||||
ContentEn string `json:"contentEn"`
|
||||
}
|
||||
|
||||
type StaticIdDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Logo string `json:"logo"`
|
||||
Title Language `json:"title"`
|
||||
}
|
||||
|
||||
type PaginationDTO struct {
|
||||
Limit int `json:"limit"`
|
||||
Page int `json:"page"`
|
||||
}
|
||||
|
||||
type ReqIdDTO struct {
|
||||
Id int `json:"Id"`
|
||||
}
|
||||
206
backend/internal/admin/static/handler.go
Normal file
206
backend/internal/admin/static/handler.go
Normal file
@@ -0,0 +1,206 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"bankline/internal/appresult"
|
||||
"bankline/internal/handlers"
|
||||
"bankline/internal/listing"
|
||||
"bankline/pkg/logging"
|
||||
"context"
|
||||
"fmt"
|
||||
"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 statics
|
||||
// @Tags statics
|
||||
// @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} DataStaticDTO
|
||||
// @Router /admin/static [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 static
|
||||
// @Tags statics
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "static ID"
|
||||
// @Success 200 {object} StaticDTO
|
||||
// @Router /admin/static/{id} [get]
|
||||
func (h *handler) GetOne(c *gin.Context) {
|
||||
|
||||
staticId := c.Param("id")
|
||||
id, err := strconv.Atoi(staticId)
|
||||
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 statics
|
||||
// @Tags statics
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param statics body AddStaticDTO true "Create JSON"
|
||||
// @Success 200 {string} string
|
||||
// @Router /admin/static [post]
|
||||
func (h *handler) Create(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
reqDTO AddStaticDTO
|
||||
)
|
||||
|
||||
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 statics
|
||||
// @Tags statics
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "static ID"
|
||||
// @Param statics body UpdateStaticDTO true "Create JSON"
|
||||
// @Success 200 {object} StaticIdDTO
|
||||
// @Router /admin/static/{id} [put]
|
||||
func (h *handler) Update(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
reqDTO UpdateStaticDTO
|
||||
res StaticIdDTO
|
||||
)
|
||||
|
||||
staticId := c.Param("id")
|
||||
id, err := strconv.Atoi(staticId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err = c.ShouldBindJSON(&reqDTO); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("uuid :::", id)
|
||||
if id != 0 {
|
||||
res, err = h.repository.UpdateData(context.Background(), reqDTO, id)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("err :::", err)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = res
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete godoc
|
||||
// @Description delete static
|
||||
// @Tags statics
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "static ID"
|
||||
// @Success 200 {string} string
|
||||
// @Router /admin/static/{id} [delete]
|
||||
func (h *handler) Delete(c *gin.Context) {
|
||||
|
||||
staticId := c.Param("id")
|
||||
id, err := strconv.Atoi(staticId)
|
||||
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
|
||||
}
|
||||
15
backend/internal/admin/static/storage.go
Normal file
15
backend/internal/admin/static/storage.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"bankline/internal/listing"
|
||||
"context"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetAllData(ctx context.Context, params listing.Params) (DataStaticDTO, error)
|
||||
GetData(ctx context.Context, id int) (StaticDTO, error)
|
||||
|
||||
AddData(ctx context.Context, dto AddStaticDTO) (StaticIdDTO, error)
|
||||
UpdateData(ctx context.Context, dto UpdateStaticDTO, id int) (StaticIdDTO, error)
|
||||
DeleteData(ctx context.Context, id int) error
|
||||
}
|
||||
Reference in New Issue
Block a user