feat: 完成后端服务核心业务逻辑实现
## 主要功能 - ✅ 用户认证模块 (登录/注册/JWT) - ✅ 照片管理模块 (上传/查询/分页/搜索) - ✅ 分类管理模块 (创建/查询/分页) - ✅ 用户管理模块 (用户列表/分页查询) - ✅ 健康检查接口 ## 技术实现 - 基于 go-zero v1.8.0 标准架构 - Handler → Logic → Model 三层架构 - SQLite/PostgreSQL 数据库支持 - JWT 认证机制 - bcrypt 密码加密 - 统一响应格式 - 自定义模型方法 (分页/搜索) ## API 接口 - POST /api/v1/auth/login - 用户登录 - POST /api/v1/auth/register - 用户注册 - GET /api/v1/health - 健康检查 - GET /api/v1/photos - 照片列表 - POST /api/v1/photos - 上传照片 - GET /api/v1/categories - 分类列表 - POST /api/v1/categories - 创建分类 - GET /api/v1/users - 用户列表 ## 配置完成 - 开发环境配置 (SQLite) - 生产环境支持 (PostgreSQL) - JWT 认证配置 - 文件上传配置 - Makefile 构建脚本 服务已验证可正常构建和启动。
This commit is contained in:
81
backend/internal/model/categorymodel.go
Executable file
81
backend/internal/model/categorymodel.go
Executable file
@ -0,0 +1,81 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ CategoryModel = (*customCategoryModel)(nil)
|
||||
|
||||
type (
|
||||
// CategoryModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customCategoryModel.
|
||||
CategoryModel interface {
|
||||
categoryModel
|
||||
withSession(session sqlx.Session) CategoryModel
|
||||
FindList(ctx context.Context, page, pageSize int, keyword string) ([]*Category, error)
|
||||
Count(ctx context.Context, keyword string) (int64, error)
|
||||
}
|
||||
|
||||
customCategoryModel struct {
|
||||
*defaultCategoryModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewCategoryModel returns a model for the database table.
|
||||
func NewCategoryModel(conn sqlx.SqlConn) CategoryModel {
|
||||
return &customCategoryModel{
|
||||
defaultCategoryModel: newCategoryModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customCategoryModel) withSession(session sqlx.Session) CategoryModel {
|
||||
return NewCategoryModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
|
||||
// FindList 分页查询分类列表
|
||||
func (m *customCategoryModel) FindList(ctx context.Context, page, pageSize int, keyword string) ([]*Category, error) {
|
||||
var conditions []string
|
||||
var args []interface{}
|
||||
|
||||
if keyword != "" {
|
||||
conditions = append(conditions, "(`name` LIKE ? OR `description` LIKE ?)")
|
||||
args = append(args, "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
whereClause := ""
|
||||
if len(conditions) > 0 {
|
||||
whereClause = " WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
args = append(args, pageSize, offset)
|
||||
|
||||
query := fmt.Sprintf("select %s from %s%s ORDER BY `created_at` DESC LIMIT ? OFFSET ?", categoryRows, m.table, whereClause)
|
||||
var resp []*Category
|
||||
err := m.conn.QueryRowsCtx(ctx, &resp, query, args...)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Count 统计分类数量
|
||||
func (m *customCategoryModel) Count(ctx context.Context, keyword string) (int64, error) {
|
||||
var conditions []string
|
||||
var args []interface{}
|
||||
|
||||
if keyword != "" {
|
||||
conditions = append(conditions, "(`name` LIKE ? OR `description` LIKE ?)")
|
||||
args = append(args, "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
whereClause := ""
|
||||
if len(conditions) > 0 {
|
||||
whereClause = " WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("select count(*) from %s%s", m.table, whereClause)
|
||||
var count int64
|
||||
err := m.conn.QueryRowCtx(ctx, &count, query, args...)
|
||||
return count, err
|
||||
}
|
||||
Reference in New Issue
Block a user