用DDD分项目模块
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/logic/article"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func CreateArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.CreateArticleReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := article.NewCreateArticleLogic(r.Context(), svcCtx)
|
||||
resp, err := l.CreateArticle(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/logic/article"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func DeleteArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DeleteArticleReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := article.NewDeleteArticleLogic(r.Context(), svcCtx)
|
||||
resp, err := l.DeleteArticle(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/logic/article"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func DetailArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DetailArticleReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := article.NewDetailArticleLogic(r.Context(), svcCtx)
|
||||
resp, err := l.DetailArticle(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/logic/article"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func ListArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.ListArticleReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := article.NewListArticleLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ListArticle(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/logic/article"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func UpdateArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateArticleReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := article.NewUpdateArticleLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UpdateArticle(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CreateArticleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCreateArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateArticleLogic {
|
||||
return &CreateArticleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateArticleLogic) CreateArticle(req *types.CreateArticleReq) (resp *types.CreateArticleResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeleteArticleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteArticleLogic {
|
||||
return &DeleteArticleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteArticleLogic) DeleteArticle(req *types.DeleteArticleReq) (resp *types.DeleteArticleResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DetailArticleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDetailArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailArticleLogic {
|
||||
return &DetailArticleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DetailArticleLogic) DetailArticle(req *types.DetailArticleReq) (resp *types.DetailArticleResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ListArticleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewListArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListArticleLogic {
|
||||
return &ListArticleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ListArticleLogic) ListArticle(req *types.ListArticleReq) (resp *types.ListArticleResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package article
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateArticleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateArticleLogic {
|
||||
return &UpdateArticleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateArticleLogic) UpdateArticle(req *types.UpdateArticleReq) (resp *types.UpdateArticleResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
29
server/internal/article/internal/model/articlemodel.go
Normal file
29
server/internal/article/internal/model/articlemodel.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var _ ArticleModel = (*customArticleModel)(nil)
|
||||
|
||||
type (
|
||||
// ArticleModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customArticleModel.
|
||||
ArticleModel interface {
|
||||
articleModel
|
||||
withSession(session sqlx.Session) ArticleModel
|
||||
}
|
||||
|
||||
customArticleModel struct {
|
||||
*defaultArticleModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewArticleModel returns a model for the database table.
|
||||
func NewArticleModel(conn sqlx.SqlConn) ArticleModel {
|
||||
return &customArticleModel{
|
||||
defaultArticleModel: newArticleModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customArticleModel) withSession(session sqlx.Session) ArticleModel {
|
||||
return NewArticleModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
93
server/internal/article/internal/model/articlemodel_gen.go
Normal file
93
server/internal/article/internal/model/articlemodel_gen.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.9.1
|
||||
|
||||
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 (
|
||||
articleFieldNames = builder.RawFieldNames(&Article{})
|
||||
articleRows = strings.Join(articleFieldNames, ",")
|
||||
articleRowsExpectAutoSet = strings.Join(stringx.Remove(articleFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
articleRowsWithPlaceHolder = strings.Join(stringx.Remove(articleFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
articleModel interface {
|
||||
Insert(ctx context.Context, data *Article) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*Article, error)
|
||||
Update(ctx context.Context, data *Article) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultArticleModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
Article struct {
|
||||
Title string `db:"title"`
|
||||
Content string `db:"content"`
|
||||
Cover string `db:"cover"`
|
||||
CreateAt time.Time `db:"create_at"`
|
||||
UpdateAt time.Time `db:"update_at"`
|
||||
IsDelete int64 `db:"is_delete"`
|
||||
Topic string `db:"topic"`
|
||||
Excerpt string `db:"excerpt"`
|
||||
Id int64 `db:"id"`
|
||||
}
|
||||
)
|
||||
|
||||
func newArticleModel(conn sqlx.SqlConn) *defaultArticleModel {
|
||||
return &defaultArticleModel{
|
||||
conn: conn,
|
||||
table: "`article`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultArticleModel) 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 *defaultArticleModel) FindOne(ctx context.Context, id int64) (*Article, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", articleRows, m.table)
|
||||
var resp Article
|
||||
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 *defaultArticleModel) Insert(ctx context.Context, data *Article) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, articleRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.Content, data.Cover, data.IsDelete, data.Topic, data.Excerpt, data.Id)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultArticleModel) Update(ctx context.Context, data *Article) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, articleRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.Title, data.Content, data.Cover, data.IsDelete, data.Topic, data.Excerpt, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultArticleModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
5
server/internal/article/internal/model/vars.go
Normal file
5
server/internal/article/internal/model/vars.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
69
server/internal/article/internal/types/types.go
Normal file
69
server/internal/article/internal/types/types.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.1
|
||||
|
||||
package types
|
||||
|
||||
type Article struct {
|
||||
Id int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Cover string `json:"cover"`
|
||||
CreateAt string `json:"create_at"` // 前端通常用字符串承载时间,可根据需求改为 time.Time
|
||||
UpdateAt string `json:"update_at"`
|
||||
IsDelete int `json:"is_delete"` // tinyint 映射为 int,1=已删除,0=未删除
|
||||
Topic string `json:"topic"`
|
||||
Excerpt string `json:"excerpt"`
|
||||
}
|
||||
|
||||
type CreateArticleReq struct {
|
||||
Title string `json:"title"` // 文章标题
|
||||
Content string `json:"content"` // 文章内容
|
||||
Cover string `json:"cover"` // 封面图
|
||||
Topic string `json:"topic"` // 话题
|
||||
}
|
||||
|
||||
type CreateArticleResp struct {
|
||||
Id int64 `json:"id"` // 新增文章的 ID
|
||||
Success bool `json:"success"` // 操作是否成功
|
||||
}
|
||||
|
||||
type DeleteArticleReq struct {
|
||||
Id int64 `path:"id"` // 要删除的文章 ID
|
||||
}
|
||||
|
||||
type DeleteArticleResp struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
type DetailArticleReq struct {
|
||||
Id int64 `path:"id"` // 从路径参数取 ID,如 /api/articles/:id
|
||||
}
|
||||
|
||||
type DetailArticleResp struct {
|
||||
Article Article `json:"article"` // 文章详情
|
||||
Success bool `json:"success"` // 操作是否成功
|
||||
}
|
||||
|
||||
type ListArticleReq struct {
|
||||
Page int `json:"page,optional"` // 页码(可选,默认第一页)
|
||||
Size int `json:"size,optional"` // 每页条数(可选,默认 10 条)
|
||||
Topic string `json:"topic,optional"` // 按话题筛选(可选)
|
||||
}
|
||||
|
||||
type ListArticleResp struct {
|
||||
Total int64 `json:"total"` // 总条数
|
||||
List []Article `json:"list"` // 文章列表
|
||||
}
|
||||
|
||||
type UpdateArticleReq struct {
|
||||
Id int64 `path:"id"` // 要更新的文章 ID
|
||||
Title string `json:"title,optional"`
|
||||
Content string `json:"content,optional"`
|
||||
Cover string `json:"cover,optional"`
|
||||
Topic string `json:"topic,optional"`
|
||||
Excerpt string `json:"excerpt,optional"`
|
||||
}
|
||||
|
||||
type UpdateArticleResp struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
Reference in New Issue
Block a user