77package controller
88
99import (
10- "filter"
1110 "fmt"
11+ "html/template"
12+ "net/http"
13+ "strconv"
14+
15+ "filter"
1216 "github.com/studygolang/mux"
1317 "model"
14- "net/http"
1518 "service"
1619 "util"
1720)
@@ -33,9 +36,17 @@ func ResIndexHandler(rw http.ResponseWriter, req *http.Request) {
3336func CatResourcesHandler (rw http.ResponseWriter , req * http.Request ) {
3437 vars := mux .Vars (req )
3538 catid := vars ["catid" ]
36- resources := service .FindResourcesByCatid (catid )
39+
40+ page , _ := strconv .Atoi (req .FormValue ("p" ))
41+ if page == 0 {
42+ page = 1
43+ }
44+
45+ resources , total := service .FindResourcesByCatid (catid , page )
46+ pageHtml := service .GetPageHtml (page , total , req .URL .Path )
47+
3748 req .Form .Set (filter .CONTENT_TPL_KEY , "/template/resources/index.html" )
38- filter .SetData (req , map [string ]interface {}{"activeResources" : "active" , "resources" : resources , "categories" : service .AllCategory , "curCatid" : catid })
49+ filter .SetData (req , map [string ]interface {}{"activeResources" : "active" , "resources" : resources , "categories" : service .AllCategory , "page" : template . HTML ( pageHtml ), " curCatid" : util . MustInt ( catid ) })
3950}
4051
4152// 某个资源详细页
@@ -51,33 +62,67 @@ func ResourceDetailHandler(rw http.ResponseWriter, req *http.Request) {
5162// uri: /resources/new{json:(|.json)}
5263func NewResourceHandler (rw http.ResponseWriter , req * http.Request ) {
5364 vars := mux .Vars (req )
54- title := req .FormValue ("title" )
65+ title := req .PostFormValue ("title" )
66+ // 请求新建资源页面
5567 if title == "" || req .Method != "POST" || vars ["json" ] == "" {
5668 req .Form .Set (filter .CONTENT_TPL_KEY , "/template/resources/new.html" )
5769 filter .SetData (req , map [string ]interface {}{"activeResources" : "active" , "categories" : service .AllCategory })
5870 return
5971 }
72+
6073 errMsg := ""
61- resForm := req .FormValue ("form" )
74+ resForm := req .PostFormValue ("form" )
6275 if resForm == model .LinkForm {
63- if req .FormValue ("url" ) == "" {
76+ if req .PostFormValue ("url" ) == "" {
6477 errMsg = "url不能为空"
6578 }
6679 } else {
67- if req .FormValue ("content" ) == "" {
80+ if req .PostFormValue ("content" ) == "" {
6881 errMsg = "内容不能为空"
6982 }
7083 }
7184 if errMsg != "" {
72- fmt .Fprint (rw , `{"errno": 1, "error":"` + errMsg + `"}` )
85+ fmt .Fprint (rw , `{"ok": 0, "error":"` + errMsg + `"}` )
86+ return
87+ }
88+
89+ user , _ := filter .CurrentUser (req )
90+ err := service .PublishResource (user , req .PostForm )
91+ if err != nil {
92+ fmt .Fprint (rw , `{"ok": 0, "error":"内部服务错误,请稍候再试!"}` )
7393 return
7494 }
95+
96+ fmt .Fprint (rw , `{"ok": 1, "data":""}` )
97+ }
98+
99+ // 修改資源
100+ // uri: /resources/modify{json:(|.json)}
101+ func ModifyResourceHandler (rw http.ResponseWriter , req * http.Request ) {
102+ id := req .FormValue ("id" )
103+ if id == "" {
104+ util .Redirect (rw , req , "/resources" )
105+ return
106+ }
107+
108+ vars := mux .Vars (req )
109+ // 请求编辑資源页面
110+ if req .Method != "POST" || vars ["json" ] == "" {
111+ resource := service .FindResourceById (id )
112+ req .Form .Set (filter .CONTENT_TPL_KEY , "/template/resources/new.html" )
113+ filter .SetData (req , map [string ]interface {}{"resource" : resource , "activeResources" : "active" , "categories" : service .AllCategory })
114+ return
115+ }
116+
75117 user , _ := filter .CurrentUser (req )
76- // 入库
77- ok := service .PublishResource (user ["uid" ].(int ), req .Form )
78- if ! ok {
79- fmt .Fprint (rw , `{"errno": 1, "error":"服务器内部错误,请稍候再试!"}` )
118+ err := service .PublishResource (user , req .PostForm )
119+ if err != nil {
120+ if err == service .NotModifyAuthorityErr {
121+ rw .WriteHeader (http .StatusForbidden )
122+ return
123+ }
124+ fmt .Fprint (rw , `{"ok": 0, "error":"内部服务错误!"}` )
80125 return
81126 }
82- fmt .Fprint (rw , `{"errno ": 0 , "data":""}` )
127+ fmt .Fprint (rw , `{"ok ": 1 , "data":""}` )
83128}
0 commit comments