github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/stories/actions/code.go (about) 1 package storyactions 2 3 import ( 4 "net/http" 5 "strings" 6 7 "github.com/fragmenta/mux" 8 "github.com/fragmenta/server" 9 "github.com/fragmenta/server/config" 10 "github.com/fragmenta/view" 11 12 "github.com/bitcubate/cryptojournal/src/lib/session" 13 "github.com/bitcubate/cryptojournal/src/stories" 14 ) 15 16 // HandleListCode displays a list of stories linking to repos (github etc) 17 // responds to GET /stories/code 18 func HandleListCode(w http.ResponseWriter, r *http.Request) error { 19 20 // Get params 21 params, err := mux.Params(r) 22 if err != nil { 23 return server.InternalError(err) 24 } 25 26 // Build a query 27 q := stories.Query().Where("points > -6").Order("rank desc, points desc, id desc").Limit(listLimit) 28 29 // Restrict to stories with have a url starting with github.com or bitbucket.org 30 // other code repos can be added later 31 q.Where("url ILIKE 'https://github.com%'").OrWhere("url ILIKE 'https://bitbucket.org%'") 32 33 // Set the offset in pages if we have one 34 page := int(params.GetInt("page")) 35 if page > 0 { 36 q.Offset(listLimit * page) 37 } 38 39 // Fetch the stories 40 results, err := stories.FindAll(q) 41 if err != nil { 42 return server.InternalError(err) 43 } 44 45 // Render the template 46 view := view.NewRenderer(w, r) 47 view.AddKey("page", page) 48 view.AddKey("stories", results) 49 view.AddKey("pubdate", storiesModTime(results)) 50 view.AddKey("meta_title", "Crypto Code") 51 view.AddKey("meta_desc", config.Get("meta_desc")) 52 view.AddKey("meta_keywords", config.Get("meta_keywords")) 53 view.AddKey("meta_rss", storiesXMLPath(w, r)) 54 view.Template("stories/views/index.html.got") 55 view.AddKey("currentUser", session.CurrentUser(w, r)) 56 57 // If xml requested, serve with that template 58 if strings.HasSuffix(r.URL.Path, ".xml") { 59 view.Layout("") 60 view.Template("stories/views/index.xml.got") 61 } 62 63 return view.Render() 64 65 }