作者 tangxvhui

新创建文章内容

... ... @@ -61,6 +61,28 @@ type (
}
)
// 获取我的发文章记录
type (
MiniArticleSearchMeRequest {
AuthorId int64 `json:"-"`
}
MiniArticleSearchMeResponse {
Total int `json:"total"`
List []ArticleSearchMe `json:"list"`
}
ArticleSearchMe {
Id int64 `json:"id"` //id
Title string `json:"title"` //标题
Images []string `json:"images"` //图片
CreatedAt int64 `json:"createdAt"` //文章的创建日期
CountLove int `json:"countLove"` //点赞数量
CountComment int `json:"CountComment"` //评论数量
Show int `json:"show"` //是否隐藏 [0显示、1不显示]
}
)
// 小程序接口
@server(
prefix: v1/mini
... ...
... ... @@ -2,9 +2,11 @@ package article
import (
"context"
"strings"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
... ... @@ -33,6 +35,18 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
}
//TODO 获取人员信息
articleAuthor := domain.UserSimple{
Id: author.Id,
Name: author.Name,
Avatar: author.Avatar,
GroupId: 0,
Group: "",
Position: author.Position,
Company: "",
CompanyId: author.CompanyId,
}
//TODO 获取图片的尺寸大小
images := []domain.Image{}
for _, val := range req.Images {
... ... @@ -59,24 +73,110 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
}
//检查文章可被哪些人评论
whoReview := []int64{}
if len(req.WhoReview) > 0 && len(whoRead) > 0 {
whoReview = lo.Uniq(req.WhoReview)
// 检查 whoRead 是否 完全包含 whoReview
ok := lo.Every(whoRead, whoReview)
if !ok {
//有指定可查看人的情况
if len(whoRead) > 0 {
if len(whoReview) > 0 {
whoReview = lo.Uniq(req.WhoReview)
// 检查 whoRead 是否 完全包含 whoReview
ok := lo.Every(whoRead, whoReview)
if !ok {
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
if len(whoReview) == 0 {
//有指定可查看人 ,但未指定可评论人
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
if len(whoRead) > 0 && len(whoReview) == 0 {
//有指定可查看人 ,但未指定可评论人
return nil, xerr.NewErrMsg("文章可评论人设置错误")
//没有指定可查看人的情况
if len(whoRead) == 0 {
if len(whoReview) > 0 {
// 未指定可查看人(全员可看),有指定可评论人,
var u *domain.User
for _, val := range whoReview {
u, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, val)
if err != nil {
return nil, xerr.NewErrMsgErr("文章可评论人设置错误", err)
}
if u.CompanyId != author.CompanyId {
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
}
// if len(whoReview) == 0 {
// 未指定可查看人(全员可看),未指定可评论人 ,忽略判断
// }
}
//切分文章分段
sectionList := []domain.ArticleSection{}
for i := range req.Section {
strList := strings.Split(req.Section[i], "\n")
for i2 := range strList {
newStr := strings.TrimSpace(strList[i2])
if len(newStr) == 0 {
continue
}
newSection := domain.ArticleSection{
Id: 0,
CompanyId: author.CompanyId,
ArticleId: 0,
Content: newStr,
SortBy: len(sectionList),
TotalComment: 0,
}
sectionList = append(sectionList, newSection)
}
}
newArticle := &domain.Article{
Id: 0,
CompanyId: author.CompanyId,
AuthorId: author.Id,
Author: articleAuthor,
Title: req.Title,
Images: images,
WhoRead: whoRead,
WhoReview: whoReview,
Location: domain.Location{
Longitude: req.Location.Latitude,
Latitude: req.Location.Latitude,
Descript: req.Location.Descript,
},
TargetUser: domain.ArticleTargetAll,
}
if len(whoRead) > 0 {
newArticle.TargetUser = domain.ArticleTargetLimit
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
newArticle, err = l.svcCtx.ArticleRepository.Insert(ctx, c, newArticle)
if err != nil {
return xerr.NewErrMsgErr("创建文章失败", err)
}
for i := range sectionList {
sectionList[i].ArticleId = newArticle.Id
_, err = l.svcCtx.ArticleSectionRepository.Insert(ctx, c, &sectionList[i])
if err != nil {
return xerr.NewErrMsgErr("创建文章内容失败", err)
}
}
// 设置保存备份
backup := newArticle.MakeBackup(newArticle.Author, sectionList)
backup.Action = "新增"
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, backup)
if err != nil {
return xerr.NewErrMsgErr("创建文章内容失败", err)
}
return nil
}, true)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章失败", err)
}
// if len(whoRead) == 0 && len(whoReview) > 0 {
// 未指定可查看人(全员可看),有指定可评论人 ,忽略判断
// }
// if len(whoRead) == 0 && len(whoReview) == 0 {
// 未指定可查看人(全员可看),未指定可评论人 ,忽略判断
// }
resp = &types.MiniArticleCreateResponse{
Id: newArticle.Id,
}
return
}
... ...
... ... @@ -26,6 +26,7 @@ type Article struct {
Location domain.Location `gorm:"type:jsonb;serializer:json"` // 坐标
TargetUser int // 分发方式 0 分发给所有人 1 分发给指定的人
CountLove int // 点赞数量
CountRead int // 浏览数量
CountComment int // 评论数量
Show int // 评论的展示状态(0显示、1不显示)
}
... ... @@ -35,8 +36,9 @@ func (m *Article) TableName() string {
}
func (m *Article) BeforeCreate(tx *gorm.DB) (err error) {
m.CreatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
nowTime := time.Now().Unix()
m.CreatedAt = nowTime
m.UpdatedAt = nowTime
return
}
... ...
... ... @@ -24,7 +24,7 @@ type ArticleBackup struct {
Action string // 操作
WhoRead []int64 `gorm:"type:jsonb;serializer:json"` // 谁可以看
WhoReview []int64 `gorm:"type:jsonb;serializer:json"` // 评论人
Tags []int `gorm:"type:jsonb;serializer:json"` // 标签
Tags []int64 `gorm:"type:jsonb;serializer:json"` // 标签
TargetUser int // 分发方式 0 分发给所有人 1 分发给指定的人
}
... ...
... ... @@ -158,6 +158,7 @@ func (repository *ArticleRepository) ModelToDomainModel(from *models.Article) (*
TargetUser: domain.ArticleTarget(from.TargetUser),
CountLove: from.CountLove,
CountComment: from.CountComment,
CountRead: from.CountRead,
Show: domain.ArticleShow(from.Show),
}
return to, nil
... ... @@ -170,6 +171,7 @@ func (repository *ArticleRepository) DomainModelToModel(from *domain.Article) (*
CreatedAt: from.CreatedAt,
UpdatedAt: from.UpdatedAt,
DeletedAt: from.DeletedAt,
IsDel: 0,
Version: from.Version,
AuthorId: from.AuthorId,
Author: from.Author,
... ... @@ -180,6 +182,7 @@ func (repository *ArticleRepository) DomainModelToModel(from *domain.Article) (*
Location: from.Location,
TargetUser: int(from.TargetUser),
CountLove: from.CountLove,
CountRead: from.CountRead,
CountComment: from.CountComment,
Show: int(from.Show),
}
... ...
... ... @@ -24,10 +24,21 @@ type Article struct {
TargetUser ArticleTarget `json:"targetUser"` // 分发方式 0 分发给所有人 1 分发给指定的人
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
Show ArticleShow `json:"showState"` // 评论的展示状态(0显示、1不显示)
CountRead int `json:"countRead"` // 浏览数量
Show ArticleShow `json:"show"` // 评论的展示状态(0显示、1不显示)
Tags []int64 `json:"tags"`
// ...more
}
type ArticleRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
Update(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
Delete(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Article, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Article, error)
}
type ArticleTarget int
const (
... ... @@ -63,11 +74,23 @@ func (a ArticleShow) Named() string {
return ""
}
type ArticleRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
Update(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
Delete(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Article, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Article, error)
func (m *Article) MakeBackup(operator UserSimple, section []ArticleSection) *ArticleBackup {
b := ArticleBackup{
Id: 0,
CompanyId: 0,
CreatedAt: 0,
UpdatedAt: 0,
DeletedAt: 0,
Version: 0,
Operator: operator,
Title: m.Title,
Section: section,
Images: m.Images,
Action: "",
TargetUser: m.TargetUser,
WhoRead: m.WhoRead,
WhoReview: m.WhoReview,
Tags: m.Tags,
}
return &b
}
... ...
... ... @@ -22,7 +22,7 @@ type ArticleBackup struct {
TargetUser ArticleTarget `json:"targetUser"` // 分发方式 0 分发给所有人 1 分发给指定的人
WhoRead []int64 `json:"whoRead"` // 谁可以看
WhoReview []int64 `json:"whoReview"` // 评论人
Tags []int `json:"tags"` // 标签
Tags []int64 `json:"tags"` // 标签
}
type ArticleBackupRepository interface {
... ...
... ... @@ -28,3 +28,10 @@ type ArticleSectionRepository interface {
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleSection, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleSection, error)
}
// 排序文章分段列表
type SortArticleSection []*ArticleSection
func (a SortArticleSection) Len() int { return len(a) }
func (a SortArticleSection) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortArticleSection) Less(i, j int) bool { return a[i].SortBy < a[j].SortBy }
... ...
... ... @@ -29,9 +29,12 @@ type Opinion struct {
}
type UserSimple struct {
Id int64 `json:"id"` // 人员id
Name string `json:"name"` // 人员的名字
Avatar string `json:"avatar,omitempty"` // 人员头像URL
Group string `json:"group,omitempty"` // 人员的分组
Position string `json:"position,omitempty"` // 职位
Id int64 `json:"id"` // 人员id
Name string `json:"name"` // 人员的名字
Avatar string `json:"avatar,omitempty"` // 人员头像URL
GroupId int64 `json:"groupId,omitempty"`
Group string `json:"group,omitempty"` // 人员的分组
Position string `json:"position,omitempty"` // 职位
Company string `json:"company,omitempty"` // 公司
CompanyId int64 `json:"companyId"`
}
... ...