diff --git a/handlers/blogHandler.go b/handlers/blogHandler.go index d9da2e3..aba9402 100644 --- a/handlers/blogHandler.go +++ b/handlers/blogHandler.go @@ -136,6 +136,33 @@ func (h *MCHandler) GetBlogByName(w http.ResponseWriter, r *http.Request) { } } +// GetAdminBlogByName GetAdminBlogByName +func (h *MCHandler) GetAdminBlogByName(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + avars := mux.Vars(r) + h.Log.Debug("vars: ", len(avars)) + if avars != nil && len(avars) == 3 && h.processAPIAdminKey(r) { + var name = avars["name"] + h.Log.Debug("name: ", name) + var astStr = avars["start"] + var aedStr = avars["end"] + st, sterr := strconv.ParseInt(astStr, 10, 64) + ed, ederr := strconv.ParseInt(aedStr, 10, 64) + + if sterr == nil && ederr == nil { + //blg := h.Manager.GetBlogByName(name, st, ed) + blg := h.DB.GetBlogsByName(name, st, ed) + w.WriteHeader(http.StatusOK) + resJSON, _ := json.Marshal(blg) + fmt.Fprint(w, string(resJSON)) + } else { + w.WriteHeader(http.StatusBadRequest) + } + } else { + w.WriteHeader(http.StatusBadRequest) + } +} + // GetBlogList GetBlogList func (h *MCHandler) GetBlogList(w http.ResponseWriter, r *http.Request) { h.setContentType(w) diff --git a/handlers/blogHandler_test.go b/handlers/blogHandler_test.go index 47d9228..f9ecaaf 100644 --- a/handlers/blogHandler_test.go +++ b/handlers/blogHandler_test.go @@ -624,6 +624,177 @@ func TestMCHandler_GetBlogByName(t *testing.T) { } } +func TestMCHandler_GetAdminBlogByName(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + + mdb.MockRows1 = &gdb.DbRows{ + Rows: [][]string{{"1", "test blog entry", "some test blog stuff", "4", "true", "2023-03-01 00:01:14", ""}, + {"2", "test blog entry 333", "some test blog stuff", "4", "false", "2023-03-01 00:01:14", "2023-03-01 00:01:14"}}, + } + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + r, _ := http.NewRequest("GET", "/ffllist", nil) + + vars := map[string]string{ + "name": "test", + "start": "1", + "end": "5", + } + r = mux.SetURLVars(r, vars) + r.Header.Set("apiAdminKey", "1234") + //r.Header.Set("apiKey", "1234") + //r.Header.Set("Content-Type", "application/json") + + w := httptest.NewRecorder() + + + + + r2, _ := http.NewRequest("GET", "/ffllist", nil) + + vars2 := map[string]string{ + "name": "test", + "start": "1", + "end": "5", + } + r2 = mux.SetURLVars(r2, vars2) + r2.Header.Set("apiAdminKey", "12346") + //r.Header.Set("apiKey", "1234") + //r.Header.Set("Content-Type", "application/json") + + w2 := httptest.NewRecorder() + + + + r3, _ := http.NewRequest("GET", "/ffllist", nil) + + vars3 := map[string]string{ + "name": "test", + "start": "1u", + "end": "5", + } + r3 = mux.SetURLVars(r3, vars3) + r3.Header.Set("apiAdminKey", "1234") + //r.Header.Set("apiKey", "1234") + //r.Header.Set("Content-Type", "application/json") + + w3 := httptest.NewRecorder() + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + len int + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + // Manager: mg.New(), + APIAdminKey: "1234", + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + len: 2, + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + // Manager: mg.New(), + APIAdminKey: "1234", + }, + args: args{ + w: w2, + r: r2, + }, + code: 400, + suc: false, + len: 0, + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + // Manager: mg.New(), + APIAdminKey: "1234", + }, + args: args{ + w: w3, + r: r3, + }, + code: 400, + suc: true, + len: 0, + ww: w3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.GetAdminBlogByName(tt.args.w, tt.args.r) + var res []db.Blog + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || len(res) != tt.len { + t.Fail() + } + }) + } +} + func TestMCHandler_GetBlogList(t *testing.T) { var l lg.Logger @@ -1395,7 +1566,6 @@ func TestMCHandler_DeleteBlog(t *testing.T) { w3 := httptest.NewRecorder() - r4, _ := http.NewRequest("GET", "/ffllist", nil) r4.Header.Set("apiAdminKey", "1234") vars4 := map[string]string{ @@ -1407,7 +1577,6 @@ func TestMCHandler_DeleteBlog(t *testing.T) { mdb.MockDeleteSuccess2 = false - //mdb.MockDeleteSuccess1 = false type fields struct { @@ -1495,7 +1664,7 @@ func TestMCHandler_DeleteBlog(t *testing.T) { DB: &mdb, Log: log, }, - Log: log, + Log: log, APIAdminKey: "1234", }, args: args{ @@ -1504,7 +1673,7 @@ func TestMCHandler_DeleteBlog(t *testing.T) { }, code: 500, suc: false, - len: 0, + len: 0, ww: w4, }, } diff --git a/handlers/coverage.out b/handlers/coverage.out index 9c3c3a3..1ceb6f9 100644 --- a/handlers/coverage.out +++ b/handlers/coverage.out @@ -45,37 +45,42 @@ github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:118.35,126.35 7 1 github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:126.35,131.4 4 1 github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:131.9,133.4 1 1 github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:134.8,136.3 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:140.73,144.35 4 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:144.35,150.35 5 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:150.35,155.4 4 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:155.9,157.4 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:158.8,160.3 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:164.78,168.62 4 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:168.62,174.35 5 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:174.35,179.4 4 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:179.9,181.4 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:182.8,184.3 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:188.74,191.11 3 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:191.11,193.3 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:193.8,198.52 5 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:198.52,200.4 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:200.9,205.19 5 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:205.19,209.5 3 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:209.10,211.5 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:217.75,220.12 3 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:220.12,222.3 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:222.8,227.53 5 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:227.53,229.4 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:229.9,234.19 5 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:234.19,238.5 3 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:238.10,240.5 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:246.72,250.62 4 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:250.62,254.19 4 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:254.19,259.19 5 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:259.19,263.5 3 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:263.10,265.5 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:266.9,268.4 1 1 -github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:269.8,271.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:140.80,144.64 4 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:144.64,152.35 7 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:152.35,158.4 4 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:158.9,160.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:161.8,163.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:167.73,171.35 4 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:171.35,177.35 5 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:177.35,182.4 4 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:182.9,184.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:185.8,187.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:191.78,195.62 4 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:195.62,201.35 5 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:201.35,206.4 4 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:206.9,208.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:209.8,211.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:215.74,218.11 3 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:218.11,220.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:220.8,225.52 5 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:225.52,227.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:227.9,232.19 5 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:232.19,236.5 3 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:236.10,238.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:244.75,247.12 3 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:247.12,249.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:249.8,254.53 5 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:254.53,256.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:256.9,261.19 5 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:261.19,265.5 3 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:265.10,267.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:273.72,277.62 4 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:277.62,281.19 4 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:281.19,286.19 5 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:286.19,290.5 3 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:290.10,292.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:293.9,295.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:296.8,298.3 1 1 github.com/Ulbora/go-micro-blog/handlers/commentHandler.go:35.72,38.11 3 1 github.com/Ulbora/go-micro-blog/handlers/commentHandler.go:38.11,40.3 1 1 github.com/Ulbora/go-micro-blog/handlers/commentHandler.go:40.8,45.47 5 1 diff --git a/handlers/handler.go b/handlers/handler.go index 5080c1e..2671f63 100644 --- a/handlers/handler.go +++ b/handlers/handler.go @@ -49,6 +49,7 @@ type Handler interface { UpdateBlog(w http.ResponseWriter, r *http.Request) GetBlog(w http.ResponseWriter, r *http.Request) GetBlogByName(w http.ResponseWriter, r *http.Request) + GetAdminBlogByName(w http.ResponseWriter, r *http.Request) GetBlogList(w http.ResponseWriter, r *http.Request) GetAdminBlogList(w http.ResponseWriter, r *http.Request) ActivateBlog(w http.ResponseWriter, r *http.Request) diff --git a/server b/server index 1e2b3de..6d00868 100755 Binary files a/server and b/server differ diff --git a/server.go b/server.go index 795c9de..a81c49e 100644 --- a/server.go +++ b/server.go @@ -131,6 +131,7 @@ func main() { router.HandleFunc("/rs/blog/update", h.UpdateBlog).Methods("PUT") router.HandleFunc("/rs/blog/get/id/{id}", h.GetBlog).Methods("GET") router.HandleFunc("/rs/blog/get/name/{name}/{start}/{end}", h.GetBlogByName).Methods("GET") + router.HandleFunc("/rs/blog/admin/get/name/{name}/{start}/{end}", h.GetAdminBlogByName).Methods("GET") router.HandleFunc("/rs/blog/list/{start}/{end}", h.GetBlogList).Methods("GET") router.HandleFunc("/rs/blog/admin/list/{start}/{end}", h.GetAdminBlogList).Methods("GET") router.HandleFunc("/rs/blog/activate", h.ActivateBlog).Methods("PUT")