fixed
This commit is contained in:
1 parent
4194cef934
commit
363127fe9e
3350 files changed
+1284220
-1
No files matched your search
+167
@@ -0,0 +1,167 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bankline/internal/admin/banner"
|
||||
"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) banner.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) (banner.DataBannerDTO, error) {
|
||||
|
||||
ns := []banner.GetBannerDTO{}
|
||||
var (
|
||||
dt banner.DataBannerDTO
|
||||
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 banner` + where + `;`
|
||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
||||
|
||||
q := ` select
|
||||
id, title,
|
||||
content_tm, content_ru, content_en,
|
||||
img, created_at
|
||||
from banner` +
|
||||
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 banner.GetBannerDTO
|
||||
errN := rows.Scan(
|
||||
&n.Id, &n.Title,
|
||||
&n.ContentTm, &n.ContentRu, &n.ContentEn,
|
||||
&n.Image, &n.CreatedAt,
|
||||
)
|
||||
if errN != nil {
|
||||
r.logger.Error("errN :::", errN)
|
||||
continue
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
dt.Categories = ns
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) GetOneData(ctx context.Context, id int) (banner.GetBannerDTO, error) {
|
||||
|
||||
var (
|
||||
n banner.GetBannerDTO
|
||||
)
|
||||
|
||||
q := ` select
|
||||
id, title,
|
||||
content_tm, content_ru, content_en,
|
||||
img, created_at
|
||||
from banner where id = $1 ; `
|
||||
|
||||
err := r.client.QueryRow(ctx, q, id).Scan(
|
||||
&n.Id, &n.Title,
|
||||
&n.ContentTm, &n.ContentRu, &n.ContentEn,
|
||||
&n.Image, &n.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) AddData(ctx context.Context, req banner.AddBannerDTO) (banner.GetBannerDTO, error) {
|
||||
var (
|
||||
n banner.GetBannerDTO
|
||||
)
|
||||
|
||||
q := `INSERT INTO banner (title, content_tm, content_ru, content_en, img) VALUES ($1, $2, $3, $4, $5) RETURNING id, title,
|
||||
content_tm, content_ru, content_en,
|
||||
img, created_at;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.Title, req.ContentTm, req.ContentRu, req.ContentEn, req.Image).Scan(
|
||||
&n.Id, &n.Title,
|
||||
&n.ContentTm, &n.ContentRu, &n.ContentEn,
|
||||
&n.Image, &n.CreatedAt,
|
||||
)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return n, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) UpdateData(ctx context.Context, req banner.UpdateBannerDTO, id int) (banner.GetBannerDTO, error) {
|
||||
var (
|
||||
q string
|
||||
n banner.GetBannerDTO
|
||||
)
|
||||
|
||||
q = ` update banner set title = $1, content_tm = $2, content_ru = $3, content_en = $4, img = $5 where id = $6 RETURNING id, title,
|
||||
content_tm, content_ru, content_en,
|
||||
img, created_at;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.Title, req.ContentTm, req.ContentRu, req.ContentEn, req.Image, id).Scan(
|
||||
&n.Id, &n.Title,
|
||||
&n.ContentTm, &n.ContentRu, &n.ContentEn,
|
||||
&n.Image, &n.CreatedAt,
|
||||
)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return n, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) DeleteData(ctx context.Context, uuid int) error {
|
||||
var (
|
||||
q string
|
||||
)
|
||||
|
||||
q = ` delete from banner where id = $1 ;`
|
||||
_, err := r.client.Exec(ctx, q, uuid)
|
||||
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
package banner
|
||||
|
||||
import "time"
|
||||
|
||||
type DataBannerDTO struct {
|
||||
Categories []GetBannerDTO `json:"banners"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Tm string `json:"tm"`
|
||||
Ru string `json:"ru"`
|
||||
En string `json:"en"`
|
||||
}
|
||||
|
||||
type GetBannerDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Title Language `json:"title"`
|
||||
ContentTm string `json:"contentTm"`
|
||||
ContentRu string `json:"contentRu"`
|
||||
ContentEn string `json:"contentEn"`
|
||||
Image string `json:"imagePath"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type AddBannerDTO struct {
|
||||
Title Language `json:"title" binding:"required"`
|
||||
ContentTm string `json:"contentTm"`
|
||||
ContentRu string `json:"contentRu"`
|
||||
ContentEn string `json:"contentEn"`
|
||||
Image string `json:"imagePath"`
|
||||
}
|
||||
|
||||
type UpdateBannerDTO struct {
|
||||
Title Language `json:"title" binding:"required"`
|
||||
ContentTm string `json:"contentTm"`
|
||||
ContentRu string `json:"contentRu"`
|
||||
ContentEn string `json:"contentEn"`
|
||||
Image string `json:"imagePath"`
|
||||
}
|
||||
|
||||
type ReqUUIDDTO struct {
|
||||
Id int `json:"Id"`
|
||||
}
|
||||
|
||||
type ReqIDDTO struct {
|
||||
Id int `json:"id"`
|
||||
}
|
||||
Executable
+212
@@ -0,0 +1,212 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"bankline/internal/appresult"
|
||||
"bankline/internal/handlers"
|
||||
"bankline/internal/listing"
|
||||
"bankline/pkg/logging"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"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 banner
|
||||
// @Tags banner
|
||||
// @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} DataBannerDTO
|
||||
// @Router /admin/banner [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 banner
|
||||
// @Tags banner
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "banner ID"
|
||||
// @Success 200 {object} GetBannerDTO
|
||||
// @Router /admin/banner/{id} [get]
|
||||
func (h *handler) GetOne(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
fmt.Println("--------------", id)
|
||||
i, err := strconv.Atoi(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
result, err := h.repository.GetOneData(context.Background(), i)
|
||||
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 banner
|
||||
// @Tags banner
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param banner body AddBannerDTO true "Create JSON"
|
||||
// @Success 200 {object} GetBannerDTO
|
||||
// @Router /admin/banner [post]
|
||||
func (h *handler) Create(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
|
||||
body, errBody := io.ReadAll(c.Request.Body)
|
||||
if errBody != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
reqDTO := AddBannerDTO{}
|
||||
err = json.Unmarshal(body, &reqDTO)
|
||||
res, err := h.repository.AddData(context.Background(), reqDTO)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = res
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// Update godoc
|
||||
// @Description update banner
|
||||
// @Tags banner
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "banner ID"
|
||||
// @Param banner body UpdateBannerDTO true "Create JSON"
|
||||
// @Success 200 {string} string
|
||||
// @Router /admin/banner/{id} [put]
|
||||
func (h *handler) Update(c *gin.Context) {
|
||||
var (
|
||||
res GetBannerDTO
|
||||
)
|
||||
bannerId := c.Param("id")
|
||||
fmt.Println("--------------", bannerId)
|
||||
id, err := strconv.Atoi(bannerId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
body, errBody := io.ReadAll(c.Request.Body)
|
||||
if errBody != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
reqDTO := UpdateBannerDTO{}
|
||||
err = json.Unmarshal(body, &reqDTO)
|
||||
|
||||
fmt.Println("id :::", id)
|
||||
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 banner
|
||||
// @Tags banner
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "banner ID"
|
||||
// @Success 200 {string} string
|
||||
// @Router /admin/banner/{id} [delete]
|
||||
func (h *handler) Delete(c *gin.Context) {
|
||||
|
||||
bannerId := c.Param("id")
|
||||
fmt.Println("--------------", bannerId)
|
||||
id, err := strconv.Atoi(bannerId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrNotAcceptable)
|
||||
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
|
||||
}
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
package banner
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"bankline/internal/listing"
|
||||
"context"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetAllData(ctx context.Context, params listing.Params) (DataBannerDTO, error)
|
||||
GetOneData(ctx context.Context, id int) (GetBannerDTO, error)
|
||||
AddData(ctx context.Context, dto AddBannerDTO) (GetBannerDTO, error)
|
||||
UpdateData(ctx context.Context, dto UpdateBannerDTO, id int) (GetBannerDTO, error)
|
||||
DeleteData(ctx context.Context, id int) error
|
||||
}
|
||||
Reference in new issue
Block a user