Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
KenWilliamson committed Aug 28, 2023
1 parent dbb0278 commit 4a9eeea
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 144 deletions.
1 change: 1 addition & 0 deletions handlers/adminConfigHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ConfigPage struct {
Desc string
KeyWords string
Config *mcd.Config
SiteData *SiteData
}

// GetConfig GetConfig
Expand Down
1 change: 1 addition & 0 deletions handlers/adminUserHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type UserPage struct {
User *mcd.User
Users *[]User
UserImage string
SiteData *SiteData
}

// User User
Expand Down
89 changes: 89 additions & 0 deletions handlers/blogHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type AddBlogPageData struct {
Desc string
KeyWords string
UserEmail string
SiteData *SiteData
}

// GetBlogList GetBlogList
Expand Down Expand Up @@ -415,3 +416,91 @@ func (h *MCHandler) processBlog(r *http.Request) (string, *mcd.Blog) {

return email, &rtn
}

// SearchBlogList SearchBlogList
func (h *MCHandler) SearchBlogList(w http.ResponseWriter, r *http.Request) {
fmt.Println("in SearchBlogList")
s, suc := h.getSession(r)
h.Log.Debug("session suc in SearchBlogList", suc)
if suc {
loggedInAuth := s.Get("loggedIn")
h.Log.Debug("loggedIn in SearchBlogList: ", loggedInAuth)
if loggedInAuth == true {
name := r.FormValue("name")
sblogList := h.Delegate.GetBlogByName(name, 0, maxPosts)
h.Log.Debug("blogCnt: ", len(*sblogList))
var sbp BlogPage
sbp.Title = h.Title
sbp.Desc = h.Desc
sbp.KeyWords = h.KeyWords
uemail := s.Get("userEmail")
if uemail != nil {
sbp.MyEmail = uemail.(string)
}
var isAdmin = s.Get("isAdmin")
h.Log.Debug("isAdmin: ", isAdmin)
if isAdmin == true {
sbp.IsAdmin = true
}
var blst []Blog
for i := range *sblogList {
var wg sync.WaitGroup
var sbb Blog
sbb.Blog = &(*sblogList)[i]
txt, err := b64.StdEncoding.DecodeString(sbb.Blog.Content)
if err == nil {
sbb.Blog.Content = string(txt)
sbb.Blog.Content = strings.Replace(sbb.Blog.Content, stripOut, "", -1)
sbb.Blog.Content = strings.Replace(sbb.Blog.Content, stripOut2, "", -1)
sbb.Blog.Content = strings.Replace(sbb.Blog.Content, stripOut3, "", -1)
sbb.Blog.Content = strings.Replace(sbb.Blog.Content, stripOut4, "", -1)

sbb.TextHTML = template.HTML(sbb.Blog.Content)
h.Log.Debug("TextHTML: ", sbb.TextHTML)
}

wg.Add(1)
go func(bbb *Blog) {
defer wg.Done()
cl := h.Delegate.GetCommentList(bbb.Blog.ID, 0, maxComments)
h.Log.Debug("commentCnt: ", len(*cl))
bbb.CommentCnt = len(*cl)
h.Log.Debug("Comments Done: ")
}(&sbb)
wg.Add(1)
go func(bbb *Blog) {
defer wg.Done()
ll := h.Delegate.ViewLikes(bbb.Blog.ID)
for _, l := range *ll {
if l.UserID == bbb.Blog.UserID {
bbb.Liked = true
}
}
h.Log.Debug("likeCnt: ", len(*ll))
bbb.LikeCnt = len(*ll)
h.Log.Debug("Views Done: ")
}(&sbb)

wg.Add(1)
go func(bbb *Blog) {
defer wg.Done()
u1 := h.Delegate.GetUserByID(bbb.Blog.UserID)
h.Log.Debug("get user: ")
bbb.User = u1
bbb.UserImage = b64.StdEncoding.EncodeToString(bbb.User.Image)
h.Log.Debug("User Done: ")
}(&sbb)

wg.Wait()

blst = append(blst, sbb)
}
sbp.BlogList = &blst
h.Log.Debug("after all waits")

h.Templates.ExecuteTemplate(w, blogListPage, &sbp)
} else {
http.Redirect(w, r, loginRt, http.StatusFound)
}
}
}
185 changes: 185 additions & 0 deletions handlers/blogHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,188 @@ func TestMCHandler_UpdateBlog(t *testing.T) {
})
}
}

