作者 tangxvhui

更新接口

... ... @@ -30,6 +30,10 @@ service Core {
@handler MiniSetUserLike
post /article/user_like/set (MiniSetUserLikeRequset) returns (MiniSetUserLikeResponse)
@doc "小程序标记当前人员查看的文章"
@handler MiniArticleMarkUserRead
post /article/mark/user_read (MiniArticleMarkUserReadRequest) returns (MiniArticleMarkUserReadResponse)
@doc "小程序获取我发布的文章"
@handler MiniArticleSearchMe
post /article/search/me (MiniArticleSearchMeRequest) returns (MiniArticleSearchMeResponse)
... ...
... ... @@ -148,6 +148,18 @@ type (
}
)
type (
MiniArticleMarkUserReadRequest {
UserId int64 `json:",optional"` // 当前操作人
CompanyId int64 `json:",optional"` // 当前公司
ArticleId int64 `json:"articleId"` // 文章id
}
MiniArticleMarkUserReadResponse {
Id int64 `json:"id"`
}
)
//管理后台获取文章详情
type (
SystemArticleGetRequest {
... ...
package article
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article"
"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/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
)
// 标记人员当前浏览的文章
func MiniArticleMarkUserReadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniArticleMarkUserReadRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := article.NewMiniArticleMarkUserReadLogic(r.Context(), svcCtx)
token := contextdata.GetUserTokenFromCtx(r.Context())
req.CompanyId = token.CompanyId
req.UserId = token.UserId
resp, err := l.MiniArticleMarkUserRead(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
... ... @@ -210,6 +210,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/article/mark/user_read",
Handler: article.MiniArticleMarkUserReadHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/article/search/me",
Handler: article.MiniArticleSearchMeHandler(serverCtx),
},
... ...
package article
import (
"context"
"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"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniArticleMarkUserReadLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniArticleMarkUserReadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniArticleMarkUserReadLogic {
return &MiniArticleMarkUserReadLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniArticleMarkUserReadLogic) MiniArticleMarkUserRead(req *types.MiniArticleMarkUserReadRequest) (resp *types.MiniArticleMarkUserReadResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
articleData, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
if err != nil {
return nil, xerr.NewErrMsgErr("标记浏览记录失败", err)
}
if articleData.CompanyId != req.CompanyId {
return nil, xerr.NewErrMsg("标记浏览记录失败")
}
//查找旧的记录
queryOption := domain.NewQueryOptions().
WithFindOnly().
WithOffsetLimit(1, 1).
MustWithKV("articleId", req.ArticleId).
MustWithKV("userId", req.UserId)
_, markRecord, err := l.svcCtx.UserReadArticleRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("标记浏览记录失败", err)
}
if len(markRecord) > 0 {
_, err = l.svcCtx.UserReadArticleRepository.Update(l.ctx, conn, markRecord[0])
if err != nil {
return nil, xerr.NewErrMsgErr("标记浏览记录失败", err)
}
resp = &types.MiniArticleMarkUserReadResponse{Id: markRecord[0].Id}
return resp, nil
}
// 新建一个标记
articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
if err != nil {
return nil, xerr.NewErrMsgErr("标记浏览记录失败", err)
}
if articleInfo.CompanyId != req.CompanyId {
return nil, xerr.NewErrMsg("标记浏览记录失败")
}
newMark := domain.UserReadArticle{
Id: 0,
CompanyId: req.CompanyId,
UserId: req.UserId,
ArticleId: articleData.Id,
Title: articleData.Title,
Author: articleData.Author,
CreatedAt: 0,
UpdatedAt: 0,
DeletedAt: 0,
Version: 0,
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
_, err = l.svcCtx.UserReadArticleRepository.Insert(ctx, c, &newMark)
if err != nil {
return err
}
err = l.svcCtx.ArticleRepository.IncreaseCountRead(ctx, c, 1, articleInfo)
return err
}, true)
if err != nil {
return nil, xerr.NewErrMsg("标记浏览记录失败")
}
resp = &types.MiniArticleMarkUserReadResponse{Id: newMark.Id}
return
}
... ...
... ... @@ -194,7 +194,7 @@ func (l *MiniSetUserLikeLogic) setUserLikeArticle(req *types.MiniSetUserLikeRequ
// 检查id
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId)
if err != nil {
//无法确认 文章的id
//无法确认 操作人id
return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err)
}
articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
... ...
... ... @@ -31,6 +31,7 @@ type ServiceContext struct {
RoleRepository domain.RoleRepository
UserFollowRepository domain.UserFollowRepository
UserLoveFlagRepository domain.UserLoveFlagRepository
UserReadArticleRepository domain.UserReadArticleRepository
UserRepository domain.UserRepository
}
... ... @@ -59,6 +60,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
UserFollowRepository: repository.NewUserFollowRepository(cache.NewCachedRepository(mlCache)),
UserLoveFlagRepository: repository.NewUserLoveFlagRepository(cache.NewCachedRepository(mlCache)),
UserRepository: repository.NewUserRepository(cache.NewCachedRepository(mlCache)),
UserReadArticleRepository: repository.NewUserReadArticleRepository(cache.NewCachedRepository(mlCache)),
ArticleTagRepository: repository.NewArticleTagRepository(cache.NewCachedRepository(mlCache)),
}
}
... ...
... ... @@ -449,6 +449,16 @@ type MiniArticleBackupItem struct {
Location Location `json:"location"`
}
type MiniArticleMarkUserReadRequest struct {
UserId int64 `json:",optional"` // 当前操作人
CompanyId int64 `json:",optional"` // 当前公司
ArticleId int64 `json:"articleId"` // 文章id
}
type MiniArticleMarkUserReadResponse struct {
Id int64 `json:"id"`
}
type SystemArticleGetRequest struct {
Id int64 `path:"id"` //id
CompanyId int64 `path:",optional"`
... ...
... ... @@ -14,6 +14,7 @@ func Migrate(db *gorm.DB) {
&models.ArticleComment{},
&models.ArticleTag{},
&models.UserLoveFlag{},
&models.UserReadArticle{},
&models.User{},
&models.Role{},
&models.Company{},
... ...
package models
import (
"fmt"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type UserReadArticle struct {
Id int64 `gorm:"primaryKey"` // 唯一标识
CreatedAt int64
UpdatedAt int64
DeletedAt int64
Version int
CompanyId int64
UserId int64
ArticleId int64
Title string
Author domain.UserSimple `gorm:"type:jsonb;serializer:json"` // 发布人
}
func (m *UserReadArticle) TableName() string {
return "user_read_article"
}
func (m *UserReadArticle) BeforeCreate(tx *gorm.DB) (err error) {
nowTime := time.Now().Unix()
m.CreatedAt = nowTime
m.UpdatedAt = nowTime
return
}
func (m *UserReadArticle) BeforeUpdate(tx *gorm.DB) (err error) {
m.UpdatedAt = time.Now().Unix()
return
}
func (m *UserReadArticle) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *UserReadArticle) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*UserReadArticle); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *UserReadArticle) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
"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"
"gorm.io/gorm"
)
type UserReadArticleRepository struct {
*cache.CachedRepository
}
func (repository *UserReadArticleRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.UserReadArticle) (*domain.UserReadArticle, error) {
var (
err error
m = &models.UserReadArticle{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *UserReadArticleRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.UserReadArticle) (*domain.UserReadArticle, error) {
var (
err error
m *models.UserReadArticle
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserReadArticleRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.UserReadArticle) (*domain.UserReadArticle, error) {
var (
err error
m *models.UserReadArticle
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserReadArticleRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.UserReadArticle) (*domain.UserReadArticle, error) {
var (
tx = conn.DB()
m = &models.UserReadArticle{Id: dm.Id}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserReadArticleRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.UserReadArticle, error) {
var (
err error
tx = conn.DB()
m = new(models.UserReadArticle)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.UserReadArticle)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserReadArticleRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.UserReadArticle, error) {
var (
tx = conn.DB()
ms []*models.UserReadArticle
dms = make([]*domain.UserReadArticle, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["userId"]; ok {
tx = tx.Where("user_id=?", v)
}
if v, ok := queryOptions["articleId"]; ok {
tx = tx.Where("article_id=?", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *UserReadArticleRepository) ModelToDomainModel(from *models.UserReadArticle) (*domain.UserReadArticle, error) {
to := &domain.UserReadArticle{}
err := copier.Copy(to, from)
return to, err
}
func (repository *UserReadArticleRepository) DomainModelToModel(from *domain.UserReadArticle) (*models.UserReadArticle, error) {
to := &models.UserReadArticle{}
err := copier.Copy(to, from)
return to, err
}
func NewUserReadArticleRepository(cache *cache.CachedRepository) domain.UserReadArticleRepository {
return &UserReadArticleRepository{CachedRepository: cache}
}
... ...
... ... @@ -38,3 +38,11 @@ type UserSimple struct {
Company string `json:"company,omitempty"` // 公司
CompanyId int64 `json:"companyId"`
}
// 记录数据的操作人
// type Operator struct {
// From string `json:"from"` // 操作来源
// Id int `json:"id"` // 人员id
// Name string `json:"name"` // 人员名字
// UpdatedAt int64 `json:"updatedAt"` // 时间
// }
... ...
... ... @@ -11,6 +11,7 @@ import (
type UserReadArticle struct {
Id int64 `json:"id"`
CompanyId int64 `json:"companyId"`
UserId int64 `json:"userId"`
ArticleId int64 `json:"articleId"`
Title string `json:"title"`
... ...