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:
93
backend/internal/model/photomodel_gen.go
Executable file
93
backend/internal/model/photomodel_gen.go
Executable file
@ -0,0 +1,93 @@
|
||||
// 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 (
|
||||
photoFieldNames = builder.RawFieldNames(&Photo{})
|
||||
photoRows = strings.Join(photoFieldNames, ",")
|
||||
photoRowsExpectAutoSet = strings.Join(stringx.Remove(photoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
photoRowsWithPlaceHolder = strings.Join(stringx.Remove(photoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
photoModel interface {
|
||||
Insert(ctx context.Context, data *Photo) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*Photo, error)
|
||||
Update(ctx context.Context, data *Photo) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultPhotoModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
Photo struct {
|
||||
Id int64 `db:"id"`
|
||||
Title string `db:"title"` // 照片标题
|
||||
Description sql.NullString `db:"description"` // 照片描述
|
||||
FilePath string `db:"file_path"` // 原图路径
|
||||
ThumbnailPath string `db:"thumbnail_path"` // 缩略图路径
|
||||
UserId int64 `db:"user_id"` // 用户ID
|
||||
CategoryId int64 `db:"category_id"` // 分类ID
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
)
|
||||
|
||||
func newPhotoModel(conn sqlx.SqlConn) *defaultPhotoModel {
|
||||
return &defaultPhotoModel{
|
||||
conn: conn,
|
||||
table: "`photo`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultPhotoModel) 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 *defaultPhotoModel) FindOne(ctx context.Context, id int64) (*Photo, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", photoRows, m.table)
|
||||
var resp Photo
|
||||
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 *defaultPhotoModel) Insert(ctx context.Context, data *Photo) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, photoRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.Description, data.FilePath, data.ThumbnailPath, data.UserId, data.CategoryId)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultPhotoModel) Update(ctx context.Context, data *Photo) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, photoRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.Title, data.Description, data.FilePath, data.ThumbnailPath, data.UserId, data.CategoryId, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultPhotoModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
Reference in New Issue
Block a user