Fix mini-slider admin CRUD JSON encoding and drop seed data from the migration.
Encode title as JSON text so create/list work with pgx, and keep the table migration permission-free so rows are added from admin. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
82b1e903f6
commit
262292651a
7 files changed
+74
-73
No files matched your search
@@ -5,16 +5,3 @@ CREATE TABLE IF NOT EXISTS mini_slider (
|
|||||||
"updated_at" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
"updated_at" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||||
"created_at" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
|
"created_at" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO mini_slider (title, img) VALUES
|
|
||||||
($s${"tm":"SYÝAHATÇYLYK","ru":"SYÝAHATÇYLYK","en":"SYÝAHATÇYLYK"}$s$, '/public/images/1698258445637883064.jpg'),
|
|
||||||
($s${"tm":"SENAGAT WE KOMMUNIKASIÝA","ru":"SENAGAT WE KOMMUNIKASIÝA","en":"SENAGAT WE KOMMUNIKASIÝA"}$s$, '/public/images/1698258484927760952.jpg'),
|
|
||||||
($s${"tm":"\"TURKMENDEMIRÝOLLARY\" AGENTLIGI","ru":"\"TURKMENDEMIRÝOLLARY\" AGENTLIGI","en":"\"TURKMENDEMIRÝOLLARY\" AGENTLIGI"}$s$, '/public/images/1698258510867643551.jpg'),
|
|
||||||
($s${"tm":"HIMIÝA SENAGATY","ru":"ХИМИЧЕСКАЯ ИНДУСТРИЯ","en":"HIMIÝA SENAGATY"}$s$, '/public/images/1698258537593043443.jpg'),
|
|
||||||
($s${"tm":"NEBITGAZ TOPLUMY","ru":"NEBITGAZ TOPLUMY","en":"NEBITGAZ TOPLUMY"}$s$, '/public/images/1698258570610558785.jpg'),
|
|
||||||
($s${"tm":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ","ru":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ","en":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ"}$s$, '/public/images/1698258644610696117.jpg'),
|
|
||||||
($s${"tm":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ","ru":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ","en":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ"}$s$, '/public/images/1698258687538830052.jpg'),
|
|
||||||
($s${"tm":"TASLAMALARYMYZ","ru":"TASLAMALARYMYZ","en":"TASLAMALARYMYZ"}$s$, '/public/images/1698258714855608782.jpg'),
|
|
||||||
($s${"tm":"TASLAMALARYMYZ","ru":"TASLAMALARYMYZ","en":"TASLAMALARYMYZ"}$s$, '/public/images/1698258772361834684.jpg'),
|
|
||||||
($s${"tm":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ","ru":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ","en":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ"}$s$, '/public/images/1698258794225139568.jpg'),
|
|
||||||
($s${"tm":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ","ru":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ","en":"ÖNÜMÇILIK KARZLARY BOÝUNÇA TASLAMALARYMYZ"}$s$, '/public/images/1698258818582436600.jpg');
|
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"bankline/internal/listing"
|
"bankline/internal/listing"
|
||||||
"bankline/pkg/logging"
|
"bankline/pkg/logging"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v4"
|
"github.com/jackc/pgx/v4"
|
||||||
@@ -28,6 +29,28 @@ var sortColumns = map[string]string{
|
|||||||
"createdAt": "created_at",
|
"createdAt": "created_at",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func encodeTitle(title mini_slider.Language) (string, error) {
|
||||||
|
b, err := json.Marshal(title)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func scanRow(scan func(dest ...interface{}) error) (mini_slider.GetMiniSliderDTO, error) {
|
||||||
|
var n mini_slider.GetMiniSliderDTO
|
||||||
|
var titleRaw []byte
|
||||||
|
if err := scan(&n.Id, &titleRaw, &n.Image, &n.CreatedAt); err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
if len(titleRaw) > 0 {
|
||||||
|
if err := json.Unmarshal(titleRaw, &n.Title); err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r repository) GetAllData(ctx context.Context, params listing.Params) (mini_slider.DataMiniSliderDTO, error) {
|
func (r repository) GetAllData(ctx context.Context, params listing.Params) (mini_slider.DataMiniSliderDTO, error) {
|
||||||
ns := []mini_slider.MiniSliderListItemDTO{}
|
ns := []mini_slider.MiniSliderListItemDTO{}
|
||||||
var (
|
var (
|
||||||
@@ -44,7 +67,10 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (mini
|
|||||||
}
|
}
|
||||||
|
|
||||||
qC := `select count(*) from mini_slider` + where + `;`
|
qC := `select count(*) from mini_slider` + where + `;`
|
||||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
if err = r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count); err != nil {
|
||||||
|
r.logger.Error("count mini slider :::", err)
|
||||||
|
return dt, err
|
||||||
|
}
|
||||||
|
|
||||||
q := ` select
|
q := ` select
|
||||||
id, title,
|
id, title,
|
||||||
@@ -54,62 +80,64 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (mini
|
|||||||
params.OrderClause(sortColumns, "created_at DESC")
|
params.OrderClause(sortColumns, "created_at DESC")
|
||||||
|
|
||||||
if params.Paginated() {
|
if params.Paginated() {
|
||||||
q += fmt.Sprintf("limit $%d offset $%d;", len(args)+1, len(args)+2)
|
q += fmt.Sprintf("LIMIT $%d OFFSET $%d;", len(args)+1, len(args)+2)
|
||||||
args = append(args, params.Limit, params.Offset())
|
args = append(args, params.Limit, params.Offset())
|
||||||
} else {
|
} else {
|
||||||
q += ";"
|
q += ";"
|
||||||
}
|
}
|
||||||
rows, err = r.client.Query(ctx, q, args...)
|
rows, err = r.client.Query(ctx, q, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
r.logger.Error("get all mini slider :::", err)
|
||||||
return dt, err
|
return dt, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var n mini_slider.MiniSliderListItemDTO
|
n, errN := scanRow(rows.Scan)
|
||||||
errN := rows.Scan(
|
|
||||||
&n.Id, &n.Title,
|
|
||||||
&n.Image, &n.CreatedAt,
|
|
||||||
)
|
|
||||||
if errN != nil {
|
if errN != nil {
|
||||||
r.logger.Error("errN :::", errN)
|
r.logger.Error("errN :::", errN)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
ns = append(ns, mini_slider.MiniSliderListItemDTO{
|
||||||
ns = append(ns, n)
|
Id: n.Id,
|
||||||
|
Title: n.Title,
|
||||||
|
Image: n.Image,
|
||||||
|
CreatedAt: n.CreatedAt,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
dt.MiniSliders = ns
|
dt.MiniSliders = ns
|
||||||
return dt, nil
|
return dt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r repository) GetOneData(ctx context.Context, id int) (mini_slider.GetMiniSliderDTO, error) {
|
func (r repository) GetOneData(ctx context.Context, id int) (mini_slider.GetMiniSliderDTO, error) {
|
||||||
var n mini_slider.GetMiniSliderDTO
|
|
||||||
|
|
||||||
q := ` select
|
q := ` select
|
||||||
id, title,
|
id, title,
|
||||||
img, created_at
|
img, created_at
|
||||||
from mini_slider where id = $1 ; `
|
from mini_slider where id = $1 ; `
|
||||||
|
|
||||||
err := r.client.QueryRow(ctx, q, id).Scan(
|
n, err := scanRow(func(dest ...interface{}) error {
|
||||||
&n.Id, &n.Title,
|
return r.client.QueryRow(ctx, q, id).Scan(dest...)
|
||||||
&n.Image, &n.CreatedAt,
|
})
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
r.logger.Error("get one mini slider :::", err)
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r repository) AddData(ctx context.Context, req mini_slider.AddMiniSliderDTO) (mini_slider.GetMiniSliderDTO, error) {
|
func (r repository) AddData(ctx context.Context, req mini_slider.AddMiniSliderDTO) (mini_slider.GetMiniSliderDTO, error) {
|
||||||
var n mini_slider.GetMiniSliderDTO
|
title, err := encodeTitle(req.Title)
|
||||||
|
if err != nil {
|
||||||
|
return mini_slider.GetMiniSliderDTO{}, err
|
||||||
|
}
|
||||||
|
|
||||||
q := `INSERT INTO mini_slider (title, img) VALUES ($1, $2) RETURNING id, title, img, created_at;`
|
q := `INSERT INTO mini_slider (title, img) VALUES ($1::json, $2) RETURNING id, title, img, created_at;`
|
||||||
|
|
||||||
err := r.client.QueryRow(ctx, q, req.Title, req.Image).Scan(
|
n, err := scanRow(func(dest ...interface{}) error {
|
||||||
&n.Id, &n.Title,
|
return r.client.QueryRow(ctx, q, title, req.Image).Scan(dest...)
|
||||||
&n.Image, &n.CreatedAt,
|
})
|
||||||
)
|
|
||||||
if err != nil && err.Error() != "no rows in result set" {
|
if err != nil && err.Error() != "no rows in result set" {
|
||||||
|
r.logger.Error("create mini slider :::", err)
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,15 +145,18 @@ func (r repository) AddData(ctx context.Context, req mini_slider.AddMiniSliderDT
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r repository) UpdateData(ctx context.Context, req mini_slider.UpdateMiniSliderDTO, id int) (mini_slider.GetMiniSliderDTO, error) {
|
func (r repository) UpdateData(ctx context.Context, req mini_slider.UpdateMiniSliderDTO, id int) (mini_slider.GetMiniSliderDTO, error) {
|
||||||
var n mini_slider.GetMiniSliderDTO
|
title, err := encodeTitle(req.Title)
|
||||||
|
if err != nil {
|
||||||
|
return mini_slider.GetMiniSliderDTO{}, err
|
||||||
|
}
|
||||||
|
|
||||||
q := ` update mini_slider set title = $1, img = $2 where id = $3 RETURNING id, title, img, created_at;`
|
q := ` update mini_slider set title = $1::json, img = $2 where id = $3 RETURNING id, title, img, created_at;`
|
||||||
|
|
||||||
err := r.client.QueryRow(ctx, q, req.Title, req.Image, id).Scan(
|
n, err := scanRow(func(dest ...interface{}) error {
|
||||||
&n.Id, &n.Title,
|
return r.client.QueryRow(ctx, q, title, req.Image, id).Scan(dest...)
|
||||||
&n.Image, &n.CreatedAt,
|
})
|
||||||
)
|
|
||||||
if err != nil && err.Error() != "no rows in result set" {
|
if err != nil && err.Error() != "no rows in result set" {
|
||||||
|
r.logger.Error("update mini slider :::", err)
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,6 +168,7 @@ func (r repository) DeleteData(ctx context.Context, id int) error {
|
|||||||
_, err := r.client.Exec(ctx, q, id)
|
_, err := r.client.Exec(ctx, q, id)
|
||||||
|
|
||||||
if err != nil && err.Error() != "no rows in result set" {
|
if err != nil && err.Error() != "no rows in result set" {
|
||||||
|
r.logger.Error("delete mini slider :::", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package mini_slider
|
package mini_slider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,27 +15,6 @@ type Language struct {
|
|||||||
En string `json:"en"`
|
En string `json:"en"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Language) Value() (driver.Value, error) {
|
|
||||||
return json.Marshal(l)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Language) Scan(src interface{}) error {
|
|
||||||
if src == nil {
|
|
||||||
*l = Language{}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var b []byte
|
|
||||||
switch v := src.(type) {
|
|
||||||
case []byte:
|
|
||||||
b = v
|
|
||||||
case string:
|
|
||||||
b = []byte(v)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("unsupported language type %T", src)
|
|
||||||
}
|
|
||||||
return json.Unmarshal(b, l)
|
|
||||||
}
|
|
||||||
|
|
||||||
type MiniSliderListItemDTO struct {
|
type MiniSliderListItemDTO struct {
|
||||||
Id int `json:"Id"`
|
Id int `json:"Id"`
|
||||||
Title Language `json:"title"`
|
Title Language `json:"title"`
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
|||||||
func (h *handler) GetAll(c *gin.Context) {
|
func (h *handler) GetAll(c *gin.Context) {
|
||||||
result, err := h.repository.GetAllData(context.Background(), listing.FromQuery(c))
|
result, err := h.repository.GetAllData(context.Background(), listing.FromQuery(c))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
h.logger.Error("get all mini slider :::", err)
|
||||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -107,6 +108,7 @@ func (h *handler) Create(c *gin.Context) {
|
|||||||
|
|
||||||
res, err := h.repository.AddData(context.Background(), reqDTO)
|
res, err := h.repository.AddData(context.Background(), reqDTO)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
h.logger.Error("create mini slider :::", err)
|
||||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -141,6 +143,7 @@ func (h *handler) Update(c *gin.Context) {
|
|||||||
|
|
||||||
res, err := h.repository.UpdateData(context.Background(), reqDTO, id)
|
res, err := h.repository.UpdateData(context.Background(), reqDTO, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
h.logger.Error("update mini slider :::", err)
|
||||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,11 +162,13 @@ func (r repository) GetMiniSliders(ctx context.Context, lang string) (users.Data
|
|||||||
lang = users.NormalizeLang(lang)
|
lang = users.NormalizeLang(lang)
|
||||||
|
|
||||||
q := ` SELECT
|
q := ` SELECT
|
||||||
id, title, img
|
id,
|
||||||
|
COALESCE(NULLIF(title->>$1, ''), NULLIF(title->>'tm', ''), ''),
|
||||||
|
img
|
||||||
FROM mini_slider
|
FROM mini_slider
|
||||||
ORDER BY created_at ASC;`
|
ORDER BY created_at ASC;`
|
||||||
|
|
||||||
rows, err := r.client.Query(ctx, q)
|
rows, err := r.client.Query(ctx, q, lang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.logger.Error("get mini sliders :::", err)
|
r.logger.Error("get mini sliders :::", err)
|
||||||
return dt, err
|
return dt, err
|
||||||
@@ -176,13 +178,11 @@ func (r repository) GetMiniSliders(ctx context.Context, lang string) (users.Data
|
|||||||
ss := []users.MiniSliderDTO{}
|
ss := []users.MiniSliderDTO{}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var s users.MiniSliderDTO
|
var s users.MiniSliderDTO
|
||||||
var title users.Language
|
errS := rows.Scan(&s.Id, &s.Title, &s.Image)
|
||||||
errS := rows.Scan(&s.Id, &title, &s.Image)
|
|
||||||
if errS != nil {
|
if errS != nil {
|
||||||
r.logger.Error("errS :::", errS)
|
r.logger.Error("errS :::", errS)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.Title = title.Pick(lang)
|
|
||||||
ss = append(ss, s)
|
ss = append(ss, s)
|
||||||
}
|
}
|
||||||
dt.MiniSliders = ss
|
dt.MiniSliders = ss
|
||||||
|
|||||||
@@ -450,7 +450,7 @@
|
|||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
<script src="/js/slider.js?v=11"></script>
|
<script src="/js/slider.js?v=12"></script>
|
||||||
<script src="/js/app.js?v=5"></script>
|
<script src="/js/app.js?v=8"></script>
|
||||||
<!-- <script src="/js/chatbot.js?v=8"></script> -->
|
<!-- <script src="/js/chatbot.js?v=8"></script> -->
|
||||||
</html>
|
</html>
|
||||||
@@ -213,11 +213,14 @@ async function fetchMiniSliders() {
|
|||||||
const imageSliderContainer = $_ID('mini-slider')
|
const imageSliderContainer = $_ID('mini-slider')
|
||||||
if (!imageSliderContainer) return
|
if (!imageSliderContainer) return
|
||||||
|
|
||||||
const response = await getData(withLang('/api/v1/client/users/mini-sliders'))
|
let slides = []
|
||||||
|
try {
|
||||||
|
const response = await getData(withLang('/api/v1/client/users/mini-sliders'))
|
||||||
|
slides = (response.data && response.data.miniSliders) || []
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
removeByClassName('minislider-loader')
|
removeByClassName('minislider-loader')
|
||||||
|
|
||||||
const slides = (response.data && response.data.miniSliders) || []
|
|
||||||
slides.forEach(slider => {
|
slides.forEach(slider => {
|
||||||
imageSliderContainer.insertAdjacentHTML('beforeend', `
|
imageSliderContainer.insertAdjacentHTML('beforeend', `
|
||||||
<div class="mySlides fade w-full relative">
|
<div class="mySlides fade w-full relative">
|
||||||
|
|||||||
Reference in new issue
Block a user