## 主要功能 - ✅ 用户认证模块 (登录/注册/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 构建脚本 服务已验证可正常构建和启动。
105 lines
3.2 KiB
Go
Executable File
105 lines
3.2 KiB
Go
Executable File
// Code generated by goctl. DO NOT EDIT.
|
|
// versions:
|
|
// goctl version: 1.8.4
|
|
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/builder"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
)
|
|
|
|
var (
|
|
categoryFieldNames = builder.RawFieldNames(&Category{})
|
|
categoryRows = strings.Join(categoryFieldNames, ",")
|
|
categoryRowsExpectAutoSet = strings.Join(stringx.Remove(categoryFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
|
categoryRowsWithPlaceHolder = strings.Join(stringx.Remove(categoryFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
|
)
|
|
|
|
type (
|
|
categoryModel interface {
|
|
Insert(ctx context.Context, data *Category) (sql.Result, error)
|
|
FindOne(ctx context.Context, id int64) (*Category, error)
|
|
FindOneByName(ctx context.Context, name string) (*Category, error)
|
|
Update(ctx context.Context, data *Category) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
defaultCategoryModel struct {
|
|
conn sqlx.SqlConn
|
|
table string
|
|
}
|
|
|
|
Category struct {
|
|
Id int64 `db:"id"`
|
|
Name string `db:"name"` // 分类名称
|
|
Description sql.NullString `db:"description"` // 分类描述
|
|
CreatedAt time.Time `db:"created_at"`
|
|
UpdatedAt time.Time `db:"updated_at"`
|
|
}
|
|
)
|
|
|
|
func newCategoryModel(conn sqlx.SqlConn) *defaultCategoryModel {
|
|
return &defaultCategoryModel{
|
|
conn: conn,
|
|
table: "`category`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultCategoryModel) Delete(ctx context.Context, id int64) error {
|
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
|
_, err := m.conn.ExecCtx(ctx, query, id)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultCategoryModel) FindOne(ctx context.Context, id int64) (*Category, error) {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", categoryRows, m.table)
|
|
var resp Category
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlx.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultCategoryModel) FindOneByName(ctx context.Context, name string) (*Category, error) {
|
|
var resp Category
|
|
query := fmt.Sprintf("select %s from %s where `name` = ? limit 1", categoryRows, m.table)
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, name)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlx.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultCategoryModel) Insert(ctx context.Context, data *Category) (sql.Result, error) {
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, categoryRowsExpectAutoSet)
|
|
ret, err := m.conn.ExecCtx(ctx, query, data.Name, data.Description)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultCategoryModel) Update(ctx context.Context, newData *Category) error {
|
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, categoryRowsWithPlaceHolder)
|
|
_, err := m.conn.ExecCtx(ctx, query, newData.Name, newData.Description, newData.Id)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultCategoryModel) tableName() string {
|
|
return m.table
|
|
}
|