func TestMCHandler_SearchBlogList(t *testing.T) {
var cf gss.ConfigOptions
cf.MaxAge = 3600
cf.Path = "/"
sessionManager, err := gss.NewSessionManager("dsdfsadfs61dsscfsdfdsdsfsdsdllsd", cf)
if err != nil {
fmt.Println(err)
log.Println("Session err: ", err)
}

r, _ := http.NewRequest("POST", "/searchBlog", strings.NewReader("name=test"))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
w := httptest.NewRecorder()

r2, _ := http.NewRequest("GET", "/", nil)
w2 := httptest.NewRecorder()

ses := sessionManager.NewSession(r, "go-micro-blog-ui")
ses.Set("loggedIn", true)
ses.Set("userEmail", "[email protected]")
ses.Set("isAdmin", true)
ses.Save(w)

cook := w.Result().Cookies()
if len(cook) > 0 {
r.AddCookie(cook[0])
}

ses2 := sessionManager.NewSession(r2, "go-micro-blog-ui")
ses2.Set("loggedIn", false)
ses2.Save(w2)

cook2 := w2.Result().Cookies()
if len(cook2) > 0 {
r2.AddCookie(cook2[0])
}

var l lg.Logger
log := l.New()
log.SetLogLevel(lg.AllLevel)

var mcdel mcd.MockDelegate
var b1 mcd.Blog
b1.Active = true
b1.Content = "dGVzdCAx"
b1.Name = "test 1"
b1.UserID = 21

var b2 mcd.Blog
b2.Active = true
b2.Content = "dGVzdCAy"
b2.Name = "test 2"
b2.UserID = 22

var b3 mcd.Blog
b3.Active = true
b3.Content = "dGVzdCAz"
b3.Name = "test 3"
b3.UserID = 23

var blo []mcd.Blog
blo = append(blo, b1)
blo = append(blo, b2)
blo = append(blo, b3)
mcdel.MockBlogList = &blo

var c1 mcd.Comment
c1.Active = true
c1.BlogID = 1
c1.Text = "test c 1"
c1.UserID = 11

var c2 mcd.Comment
c2.Active = true
c2.BlogID = 2
c2.Text = "test c 2"
c2.UserID = 22
var cml []mcd.Comment
cml = append(cml, c1)
cml = append(cml, c2)
mcdel.MockCommentList = &cml

var l1 mcd.Like
l1.BlogID = 44
l1.UserID = 22

var l2 mcd.Like
l2.BlogID = 44
l2.UserID = 23
var lkl []mcd.Like
lkl = append(lkl, l1)
lkl = append(lkl, l2)
mcdel.MockLikeList = &lkl

//mcdel.GetUserActive = true
//mcdel.GetUserEmail = "[email protected]"

var mus mcd.User
mus.Active = true
mus.Email = "[email protected]"
mcdel.MockUser = &mus

type fields struct {
Log lg.Log
APIKey string
APIAdminKey string
Delegate mcd.Delegate
Signins map[string]s.Signin
SessionManager gss.SessionManager
AdminTemplates *template.Template
Templates *template.Template
SiteName string
Title string
Desc string
KeyWords string
SiteURL string
}
type args struct {
w http.ResponseWriter
r *http.Request
}
tests := []struct {
name string
fields fields
args args
resCode int
w *httptest.ResponseRecorder
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{
Log: log,
Delegate: &mcdel,
SessionManager: sessionManager,
Templates: template.Must(template.ParseFiles("testHtmls/test.html")),
},
args: args{
w: w,
r: r,
},
resCode: 200,
w: w,
},
{
name: "test 2",
fields: fields{
Log: log,
Delegate: &mcdel,
SessionManager: sessionManager,
Templates: template.Must(template.ParseFiles("testHtmls/test.html")),
},
args: args{
w: w2,
r: r2,
},
resCode: 302,
w: w2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &MCHandler{
Log: tt.fields.Log,
APIKey: tt.fields.APIKey,
APIAdminKey: tt.fields.APIAdminKey,
Delegate: tt.fields.Delegate,
Signins: tt.fields.Signins,
SessionManager: tt.fields.SessionManager,
AdminTemplates: tt.fields.AdminTemplates,
Templates: tt.fields.Templates,
SiteName: tt.fields.SiteName,
Title: tt.fields.Title,
Desc: tt.fields.Desc,
KeyWords: tt.fields.KeyWords,
SiteURL: tt.fields.SiteURL,
}
h.SearchBlogList(tt.args.w, tt.args.r)
if tt.resCode != tt.w.Code {
t.Fail()
}
})
}
}
Loading

0 comments on commit 4a9eeea

Please sign in to comment.