120 lines
3.4 KiB
Go
Executable File
120 lines
3.4 KiB
Go
Executable File
package api
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"git.zhouxhere.com/zhouxhere/maptile/model"
|
||
"github.com/labstack/echo/v4"
|
||
)
|
||
|
||
// GetFeatures 获取要素列表
|
||
// @Summary 获取要素列表
|
||
// @Description 获取要素列表
|
||
// @Tags feature
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param page query int false "页码"
|
||
// @Param size query int false "每页数量"
|
||
// @Success 200 {object} Pagination[FeaturePublic]
|
||
// @Router /features [get]
|
||
// GetFeatures 处理获取特征列表的请求
|
||
func (a *API) GetFeatures(c echo.Context) error {
|
||
// 初始化特征列表请求参数,默认页码为1,每页大小为10
|
||
var featureList = model.FeatureList{
|
||
ListQuery: model.ListQuery{
|
||
Page: 1,
|
||
Size: 10,
|
||
},
|
||
}
|
||
// 绑定请求参数并验证
|
||
if err := BindAndValidate(c, &featureList); err != nil {
|
||
// 如果绑定或验证失败,返回400错误
|
||
return Error(c, http.StatusBadRequest, err.Error())
|
||
}
|
||
// 调用存储层获取特征列表和总数
|
||
features, total, err := a.store.ListFeature(&featureList)
|
||
if err != nil {
|
||
// 如果存储层操作失败,返回500错误
|
||
return Error(c, http.StatusInternalServerError, err.Error())
|
||
}
|
||
// 将 features 转换为 FeaturePublic
|
||
featurePublics := make([]model.FeaturePublic, len(features))
|
||
for i, feature := range features {
|
||
featurePublics[i] = feature.ToPublic()
|
||
}
|
||
return Paginate(c, featurePublics, featureList.Page, featureList.Size, total, "获取要素列表成功")
|
||
}
|
||
|
||
// PostFeature 创建要素
|
||
// @Summary 创建要素
|
||
// @Description 创建要素
|
||
// @Tags feature
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param feature body store.FeatureCreate true "要素"
|
||
// @Success 200 {object} Response[FeaturePublic]
|
||
// @Router /feature [post]
|
||
func (a *API) PostFeature(c echo.Context) error {
|
||
var featureCreate model.FeatureCreate
|
||
if err := BindAndValidate(c, &featureCreate); err != nil {
|
||
return Error(c, http.StatusBadRequest, err.Error())
|
||
}
|
||
feature, err := a.store.CreateFeature(&featureCreate)
|
||
if err != nil {
|
||
return Error(c, http.StatusInternalServerError, err.Error())
|
||
}
|
||
return Success(c, feature.ToPublic(), "创建要素成功")
|
||
}
|
||
|
||
// GetFeatureByID 获取要素
|
||
// @Summary 获取要素
|
||
// @Description 获取要素
|
||
// @Tags feature
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param id path string true "要素 ID"
|
||
// @Success 200 {object} Response[FeaturePublic]
|
||
// @Router /feature/{id} [get]
|
||
func (a *API) GetFeatureByID(c echo.Context) error {
|
||
return nil
|
||
}
|
||
|
||
// PutFeature 更新要素
|
||
// @Summary 更新要素
|
||
// @Description 更新要素
|
||
// @Tags feature
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param feature body store.FeatureUpdate true "要素"
|
||
// @Success 200 {object} Response[FeaturePublic]
|
||
// @Router /feature [put]
|
||
func (a *API) PutFeature(c echo.Context) error {
|
||
return nil
|
||
}
|
||
|
||
// DeleteFeature 删除要素
|
||
// @Summary 删除要素
|
||
// @Description 删除要素
|
||
// @Tags feature
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param feature body store.FeatureDeleteOrBanned true "要素"
|
||
// @Success 200 {object} Response[FeaturePublic]
|
||
// @Router /feature/{id} [delete]
|
||
func (a *API) DeleteFeature(c echo.Context) error {
|
||
return nil
|
||
}
|
||
|
||
// BannedFeature 禁用要素
|
||
// @Summary 禁用要素
|
||
// @Description 禁用要素
|
||
// @Tags feature
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param feature body store.FeatureDeleteOrBanned true "要素"
|
||
// @Success 200 {object} Response[FeaturePublic]
|
||
// @Router /feature/banned/{id} [post]
|
||
func (a *API) BannedFeature(c echo.Context) error {
|
||
return nil
|
||
}
|