fixed
This commit is contained in:
1 parent
4194cef934
commit
363127fe9e
3350 files changed
+1284220
-1
No files matched your search
+224
@@ -0,0 +1,224 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bankline/internal/admin/sub_category"
|
||||
"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) sub_category.Repository {
|
||||
return &repository{
|
||||
client: client,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
var sortColumns = map[string]string{
|
||||
"id": "sc.id",
|
||||
"status": "sc.status",
|
||||
"createdAt": "sc.created_at",
|
||||
}
|
||||
|
||||
func (r repository) GetAllData(ctx context.Context, params listing.Params, categoryId int) (sub_category.DataDTO, error) {
|
||||
|
||||
ns := []sub_category.SubCategoryDTO{}
|
||||
var (
|
||||
dt sub_category.DataDTO
|
||||
err error
|
||||
rows pgx.Rows
|
||||
)
|
||||
|
||||
where := ` where sc.category_id = $1 `
|
||||
args := []interface{}{categoryId}
|
||||
if params.Search != "" {
|
||||
where += ` and (sc.name::text ILIKE $2 OR scc.tm ILIKE $2 OR scc.ru ILIKE $2 OR scc.en ILIKE $2) `
|
||||
args = append(args, params.SearchPattern())
|
||||
}
|
||||
|
||||
qC := `select count(sc.*) from sub_categories sc
|
||||
LEFT JOIN sub_categories_content scc ON sc.id = scc.sub_category_id` + where + `;`
|
||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
||||
|
||||
q := `select
|
||||
sc.id, sc.status,
|
||||
sc.name, sc.created_at,
|
||||
COALESCE(scc.en, ''), COALESCE(scc.tm, ''), COALESCE(scc.ru, '')
|
||||
from sub_categories sc
|
||||
LEFT JOIN sub_categories_content scc ON sc.id = scc.sub_category_id` +
|
||||
where +
|
||||
params.OrderClause(sortColumns, "sc.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 {
|
||||
fmt.Println("=========", err)
|
||||
return dt, nil
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var n sub_category.SubCategoryDTO
|
||||
errN := rows.Scan(
|
||||
&n.Id, &n.Status,
|
||||
&n.Name, &n.CreatedAt,
|
||||
&n.Content.En, &n.Content.Tm, &n.Content.Ru,
|
||||
)
|
||||
if errN != nil {
|
||||
fmt.Println("errN :::", errN)
|
||||
continue
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
dt.SubCategories = ns
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) GetData(ctx context.Context, id int) (sub_category.SubCategoryDTO, error) {
|
||||
|
||||
var (
|
||||
ns sub_category.SubCategoryDTO
|
||||
)
|
||||
|
||||
qC := `select
|
||||
sc.id, sc.category_id, sc.name, sc.status, sc.created_at,
|
||||
COALESCE(scc.en, ''), COALESCE(scc.tm, ''), COALESCE(scc.ru, '')
|
||||
from sub_categories sc
|
||||
LEFT JOIN sub_categories_content scc ON sc.id = scc.sub_category_id
|
||||
where sc.id = $1 ;`
|
||||
err := r.client.QueryRow(ctx, qC, id).Scan(
|
||||
&ns.Id, &ns.CatId, &ns.Name, &ns.Status, &ns.CreatedAt,
|
||||
&ns.Content.En, &ns.Content.Tm, &ns.Content.Ru,
|
||||
)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return ns, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (r repository) AddData(ctx context.Context, req sub_category.AddSubCategoryDTO) (sub_category.SubCategoryDTO, error) {
|
||||
|
||||
var (
|
||||
n sub_category.SubCategoryDTO
|
||||
lang sub_category.Language
|
||||
)
|
||||
|
||||
q := `INSERT INTO sub_categories (category_id, name) VALUES ($1, $2) RETURNING id, category_id, name, status;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.CatId, req.Name).Scan(
|
||||
&n.Id, &n.CatId,
|
||||
&n.Name, &n.Status,
|
||||
)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
fmt.Println("-----------", err)
|
||||
return n, err
|
||||
}
|
||||
|
||||
if n.Id != 0 {
|
||||
q = `INSERT INTO sub_categories_content (sub_category_id, en, ru, tm)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING en, ru, tm;`
|
||||
|
||||
err = r.client.QueryRow(ctx, q, n.Id, req.Content.En, req.Content.Ru, req.Content.Tm).Scan(
|
||||
&lang.En, &lang.Ru, &lang.Tm,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println("-----------", err)
|
||||
return n, err
|
||||
}
|
||||
n.Content = lang
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) UpdateData(ctx context.Context, req sub_category.UpdateSubCategoryDTO, id int) (sub_category.SubCategoryDTO, error) {
|
||||
var (
|
||||
q string
|
||||
n sub_category.SubCategoryDTO
|
||||
lang sub_category.Language
|
||||
)
|
||||
|
||||
q = ` update sub_categories set category_id = $1, name = $2 where Id = $3 RETURNING id, category_id, name, status;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.CatId, req.Name, id).Scan(
|
||||
&n.Id, &n.CatId,
|
||||
&n.Name, &n.Status,
|
||||
)
|
||||
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
fmt.Println("-----------", err)
|
||||
return n, err
|
||||
}
|
||||
|
||||
if n.Id != 0 {
|
||||
q = `UPDATE sub_categories_content SET en = $1, ru = $2, tm = $3
|
||||
WHERE sub_category_id = $4 RETURNING en, ru, tm;`
|
||||
|
||||
err = r.client.QueryRow(ctx, q, req.Content.En, req.Content.Ru, req.Content.Tm, n.Id).Scan(
|
||||
&lang.En, &lang.Ru, &lang.Tm,
|
||||
)
|
||||
if err != nil {
|
||||
// Row may be missing on older data — insert then.
|
||||
if err.Error() == "no rows in result set" {
|
||||
q = `INSERT INTO sub_categories_content (sub_category_id, en, ru, tm)
|
||||
VALUES ($1, $2, $3, $4) RETURNING en, ru, tm;`
|
||||
err = r.client.QueryRow(ctx, q, n.Id, req.Content.En, req.Content.Ru, req.Content.Tm).Scan(
|
||||
&lang.En, &lang.Ru, &lang.Tm,
|
||||
)
|
||||
}
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
fmt.Println("-----------", err)
|
||||
return n, err
|
||||
}
|
||||
}
|
||||
n.Content = lang
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) UpdateStatusData(ctx context.Context, status string, id int) (sub_category.SubCategoryDTO, error) {
|
||||
|
||||
q := ` update sub_categories set status = $1 where id = $2;`
|
||||
|
||||
_, err := r.client.Exec(ctx, q, status, id)
|
||||
|
||||
dt, err := r.GetData(ctx, id)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return dt, err
|
||||
}
|
||||
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) DeleteData(ctx context.Context, Id int) error {
|
||||
var (
|
||||
q string
|
||||
)
|
||||
|
||||
q = ` delete from sub_categories where Id = $1 ;`
|
||||
_, err := r.client.Exec(ctx, q, Id)
|
||||
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
package sub_category
|
||||
|
||||
import "time"
|
||||
|
||||
type DataDTO struct {
|
||||
SubCategories []SubCategoryDTO `json:"subCategory"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Tm string `json:"tm"`
|
||||
Ru string `json:"ru"`
|
||||
En string `json:"en"`
|
||||
}
|
||||
|
||||
type SubCategoryDTO struct {
|
||||
Id int `json:"Id"`
|
||||
CatId int `json:"categoryId"`
|
||||
Name Language `json:"name"`
|
||||
Content Language `json:"content"`
|
||||
Status bool `json:"status"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type AddSubCategoryDTO struct {
|
||||
CatId int `json:"categoryId"`
|
||||
Name Language `json:"name"`
|
||||
Content Language `json:"content"`
|
||||
}
|
||||
|
||||
type UpdateSubCategoryDTO struct {
|
||||
CatId int `json:"categoryId"`
|
||||
Name Language `json:"name"`
|
||||
Content Language `json:"content"`
|
||||
}
|
||||
+264
@@ -0,0 +1,264 @@
|
||||
package sub_category
|
||||
|
||||
import (
|
||||
"bankline/internal/appresult"
|
||||
"bankline/internal/handlers"
|
||||
"bankline/internal/listing"
|
||||
"bankline/pkg/logging"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
getAllURL = ""
|
||||
getOneURL = "/:id"
|
||||
getEditURL = "/:id/edit"
|
||||
getStatusURL = "/:id/status"
|
||||
)
|
||||
|
||||
var validate *validator.Validate
|
||||
|
||||
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(getOneURL, h.GetAll)
|
||||
router.GET(getEditURL, h.GetOne)
|
||||
router.POST(getAllURL, h.Create)
|
||||
|
||||
router.PUT(getOneURL, h.Update)
|
||||
router.PUT(getStatusURL, h.UpdateStatus)
|
||||
router.DELETE(getOneURL, h.Delete)
|
||||
}
|
||||
|
||||
// GetAll godoc
|
||||
// @Description all sub-categories
|
||||
// @Tags sub-categories
|
||||
// @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, status, createdAt)"
|
||||
// @Param order query string false "sort order (asc, desc)"
|
||||
// @Param id path int true "category id"
|
||||
// @Success 200 {object} DataDTO
|
||||
// @Router /admin/sub-categories/{id} [get]
|
||||
func (h *handler) GetAll(c *gin.Context) {
|
||||
categoryId := c.Param("id")
|
||||
|
||||
id, err := strconv.Atoi(categoryId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.repository.GetAllData(context.Background(), listing.FromQuery(c), id)
|
||||
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 category
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "sub-category id"
|
||||
// @Success 200 {object} SubCategoryDTO
|
||||
// @Router /admin/sub-categories/{id}/edit [get]
|
||||
func (h *handler) GetOne(c *gin.Context) {
|
||||
|
||||
categoryId := c.Param("id")
|
||||
id, err := strconv.Atoi(categoryId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.repository.GetData(context.Background(), id)
|
||||
if err != nil {
|
||||
appresult.ErrInternalServer.Err = err
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = result
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Create godoc
|
||||
// @Description create sub categories
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param sub-categories body AddSubCategoryDTO true "Create JSON"
|
||||
// @Success 200 {object} SubCategoryDTO
|
||||
// @Router /admin/sub-categories [post]
|
||||
func (h *handler) Create(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
reqDTO AddSubCategoryDTO
|
||||
)
|
||||
|
||||
if err = c.ShouldBindJSON(&reqDTO); err != nil {
|
||||
appresult.ErrMissingParam.Err = err
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := h.repository.AddData(context.Background(), reqDTO)
|
||||
|
||||
if err != nil {
|
||||
appresult.ErrInternalServer.Err = err
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = res
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// Update godoc
|
||||
// @Description update sub categories
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "sub-category id"
|
||||
// @Param sub-categories body UpdateSubCategoryDTO true "Create JSON"
|
||||
// @Success 200 {object} SubCategoryDTO
|
||||
// @Router /admin/sub-categories/{id} [put]
|
||||
func (h *handler) Update(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
reqDTO UpdateSubCategoryDTO
|
||||
res SubCategoryDTO
|
||||
)
|
||||
|
||||
categoryId := c.Param("id")
|
||||
id, err := strconv.Atoi(categoryId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err = c.ShouldBindJSON(&reqDTO); err != nil {
|
||||
appresult.ErrNotFound.Err = err
|
||||
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 {
|
||||
appresult.ErrInternalServer.Err = err
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = res
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus godoc
|
||||
// @Description update status sub-categories
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "sub-category id"
|
||||
// @Param status query bool true "status"
|
||||
// @Success 200 {object} SubCategoryDTO
|
||||
// @Router /admin/sub-categories/{id}/status [put]
|
||||
func (h *handler) UpdateStatus(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
res SubCategoryDTO
|
||||
)
|
||||
Id := c.Param("id")
|
||||
id, err := strconv.Atoi(Id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
status, _ := c.GetQuery("status")
|
||||
fmt.Println("id :::", id)
|
||||
if id != 0 {
|
||||
res, err = h.repository.UpdateStatusData(context.Background(), status, id)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = res
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete godoc
|
||||
// @Description delete sub-category
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "sub category id"
|
||||
// @Success 200 {string} string
|
||||
// @Router /admin/sub-categories/{id} [delete]
|
||||
func (h *handler) Delete(c *gin.Context) {
|
||||
|
||||
categoryId := c.Param("id")
|
||||
id, err := strconv.Atoi(categoryId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.repository.DeleteData(context.Background(), id)
|
||||
if err != nil {
|
||||
appresult.ErrInternalServer.Err = err
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = ""
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
package sub_category
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package sub_category
|
||||
|
||||
import (
|
||||
"bankline/internal/listing"
|
||||
"context"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetAllData(ctx context.Context, params listing.Params, categoryId int) (DataDTO, error)
|
||||
GetData(ctx context.Context, id int) (SubCategoryDTO, error)
|
||||
AddData(ctx context.Context, dto AddSubCategoryDTO) (SubCategoryDTO, error)
|
||||
UpdateData(ctx context.Context, dto UpdateSubCategoryDTO, id int) (SubCategoryDTO, error)
|
||||
UpdateStatusData(ctx context.Context, status string, id int) (SubCategoryDTO, error)
|
||||
DeleteData(ctx context.Context, id int) error
|
||||
}
|
||||
Reference in new issue
Block a user