fixed
This commit is contained in:
1 parent
4194cef934
commit
363127fe9e
3350 files changed
+1284220
-1
No files matched your search
+169
@@ -0,0 +1,169 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bankline/internal/admin/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) category.Repository {
|
||||
return &repository{
|
||||
client: client,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
var sortColumns = map[string]string{
|
||||
"id": "cs.id",
|
||||
"status": "cs.status",
|
||||
"createdAt": "cs.created_at",
|
||||
}
|
||||
|
||||
func (r repository) GetAllData(ctx context.Context, params listing.Params) (category.DataCategoryDTO, error) {
|
||||
|
||||
ns := []category.CategoryDTO{}
|
||||
var (
|
||||
dt category.DataCategoryDTO
|
||||
err error
|
||||
rows pgx.Rows
|
||||
)
|
||||
|
||||
where := ""
|
||||
args := []interface{}{}
|
||||
if params.Search != "" {
|
||||
where = ` where cs.name::text ILIKE $1 `
|
||||
args = append(args, params.SearchPattern())
|
||||
}
|
||||
|
||||
qC := `select count(*) from categories cs` + where + `;`
|
||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
||||
|
||||
q := ` select
|
||||
cs.Id, cs.name,
|
||||
jsonb_agg(ss.name) as sub_category,
|
||||
cs.status, cs.created_at
|
||||
from categories cs
|
||||
left join sub_categories ss on ss.category_id = cs.Id ` +
|
||||
where +
|
||||
` group by cs.Id ` +
|
||||
params.OrderClause(sortColumns, "cs.created_at ASC")
|
||||
|
||||
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 category.CategoryDTO
|
||||
errN := rows.Scan(
|
||||
&n.Id, &n.Name, &n.SubName,
|
||||
&n.Status, &n.CreatedAt,
|
||||
)
|
||||
if errN != nil {
|
||||
r.logger.Error("errN :::", errN)
|
||||
continue
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
dt.Categories = ns
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) GetData(ctx context.Context, id int) (category.CategoryDTO, error) {
|
||||
|
||||
var (
|
||||
ns category.CategoryDTO
|
||||
)
|
||||
|
||||
qC := `select id, name, status, created_at from categories where id = $1 ;`
|
||||
err := r.client.QueryRow(ctx, qC, id).Scan(&ns.Id, &ns.Name, &ns.Status, &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 category.AddCategoryDTO) (category.CategoryIdDTO, error) {
|
||||
|
||||
var (
|
||||
n category.CategoryIdDTO
|
||||
)
|
||||
|
||||
q := `INSERT INTO categories (name) VALUES ($1) RETURNING id, name, status;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.Name).Scan(
|
||||
&n.Id, &n.Name, &n.Status,
|
||||
)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return n, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) UpdateData(ctx context.Context, req category.UpdateCategoryDTO, id int) (category.CategoryIdDTO, error) {
|
||||
var (
|
||||
q string
|
||||
n category.CategoryIdDTO
|
||||
)
|
||||
|
||||
q = ` update categories set name = $1 where Id = $2 RETURNING id, name, status;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.Name, id).Scan(
|
||||
&n.Id, &n.Name, &n.Status,
|
||||
)
|
||||
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return n, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) UpdateStatusData(ctx context.Context, status string, id int) (category.CategoryDTO, error) {
|
||||
|
||||
q := ` update 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 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
+49
@@ -0,0 +1,49 @@
|
||||
package category
|
||||
|
||||
import "time"
|
||||
|
||||
type DataCategoryDTO struct {
|
||||
Categories []CategoryDTO `json:"categories"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Tm string `json:"tm"`
|
||||
Ru string `json:"ru"`
|
||||
En string `json:"en"`
|
||||
}
|
||||
|
||||
type CategoryDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Name Language `json:"name"`
|
||||
SubName []Language `json:"subCategories"`
|
||||
Status bool `json:"status"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type AddCategoryDTO struct {
|
||||
Name Language `json:"name" binding:"required"`
|
||||
}
|
||||
|
||||
type UpdateCategoryDTO struct {
|
||||
Name Language `json:"name" binding:"required"`
|
||||
// Status intentionally has no "required" binding: the validator treats
|
||||
// false as a missing value, which would make it impossible to update a
|
||||
// disabled category.
|
||||
Status bool `json:"status"`
|
||||
}
|
||||
|
||||
type CategoryIdDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Name Language `json:"name"`
|
||||
Status bool `json:"status"`
|
||||
}
|
||||
|
||||
type PaginationDTO struct {
|
||||
Limit int `json:"limit"`
|
||||
Page int `json:"page"`
|
||||
}
|
||||
|
||||
type ReqIdDTO struct {
|
||||
Id int `json:"Id"`
|
||||
}
|
||||
Executable
+246
@@ -0,0 +1,246 @@
|
||||
package category
|
||||
|
||||
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"
|
||||
getStatusURL = "/:id/status"
|
||||
)
|
||||
|
||||
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.PUT(getStatusURL, h.UpdateStatus)
|
||||
router.DELETE(getOneURL, h.Delete)
|
||||
|
||||
}
|
||||
|
||||
// GetAll godoc
|
||||
// @Description all data categories
|
||||
// @Tags 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)"
|
||||
// @Success 200 {object} DataCategoryDTO
|
||||
// @Router /admin/categories [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 category
|
||||
// @Tags categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "category ID"
|
||||
// @Success 200 {object} CategoryDTO
|
||||
// @Router /admin/categories/{id} [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 {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = result
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Create godoc
|
||||
// @Description create categories
|
||||
// @Tags categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param categories body AddCategoryDTO true "Create JSON"
|
||||
// @Success 200 {string} string
|
||||
// @Router /admin/categories [post]
|
||||
func (h *handler) Create(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
reqDTO AddCategoryDTO
|
||||
)
|
||||
|
||||
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 categories
|
||||
// @Tags categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "category ID"
|
||||
// @Param categories body UpdateCategoryDTO true "Create JSON"
|
||||
// @Success 200 {object} CategoryIdDTO
|
||||
// @Router /admin/categories/{id} [put]
|
||||
func (h *handler) Update(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
reqDTO UpdateCategoryDTO
|
||||
res CategoryIdDTO
|
||||
)
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
// UpdateStatus godoc
|
||||
// @Description update status categories
|
||||
// @Tags categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "category id"
|
||||
// @Param status query bool true "status"
|
||||
// @Success 200 {object} CategoryDTO
|
||||
// @Router /admin/categories/{id}/status [put]
|
||||
func (h *handler) UpdateStatus(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
res CategoryDTO
|
||||
)
|
||||
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 category
|
||||
// @Tags categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "category ID"
|
||||
// @Success 200 {string} string
|
||||
// @Router /admin/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 {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = ""
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
package category
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
package category
|
||||
|
||||
import (
|
||||
"bankline/internal/listing"
|
||||
"context"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetAllData(ctx context.Context, params listing.Params) (DataCategoryDTO, error)
|
||||
GetData(ctx context.Context, id int) (CategoryDTO, error)
|
||||
|
||||
AddData(ctx context.Context, dto AddCategoryDTO) (CategoryIdDTO, error)
|
||||
UpdateData(ctx context.Context, dto UpdateCategoryDTO, id int) (CategoryIdDTO, error)
|
||||
UpdateStatusData(ctx context.Context, status string, id int) (CategoryDTO, error)
|
||||
DeleteData(ctx context.Context, uuid int) error
|
||||
}
|
||||
Reference in new issue
Block a user