更新结构
29
server/internal/articlemodel/articlemodel.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package articlemodel
|
||||
|
||||
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/articlemodel/articlemodel_gen.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.9.1
|
||||
|
||||
package articlemodel
|
||||
|
||||
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/articlemodel/vars.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package articlemodel
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
@@ -2,7 +2,7 @@ package router
|
||||
|
||||
import (
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
handler "github.com/JACKYMYPERSON/hldrCenter/handler/uploadimg"
|
||||
handler "github.com/JACKYMYPERSON/hldrCenter/internal/handler/uploadimg"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
16
server/sql/article.sql
Normal file
@@ -0,0 +1,16 @@
|
||||
DROP TABLE IF EXISTS `article`;
|
||||
CREATE TABLE `article` (
|
||||
`title` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
|
||||
`content` text CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
|
||||
`cover` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
|
||||
`create_at` datetime NOT NULL,
|
||||
`update_at` datetime NOT NULL,
|
||||
`is_delete` tinyint NOT NULL,
|
||||
`topic` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
|
||||
`excerpt` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
|
||||
`id` bigint NOT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
|
||||
-- @table:article -- 声明表名为article
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 4.7 KiB |
|
Before Width: | Height: | Size: 44 KiB |
|
Before Width: | Height: | Size: 165 KiB |
|
Before Width: | Height: | Size: 6.4 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 7.2 KiB |
|
Before Width: | Height: | Size: 7.2 KiB |
|
Before Width: | Height: | Size: 7.2 KiB |
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 6.8 KiB |
|
Before Width: | Height: | Size: 6.8 KiB |
|
Before Width: | Height: | Size: 4.4 KiB |
|
Before Width: | Height: | Size: 6.0 KiB |
|
Before Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 22 KiB |