|
| 1 | +// Copyright 2013 The StudyGolang Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// http://studygolang.com |
| 5 | +// Author:polaris [email protected] |
| 6 | + |
| 7 | +package controller |
| 8 | + |
| 9 | +import ( |
| 10 | + "filter" |
| 11 | + "fmt" |
| 12 | + "github.com/studygolang/mux" |
| 13 | + "model" |
| 14 | + "net/http" |
| 15 | + "service" |
| 16 | + "util" |
| 17 | +) |
| 18 | + |
| 19 | +// 在需要评论且要回调的地方注册评论对象 |
| 20 | +func init() { |
| 21 | + // 注册评论对象 |
| 22 | + service.RegisterCommentObject("resource", service.ResourceComment{}) |
| 23 | +} |
| 24 | + |
| 25 | +// 资源索引页 |
| 26 | +// uri: /resources |
| 27 | +func ResIndexHandler(rw http.ResponseWriter, req *http.Request) { |
| 28 | + util.Redirect(rw, req, "/resources/cat/1") |
| 29 | +} |
| 30 | + |
| 31 | +// 某个分类的资源列表 |
| 32 | +// uri: /resources/cat/{catid:[0-9]+} |
| 33 | +func CatResourcesHandler(rw http.ResponseWriter, req *http.Request) { |
| 34 | + vars := mux.Vars(req) |
| 35 | + catid := vars["catid"] |
| 36 | + resources := service.FindResourcesByCatid(catid) |
| 37 | + req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/index.html") |
| 38 | + filter.SetData(req, map[string]interface{}{"activeResources": "active", "resources": resources, "categories": model.AllCategory, "curCatid": catid}) |
| 39 | +} |
| 40 | + |
| 41 | +// 某个资源详细页 |
| 42 | +// uri: /resources/{id:[0-9]+} |
| 43 | +func ResourceDetailHandler(rw http.ResponseWriter, req *http.Request) { |
| 44 | + vars := mux.Vars(req) |
| 45 | + resource, comments := service.FindResource(vars["id"]) |
| 46 | + req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/detail.html") |
| 47 | + filter.SetData(req, map[string]interface{}{"activeResources": "active", "resource": resource, "comments": comments}) |
| 48 | +} |
| 49 | + |
| 50 | +// 发布新资源 |
| 51 | +// uri: /resources/new{json:(|.json)} |
| 52 | +func NewResourceHandler(rw http.ResponseWriter, req *http.Request) { |
| 53 | + vars := mux.Vars(req) |
| 54 | + title := req.FormValue("title") |
| 55 | + if title == "" || req.Method != "POST" || vars["json"] == "" { |
| 56 | + req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/new.html") |
| 57 | + filter.SetData(req, map[string]interface{}{"activeResources": "active", "categories": model.AllCategory}) |
| 58 | + return |
| 59 | + } |
| 60 | + errMsg := "" |
| 61 | + resForm := req.FormValue("form") |
| 62 | + if resForm == model.LinkForm { |
| 63 | + if req.FormValue("url") == "" { |
| 64 | + errMsg = "url不能为空" |
| 65 | + } |
| 66 | + } else { |
| 67 | + if req.FormValue("content") == "" { |
| 68 | + errMsg = "内容不能为空" |
| 69 | + } |
| 70 | + } |
| 71 | + if errMsg != "" { |
| 72 | + fmt.Fprint(rw, `{"errno": 1, "error":"`+errMsg+`"}`) |
| 73 | + return |
| 74 | + } |
| 75 | + user, _ := filter.CurrentUser(req) |
| 76 | + // 入库 |
| 77 | + ok := service.PublishResource(user["uid"].(int), req.Form) |
| 78 | + if !ok { |
| 79 | + fmt.Fprint(rw, `{"errno": 1, "error":"服务器内部错误,请稍候再试!"}`) |
| 80 | + return |
| 81 | + } |
| 82 | + fmt.Fprint(rw, `{"errno": 0, "data":""}`) |
| 83 | +} |
0 commit comments