Skip to content

Commit

Permalink
update article search parrt
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean04111 committed Mar 2, 2023
1 parent f6543b5 commit 4ce32c6
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 61 deletions.
78 changes: 70 additions & 8 deletions internal/logic/searcharticle/searchbytitlelogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package searcharticle

import (
"context"
"errors"
"reflect"

"yourbackend/internal/model"
"yourbackend/internal/svc"
"yourbackend/internal/types"

Expand All @@ -12,8 +15,9 @@ import (

type SearchbytitleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
ctx context.Context
svcCtx *svc.ServiceContext
Catcher []string
}

func NewSearchbytitleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchbytitleLogic {
Expand All @@ -25,13 +29,71 @@ func NewSearchbytitleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sea
}

func (l *SearchbytitleLogic) Searchbytitle(req *types.Searchreq) (resp *types.Searchresp, err error) {
res, er := l.FromES(req.Articlename)
if er != nil {
return &types.Searchresp{
Status: 1,
}, nil
}
if len(l.Catcher)>20{
return &types.Searchresp{
Status: 1,
},nil
}
var modelres []types.Article
for i := 0; i < len(res); i++ {
modelres = append(modelres, types.Article{
Id: res[i].Mongoid,
Title: res[i].Title,
Description: res[i].Fewcontent,
Likes: int(res[i].Likes),
Reads: int(res[i].Views),
Url: res[i].Url,
Pubtime: res[i].Pubtime,
Imglink: res[i].Coverlinks,
})
}
return &types.Searchresp{
Status: 0,
Articlelist: modelres,
}, nil
}

return
type Arti struct {
Fewcontent string
Mongoid string
Title string
}
func (l *SearchbytitleLogic)FromES(keyword string)(string,error){
esclient,e:=elastic.NewClient(elastic.SetURL(l.svcCtx.Config.ES.Addr))
if e!=nil{
return "",e

func (l *SearchbytitleLogic) FromES(keyword string) ([]*model.Articles, error) {
esclient, e := elastic.NewClient(elastic.SetURL(l.svcCtx.Config.ES.Addr), elastic.SetSniff(false))
if e != nil {
return nil, e
}
matchquery := elastic.NewMatchQuery("fewcontent", keyword)
searchresult, er := esclient.Search().Index("article").Query(matchquery).Do(context.Background())
if er != nil {
return nil, er
}else
if searchresult.TotalHits() > 0 {
var got Arti
for _, item := range searchresult.Each(reflect.TypeOf(got)) {
if t, ok := item.(Arti); ok {
l.Catcher = append(l.Catcher, t.Mongoid)
}
}
var Res []*model.Articles
for i := 0; i < len(l.Catcher); i++ {
res, e := l.svcCtx.ArticleMysqlModel.FindOne(l.ctx, l.Catcher[i])
if e != nil {
return nil, e
}
Res = append(Res, res)
return Res, nil
}
} else {
return nil, errors.New("NoMatch")
}

return nil,nil
}

138 changes: 85 additions & 53 deletions internal/logic/updatecontent/updatecontentlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package updatecontent
import (
"context"
"errors"
"strconv"
"strings"
"sync"
"time"

"yourbackend/internal/model"
"yourbackend/internal/svc"
"yourbackend/internal/types"

Expand Down Expand Up @@ -37,8 +39,8 @@ func NewUpdatecontentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upd
//redis should be here to catch the request !!

func (l *UpdatecontentLogic) Updatecontent(req *types.Articlereq) (*types.Articleresp, error) {
gotuser, e := l.svcCtx.MysqlModel.FindOne(l.ctx, l.ctx.Value("email").(string))
if req.Arid == "" {
gotuser, e := l.svcCtx.MysqlModel.FindOne(l.ctx, l.ctx.Value("email").(string))
if e != nil {
return &types.Articleresp{
Status: 1,
Expand All @@ -50,7 +52,7 @@ func (l *UpdatecontentLogic) Updatecontent(req *types.Articlereq) (*types.Articl
errchan := make(chan error, 2)
go func() {
defer wg.Done()
err := l.ToES(arid, title, fewcontent, "insert",req.Content=="")
err := l.ToES(arid, title, fewcontent, "insert", req.Content == "")
if err != nil {
errchan <- err
} else {
Expand All @@ -59,19 +61,22 @@ func (l *UpdatecontentLogic) Updatecontent(req *types.Articlereq) (*types.Articl
}()
go func() {
defer wg.Done()
er := l.ToMongo(bson, "insert")
if er != nil {
errchan <- er
er1, er2 := l.ToMongo(bson, "insert")
if er1 != nil {
errchan <- er1
}
if er2 != nil {
errchan <- er2
} else {
return
}
}()
wg.Wait()
select {
case <-errchan:
return &types.Articleresp{
Status: 1,
}, nil
return &types.Articleresp{
Status: 1,
}, nil
default:
return &types.Articleresp{
Status: 0,
Expand All @@ -80,40 +85,43 @@ func (l *UpdatecontentLogic) Updatecontent(req *types.Articlereq) (*types.Articl
}, nil
}
} else {
bson, arid, title, fewcontent := l.BsonMFiller(req.Content, "", "", req.Arid, time.Now().Unix(), req.IsPublish, req.Content == "")
bson, arid, title, fewcontent := l.BsonMFiller(req.Content, gotuser.AvatarLink, gotuser.Name, req.Arid, time.Now().Unix(), req.IsPublish, req.Content == "")
errchan := make(chan error, 3)
wg := new(sync.WaitGroup)
wg.Add(2)
go func() {
defer wg.Done()
err := l.ToMongo(bson, "update")
if err != nil {
errchan<-err
}else{
err1, err2 := l.ToMongo(bson, "update")
if err1 != nil {
errchan <- err1
}
if err2 != nil {
errchan <- err2
} else {
return
}
}()
go func(){
go func() {
defer wg.Done()
err:=l.ToES(arid,title,fewcontent,"update",req.Content=="")
if err!=nil{
errchan<-err
}else{
err := l.ToES(arid, title, fewcontent, "update", req.Content == "")
if err != nil {
errchan <- err
} else {
return
}
}()
wg.Wait()
select{
select {
case <-errchan:
return &types.Articleresp{
Status: 1,
},nil
default:
return &types.Articleresp{
Status: 0,
Arid: arid,
IsPublish: req.IsPublish,
},nil
}, nil
default:
return &types.Articleresp{
Status: 0,
Arid: arid,
IsPublish: req.IsPublish,
}, nil
}
}
}
Expand All @@ -124,7 +132,7 @@ type ESar struct {
Title string `json:"title"`
}

func (l *UpdatecontentLogic) ToES(mongoid, title, fewcontent, operation string,isDelete bool) error {
func (l *UpdatecontentLogic) ToES(mongoid, title, fewcontent, operation string, isDelete bool) error {
esclient, e := elastic.NewClient(elastic.SetURL(l.svcCtx.Config.ES.Addr), elastic.SetSniff(false))
if e != nil {
return e
Expand All @@ -137,7 +145,7 @@ func (l *UpdatecontentLogic) ToES(mongoid, title, fewcontent, operation string,i
Fewcontent: fewcontent,
Title: title,
}
if isDelete{
if isDelete {
_, errrr := esclient.DeleteByQuery().Index("article").Query(elastic.NewTermQuery("mongoid", mongoid)).ProceedOnVersionConflict().Do(context.Background())
return errrr
}
Expand All @@ -151,21 +159,23 @@ func (l *UpdatecontentLogic) ToES(mongoid, title, fewcontent, operation string,i
case "update":
//!
script := elastic.NewScriptInline("ctx._source.title=params.title;ctx._source.fewcontent=params.fewcontent").Params(map[string]interface{}{
"title":title,
"fewcontent":fewcontent,
})
"title": title,
"fewcontent": fewcontent,
})
_, errr := esclient.UpdateByQuery().Index("article").Query(elastic.NewTermQuery("mongoid", mongoid)).Script(script).ProceedOnVersionConflict().Do(context.Background())
return errr

default:
return errors.New("wrong operation")
}
}
func (l *UpdatecontentLogic) ToMongo(ar bson.M, operation string) error {

//The first error id for mongoDB the second error is for mysql
func (l *UpdatecontentLogic) ToMongo(ar bson.M, operation string) (error, error) {
clientops := options.Client().ApplyURI(l.svcCtx.Config.Mongo.Addr)
mongoclient, err := mongo.Connect(context.TODO(), clientops)
if err != nil {
return err
return err, nil
}
collection := mongoclient.Database("DB").Collection("article")
switch operation {
Expand All @@ -174,24 +184,46 @@ func (l *UpdatecontentLogic) ToMongo(ar bson.M, operation string) error {
_, errr := collection.DeleteOne(context.Background(), bson.M{"arid": ar["arid"]}, options.Delete().SetCollation(&options.Collation{
CaseLevel: false,
}))
return errr
e := l.svcCtx.ArticleMysqlModel.Delete(l.ctx, ar["arid"].(string))
return errr, e
} else {
_, er := collection.InsertOne(context.Background(), ar)
return er
_, e := l.svcCtx.ArticleMysqlModel.Insert(l.ctx, &model.Articles{
Mongoid: ar["arid"].(string),
Title: ar["title"].(string),
Fewcontent: ar["fewcontent"].(string),
Likes: ar["likes"].(int64),
Views: ar["views"].(int64),
Url: ar["url"].(string),
Pubtime: strconv.Itoa(int(ar["created"].(int64))),
Coverlinks: ar["coverlink"].(string),
})
return er, e
}
case "update":
if ar["isDelete"] == true {
_, errr := collection.DeleteOne(context.Background(), bson.M{"arid": ar["arid"]}, options.Delete().SetCollation(&options.Collation{
CaseLevel: false,
}))
return errr
e := l.svcCtx.ArticleMysqlModel.Delete(l.ctx, ar["arid"].(string))
return errr, e
} else {
//!
_, e := collection.UpdateOne(context.Background(), bson.M{"arid": ar["arid"]}, bson.M{"$set": bson.M{"content": ar["content"], "created": ar["created"], "ispublish": ar["ispublish"], "lastrefresh": ar["lastrefresh"], "fewcontent": ar["fewcontent"], "isDelete": ar["isDelete"]}}, options.Update().SetUpsert(true))
return e
er := l.svcCtx.ArticleMysqlModel.Update(l.ctx, &model.Articles{
Mongoid: ar["arid"].(string),
Title: ar["title"].(string),
Fewcontent: ar["fewcontent"].(string),
Likes: ar["likes"].(int64),
Views: ar["views"].(int64),
Url: ar["url"].(string),
Pubtime: strconv.Itoa(int(ar["created"].(int64))),
Coverlinks: ar["coverlink"].(string),
})
return e, er
}
default:
return errors.New("wrong operation")
return errors.New("wrong operation"), nil
}
}

Expand All @@ -200,10 +232,10 @@ func (l *UpdatecontentLogic) ToMongo(ar bson.M, operation string) error {
//The returns are bson, arid, title,fewcontent
func (l *UpdatecontentLogic) BsonMFiller(content, cover, author, arid string, time int64, ispublish, isDelete bool) (bson.M, string, string, string) {
bm := bson.M{}
if isDelete{
bm["arid"]=arid
bm["isDelete"]=true
return bm,arid,"",""
if isDelete {
bm["arid"] = arid
bm["isDelete"] = true
return bm, arid, "", ""
}
co := strings.Index(content, "\n")
title := strings.TrimSpace(strings.Trim(content[:co], "#"))
Expand All @@ -212,31 +244,31 @@ func (l *UpdatecontentLogic) BsonMFiller(content, cover, author, arid string, ti
}
a := strings.Index(content, "![](")
b := strings.Index(content, "g)")
if a==b{
if a == b {
bm["coverlink"] = cover
}else{
bm["coverlink"] =content[a+4 : b+1]
} else {
bm["coverlink"] = content[a+4 : b+1]
}
var min = 100
if len(content) < 100 {
min = len(content)-strings.Count(content,"#")
min = len(content) - strings.Count(content, "#")
}
fewcontent := strings.Trim(content,"#")[1:min]
fewcontent := strings.Trim(content, "#")[1:min]

bm["fewcontent"] = fewcontent
bm["title"] = title
bm["content"] = content
bm["arid"] = arid
bm["created"] = time
bm["authorname"] = author
bm["authorid"] = l.ctx.Value("uid")
bm["likes"] = 0
bm["views"] = 0
bm["likes"] = int64(0)
bm["views"] = int64(0)
bm["readers"] = []string{}
bm["ispublish"] = ispublish
bm["lastrefresh"] = int64(0)
bm["lastrefresh"] = time
bm["isDelete"] = isDelete
bm["url"] = l.svcCtx.Config.Url.Url + "reading/?ar_id" + arid
bm["daysdata"]=[7]int{}
bm["daysdata"] = [7]int{}
return bm, arid, title, fewcontent
}
Loading

0 comments on commit 4ce32c6

Please sign in to comment.