settings section
This commit is contained in:
260
internal/admin/branches/db/postgresql.go
Normal file
260
internal/admin/branches/db/postgresql.go
Normal file
@@ -0,0 +1,260 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/admin/branches"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client postgresql.Client
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) branches.Repository {
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
const branchColumns = `b.id, b.title, b.province_id, b.location_id, b.unikal_nomer, b.address,
|
||||
b.billing_login, b.billing_password, b.billing_username_sber, b.billing_password_sber,
|
||||
b.billing_username_visa, b.billing_password_visa`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter branches.ListFilter) (branches.ListBranchDTO, error) {
|
||||
fromClause, whereClause, args := buildListQuery(filter)
|
||||
|
||||
var count int64
|
||||
countQuery := `SELECT COUNT(*) ` + fromClause + whereClause
|
||||
if err := r.client.QueryRow(ctx, countQuery, args...).Scan(&count); err != nil {
|
||||
return branches.ListBranchDTO{}, err
|
||||
}
|
||||
|
||||
offset := (filter.Page - 1) * filter.Limit
|
||||
limitPlaceholder := len(args) + 1
|
||||
offsetPlaceholder := len(args) + 2
|
||||
listQuery := fmt.Sprintf(
|
||||
`SELECT %s%s%s ORDER BY b.id LIMIT $%d OFFSET $%d`,
|
||||
branchColumns, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
||||
)
|
||||
listArgs := append(args, filter.Limit, offset)
|
||||
|
||||
rows, err := r.client.Query(ctx, listQuery, listArgs...)
|
||||
if err != nil {
|
||||
return branches.ListBranchDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []branches.BranchDTO
|
||||
for rows.Next() {
|
||||
item, err := scanBranch(rows)
|
||||
if err != nil {
|
||||
return branches.ListBranchDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return branches.ListBranchDTO{}, err
|
||||
}
|
||||
|
||||
return branches.ListBranchDTO{Count: count, Branches: items}, nil
|
||||
}
|
||||
|
||||
func buildListQuery(filter branches.ListFilter) (fromClause, whereClause string, args []interface{}) {
|
||||
fromClause = ` FROM branches b`
|
||||
|
||||
if filter.ProvinceName != "" {
|
||||
fromClause += ` JOIN province p ON p.id = b.province_id`
|
||||
}
|
||||
|
||||
var conditions []string
|
||||
|
||||
if filter.ProvinceID != nil {
|
||||
args = append(args, *filter.ProvinceID)
|
||||
conditions = append(conditions, fmt.Sprintf("b.province_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.LocationID != nil {
|
||||
args = append(args, *filter.LocationID)
|
||||
conditions = append(conditions, fmt.Sprintf("b.location_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.ProvinceName != "" {
|
||||
args = append(args, likePattern(filter.ProvinceName))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
p.title->>'en' ILIKE $%[1]d OR p.title->>'ru' ILIKE $%[1]d OR p.title->>'tm' ILIKE $%[1]d
|
||||
)`, n))
|
||||
}
|
||||
|
||||
if filter.BranchName != "" {
|
||||
args = append(args, likePattern(filter.BranchName))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
b.title->>'en' ILIKE $%[1]d OR b.title->>'ru' ILIKE $%[1]d OR b.title->>'tm' ILIKE $%[1]d
|
||||
)`, n))
|
||||
}
|
||||
|
||||
if filter.UnikalNomer != "" {
|
||||
args = append(args, likePattern(filter.UnikalNomer))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf("b.unikal_nomer ILIKE $%d", n))
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
return fromClause, "", args
|
||||
}
|
||||
|
||||
return fromClause, " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
func likePattern(term string) string {
|
||||
term = strings.ReplaceAll(term, `\`, `\\`)
|
||||
term = strings.ReplaceAll(term, `%`, `\%`)
|
||||
term = strings.ReplaceAll(term, `_`, `\_`)
|
||||
return "%" + term + "%"
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (branches.BranchDTO, error) {
|
||||
query := fmt.Sprintf(`SELECT %s FROM branches b WHERE b.id = $1`, branchColumns)
|
||||
row := r.client.QueryRow(ctx, query, id)
|
||||
item, err := scanBranch(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto branches.CreateBranchDTO) (branches.BranchDTO, error) {
|
||||
if err := r.ensureRelations(ctx, dto.ProvinceId, dto.LocationId); err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
|
||||
title, address, err := marshalBranchJSON(dto.Title, dto.Address)
|
||||
if err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO branches (
|
||||
title, province_id, location_id, unikal_nomer, address,
|
||||
billing_login, billing_password, billing_username_sber, billing_password_sber,
|
||||
billing_username_visa, billing_password_visa
|
||||
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)
|
||||
RETURNING %s`, branchColumns)
|
||||
|
||||
row := r.client.QueryRow(ctx, query,
|
||||
title, dto.ProvinceId, dto.LocationId, dto.UnikalNomer, address,
|
||||
dto.BillingLogin, dto.BillingPassword, dto.BillingUsernameSber, dto.BillingPasswordSber,
|
||||
dto.BillingUsernameVisa, dto.BillingPasswordVisa,
|
||||
)
|
||||
item, err := scanBranch(row)
|
||||
if isUniqueViolation(err) {
|
||||
return branches.BranchDTO{}, fmt.Errorf("unikal nomer already exists")
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id int64, dto branches.UpdateBranchDTO) (branches.BranchDTO, error) {
|
||||
if err := r.ensureRelations(ctx, dto.ProvinceId, dto.LocationId); err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
|
||||
title, address, err := marshalBranchJSON(dto.Title, dto.Address)
|
||||
if err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE branches SET
|
||||
title = $1, province_id = $2, location_id = $3, unikal_nomer = $4, address = $5,
|
||||
billing_login = $6, billing_password = $7, billing_username_sber = $8,
|
||||
billing_password_sber = $9, billing_username_visa = $10, billing_password_visa = $11
|
||||
WHERE id = $12
|
||||
RETURNING %s`, branchColumns)
|
||||
|
||||
row := r.client.QueryRow(ctx, query,
|
||||
title, dto.ProvinceId, dto.LocationId, dto.UnikalNomer, address,
|
||||
dto.BillingLogin, dto.BillingPassword, dto.BillingUsernameSber, dto.BillingPasswordSber,
|
||||
dto.BillingUsernameVisa, dto.BillingPasswordVisa, id,
|
||||
)
|
||||
item, err := scanBranch(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
if isUniqueViolation(err) {
|
||||
return branches.BranchDTO{}, fmt.Errorf("unikal nomer already exists")
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM branches WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return pgx.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repository) ensureRelations(ctx context.Context, provinceID, locationID int64) error {
|
||||
var id int64
|
||||
err := r.client.QueryRow(ctx,
|
||||
`SELECT id FROM location WHERE id = $1 AND province_id = $2`, locationID, provinceID,
|
||||
).Scan(&id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fmt.Errorf("location not found in province")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func marshalBranchJSON(title, address branches.LocalizedText) ([]byte, []byte, error) {
|
||||
titleJSON, err := json.Marshal(title)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
addressJSON, err := json.Marshal(address)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return titleJSON, addressJSON, nil
|
||||
}
|
||||
|
||||
func isUniqueViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
return errors.As(err, &pgErr) && pgErr.Code == "23505"
|
||||
}
|
||||
|
||||
type scannable interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
func scanBranch(row scannable) (branches.BranchDTO, error) {
|
||||
var item branches.BranchDTO
|
||||
var titleJSON, addressJSON []byte
|
||||
|
||||
err := row.Scan(
|
||||
&item.Id, &titleJSON, &item.ProvinceId, &item.LocationId, &item.UnikalNomer, &addressJSON,
|
||||
&item.BillingLogin, &item.BillingPassword, &item.BillingUsernameSber, &item.BillingPasswordSber,
|
||||
&item.BillingUsernameVisa, &item.BillingPasswordVisa,
|
||||
)
|
||||
if err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(addressJSON, &item.Address); err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
65
internal/admin/branches/dto.go
Normal file
65
internal/admin/branches/dto.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package branches
|
||||
|
||||
type LocalizedText struct {
|
||||
En string `json:"en"`
|
||||
Ru string `json:"ru"`
|
||||
Tm string `json:"tm"`
|
||||
}
|
||||
|
||||
type BranchDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Title LocalizedText `json:"title"`
|
||||
ProvinceId int64 `json:"provinceId"`
|
||||
LocationId int64 `json:"locationId"`
|
||||
UnikalNomer string `json:"unikalNomer"`
|
||||
Address LocalizedText `json:"address"`
|
||||
BillingLogin string `json:"billingLogin"`
|
||||
BillingPassword string `json:"billingPassword"`
|
||||
BillingUsernameSber string `json:"billingUsernameSber"`
|
||||
BillingPasswordSber string `json:"billingPasswordSber"`
|
||||
BillingUsernameVisa string `json:"billingUsernameVisa"`
|
||||
BillingPasswordVisa string `json:"billingPasswordVisa"`
|
||||
}
|
||||
|
||||
type CreateBranchDTO struct {
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
ProvinceId int64 `json:"provinceId" binding:"required"`
|
||||
LocationId int64 `json:"locationId" binding:"required"`
|
||||
UnikalNomer string `json:"unikalNomer" binding:"required"`
|
||||
Address LocalizedText `json:"address" binding:"required"`
|
||||
BillingLogin string `json:"billingLogin"`
|
||||
BillingPassword string `json:"billingPassword"`
|
||||
BillingUsernameSber string `json:"billingUsernameSber"`
|
||||
BillingPasswordSber string `json:"billingPasswordSber"`
|
||||
BillingUsernameVisa string `json:"billingUsernameVisa"`
|
||||
BillingPasswordVisa string `json:"billingPasswordVisa"`
|
||||
}
|
||||
|
||||
type UpdateBranchDTO struct {
|
||||
Title LocalizedText `json:"title" binding:"required"`
|
||||
ProvinceId int64 `json:"provinceId" binding:"required"`
|
||||
LocationId int64 `json:"locationId" binding:"required"`
|
||||
UnikalNomer string `json:"unikalNomer" binding:"required"`
|
||||
Address LocalizedText `json:"address" binding:"required"`
|
||||
BillingLogin string `json:"billingLogin"`
|
||||
BillingPassword string `json:"billingPassword"`
|
||||
BillingUsernameSber string `json:"billingUsernameSber"`
|
||||
BillingPasswordSber string `json:"billingPasswordSber"`
|
||||
BillingUsernameVisa string `json:"billingUsernameVisa"`
|
||||
BillingPasswordVisa string `json:"billingPasswordVisa"`
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
ProvinceID *int64
|
||||
LocationID *int64
|
||||
ProvinceName string
|
||||
BranchName string
|
||||
UnikalNomer string
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type ListBranchDTO struct {
|
||||
Count int64 `json:"count"`
|
||||
Branches []BranchDTO `json:"branches"`
|
||||
}
|
||||
283
internal/admin/branches/handler.go
Normal file
283
internal/admin/branches/handler.go
Normal file
@@ -0,0 +1,283 @@
|
||||
package branches
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
const baseURL = "/branches"
|
||||
|
||||
type handler struct {
|
||||
logger *logging.Logger
|
||||
repository Repository
|
||||
}
|
||||
|
||||
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
||||
return &handler{logger: logger, repository: repository}
|
||||
}
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
||||
group.GET("", h.list)
|
||||
group.GET("/:id", h.getByID)
|
||||
group.POST("", h.create)
|
||||
group.PUT("/:id", h.update)
|
||||
group.DELETE("/:id", h.delete)
|
||||
}
|
||||
|
||||
// list godoc
|
||||
// @Summary List branches
|
||||
// @Description Returns paginated branches with optional filters
|
||||
// @Tags branches
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param province_id query int false "Filter by province ID"
|
||||
// @Param location_id query int false "Filter by location ID"
|
||||
// @Param province_name query string false "Filter by province name (en, ru, tm)"
|
||||
// @Param branch_name query string false "Filter by branch name (en, ru, tm)"
|
||||
// @Param unikal_nomer query string false "Filter by unique number"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches [get]
|
||||
func (h *handler) list(c *gin.Context) {
|
||||
filter, ok := parseListFilter(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.repository.List(c.Request.Context(), filter)
|
||||
if err != nil {
|
||||
h.logger.Errorf("list branches failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
||||
}
|
||||
|
||||
// getByID godoc
|
||||
// @Summary Get branch by ID
|
||||
// @Description Returns a single branch
|
||||
// @Tags branches
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Branch ID"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches/{id} [get]
|
||||
func (h *handler) getByID(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
h.logger.Errorf("get branch failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// create godoc
|
||||
// @Summary Create branch
|
||||
// @Description Creates a new branch
|
||||
// @Tags branches
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param body body CreateBranchDTO true "Branch payload"
|
||||
// @Success 201 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches [post]
|
||||
func (h *handler) create(c *gin.Context) {
|
||||
var dto CreateBranchDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Create(c.Request.Context(), dto)
|
||||
if err != nil {
|
||||
if isClientError(err) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
h.logger.Errorf("create branch failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// update godoc
|
||||
// @Summary Update branch
|
||||
// @Description Updates an existing branch
|
||||
// @Tags branches
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Branch ID"
|
||||
// @Param body body UpdateBranchDTO true "Branch payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches/{id} [put]
|
||||
func (h *handler) update(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdateBranchDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Update(c.Request.Context(), id, dto)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
if isClientError(err) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
h.logger.Errorf("update branch failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// delete godoc
|
||||
// @Summary Delete branch
|
||||
// @Description Deletes a branch
|
||||
// @Tags branches
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Branch ID"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches/{id} [delete]
|
||||
func (h *handler) delete(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
} else if err != nil {
|
||||
h.logger.Errorf("delete branch failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData("branch removed"))
|
||||
}
|
||||
|
||||
func isClientError(err error) bool {
|
||||
msg := err.Error()
|
||||
return msg == "location not found in province" || msg == "unikal nomer already exists"
|
||||
}
|
||||
|
||||
func parseID(raw string) (int64, bool) {
|
||||
id, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
const (
|
||||
defaultPage = 1
|
||||
defaultLimit = 10
|
||||
maxLimit = 100
|
||||
)
|
||||
|
||||
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter := ListFilter{
|
||||
Page: defaultPage,
|
||||
Limit: defaultLimit,
|
||||
}
|
||||
|
||||
if raw := c.Query("province_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.ProvinceID = &id
|
||||
}
|
||||
|
||||
if raw := c.Query("location_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.LocationID = &id
|
||||
}
|
||||
|
||||
if raw := c.Query("page"); raw != "" {
|
||||
page, err := strconv.Atoi(raw)
|
||||
if err != nil || page < 1 {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Page = page
|
||||
}
|
||||
|
||||
if raw := c.Query("limit"); raw != "" {
|
||||
limit, err := strconv.Atoi(raw)
|
||||
if err != nil || limit < 1 || limit > maxLimit {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Limit = limit
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("province_name")); raw != "" {
|
||||
filter.ProvinceName = raw
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("branch_name")); raw != "" {
|
||||
filter.BranchName = raw
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("unikal_nomer")); raw != "" {
|
||||
filter.UnikalNomer = raw
|
||||
}
|
||||
|
||||
return filter, true
|
||||
}
|
||||
11
internal/admin/branches/storage.go
Normal file
11
internal/admin/branches/storage.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package branches
|
||||
|
||||
import "context"
|
||||
|
||||
type Repository interface {
|
||||
List(ctx context.Context, filter ListFilter) (ListBranchDTO, error)
|
||||
GetByID(ctx context.Context, id int64) (BranchDTO, error)
|
||||
Create(ctx context.Context, dto CreateBranchDTO) (BranchDTO, error)
|
||||
Update(ctx context.Context, id int64, dto UpdateBranchDTO) (BranchDTO, error)
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
Reference in New Issue
Block a user