1 Star 0 Fork 0

肖金光 / goclean

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
blog.go 5.88 KB
一键复制 编辑 原始数据 按行查看 历史
xiaojg-mbp 提交于 2024-03-14 09:16 . go11补充 protobuf项目
package impl
import (
"context"
"net/http"
"time"
"gitee.com/aviptle/goclean/go11jin/vblog01/apps/blog"
"gitee.com/aviptle/goclean/go11jin/vblog01/common"
"gitee.com/aviptle/goclean/go11jin/vblog01/logger"
)
// ctx: Context上下文, 一个接口或者函数, 他的参数保护2类数据:
// 1. 用户传递过来的数据: in *blog.Blog
// 2. 还有1类数据 不是通过用户传递的, 比如Trace, RequestId
func (i *impl) CreateBlog(ctx context.Context, in *blog.CreateBlogRequest) (
*blog.Blog, error) {
// 检查用户的参数
if err := in.Validate(); err != nil {
// 我这里就想返回一个业务异常?
// 当前Error里面不保护异常码信息?
return nil, common.NewApiException(http.StatusBadRequest, err.Error())
}
// 生成一个blog对象
// ins := blog.Blog{
// Meta: &blog.Meta{},
// CreateBlogRequest: &blog.CreateBlogRequest{},
// }
// ins.CreateBlogRequest.Extra 这个map 就没有初始化, 很容易报空指针错误
// 代码不具有可兼容
// 使用构造函数来创建一个对象
ins := blog.NewBlog(in)
// 直接和数据库交互
// 尽量不要直接这样写? 比如用户提交一个请求
// INSERT INTO `blogs` (`created_at`,`updated_at`,`published_at`,`title`,`author`,`content`,`tags`,`status`)
// VALUES (1684566571,1684566571,0,'test','test','test','{}','0')
err := i.db.WithContext(ctx).Save(ins).Error
if err != nil {
return nil, common.NewApiException(http.StatusInternalServerError, err.Error())
}
//
// go func() {
// <-ctx.Done()
// // 退出
// // 取消该次数据库操作
// }()
return ins, nil
}
// 查询文章列表
func (i *impl) QueryBlog(ctx context.Context, in *blog.QueryBlogRequest) (
*blog.BlogSet, error) {
// 提前构造一个BlogSet对象出来
set := blog.NewBlogSet()
// 需要构造SQL查询
query := i.db.WithContext(ctx).Model(&blog.Blog{})
// 模糊查询
if in.Keywords != "" {
// %xxxx% ---> *xxxx*
// go项目教学 --> 项目教学 (%项目教学%)
query = query.Where("content LIKE ?", "%"+in.Keywords+"%")
}
if in.Author != "" {
query = query.Where("author = ?", in.Author)
}
// 分页是必传参数? offset 忽略前面多少个,获取多少个数据
// 数据是一个[]*Blog
err := query.
// COUNT(*) WHERE xxx OFFSET LIMIT
Count(&set.Total).
Offset(in.Offset()).
Limit(in.PageSize).
// SELECT * FORM ....
// SELECT * FROM `blogs` LIMIT 20
// SELECT * FROM `blogs` WHERE content LIKE '%项目教学%' LIMIT 20
// MySQL的教程对SQL基本使用有所了解
Find(&set.Items).
Error
if err != nil {
return nil, err
}
return set, nil
}
// 查询单个文章
func (i *impl) DescribeBlog(ctx context.Context, d *blog.DescribeBlogRequest) (
*blog.Blog, error) {
// 检查用户的参数
// fmt.Println(d.Id)
if err := d.Validate(); err != nil {
return nil, common.NewApiException(http.StatusBadRequest, err.Error())
}
// 提前构造一个blog.Blog对象出来
// set := blog.NewDescribeBlogRequest()
req := blog.NewCreateBlogRequest()
set := blog.NewBlog(req)
// in := blog.DescribeBlogRequest{}
// 查数据库,需要构造SQL查询
err := i.db.WithContext(ctx).
// Model(&blog.Blog{}).
// Where("id = ?", d.Id).
Find(set, d.Id).Error
if err != nil {
return nil, err
}
return set, nil
}
// 更新文章
func (i *impl) UpdateBlog(ctx context.Context, u *blog.UpdateBlogRequest) (
*blog.Blog, error) {
if u.UpdateModeStatus == blog.UPDATE_MODE_PUT {
// 检查用户的参数
if err := u.Validate(); err != nil {
return nil, common.NewApiException(http.StatusBadRequest, err.Error())
}
}
// 通过 id 查询现有的博客 Blog结构体信息
ins := blog.NewBlog(blog.NewCreateBlogRequest())
query := i.db.WithContext(ctx)
err := query.Find(ins, u.Id).Error
if err != nil {
return nil, common.NewApiException(http.StatusInternalServerError, err.Error())
}
// 把createBlogRequest结构体中的author,content 更新到Blog中,同时更新 updated_at字段
if u.UpdateModeStatus == blog.UPDATE_MODE_PUT {
ins, err = i.UpdateBlogPut(ins, u)
if err != nil {
return nil, common.NewApiException(http.StatusInternalServerError, err.Error())
}
} else if u.UpdateModeStatus == blog.UPDATE_MODE_PATCH {
ins, err = i.UpdateBlogPatch(ins, u)
if err != nil {
return nil, common.NewApiException(http.StatusInternalServerError, err.Error())
}
}
// 保存入库
err = i.db.WithContext(ctx).Save(ins).Error
if err != nil {
return nil, common.NewApiException(http.StatusInternalServerError, err.Error())
}
// 结果响应回浏览器
return ins, nil
}
func (i *impl) UpdateBlogPut(ins *blog.Blog, u *blog.UpdateBlogRequest) (*blog.Blog, error) {
ins.Title = u.Title
ins.Author = u.Author
ins.Content = u.Content
ins.UpdatedAt = time.Now().Unix()
return ins, nil
}
func (i *impl) UpdateBlogPatch(ins *blog.Blog, u *blog.UpdateBlogRequest) (*blog.Blog, error) {
ins.UpdatedAt = time.Now().Unix()
logger.L().Debug().Msgf("login user: %v", u.Content)
var flag = true
if u.Title != "" {
ins.Title = u.Title
flag = false
// return ins, nil
}
if u.Author != "" {
ins.Author = u.Author
flag = false
// return ins, nil
}
if u.Content != "" {
ins.Content = u.Content
flag = false
// return ins, nil
}
if flag {
return nil, common.NewApiException(http.StatusBadRequest, "title/author/content不能都为空")
}
return ins, nil
}
// 删除文章, 返回删除的对象, 用前端提升, 用于对象最终
func (i *impl) DeleteBlog(ctx context.Context, d *blog.DeleteBlogRequest) (
*blog.Blog, error) {
ins := &blog.Blog{}
// 查询id是否存在
err := i.db.WithContext(ctx).First(ins, d.Id).Error
if err != nil {
return nil, common.NewApiException(http.StatusInternalServerError, err.Error())
}
// 删除id
tx := i.db.WithContext(ctx).Where("id = ?", d.Id).Delete(ins)
if tx.Error != nil {
return nil, common.NewApiException(http.StatusInternalServerError, tx.Error.Error())
}
return ins, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/aviptle/goclean.git
git@gitee.com:aviptle/goclean.git
aviptle
goclean
goclean
6b9efd2ceda8

搜索帮助

344bd9b3 5694891 D2dac590 5694891