func GetComicView(w http.ResponseWriter, r *http.Request) { db, err := models.NewConnect(dbPath) defer db.Close() n := r.URL.Query().Get(":id") id, err := strconv.ParseInt(n, 10, 64) if err != nil { fmt.Printf("%p", err) http.NotFound(w, r) return } c, err := models.GetComic(db, id) if err != nil { fmt.Printf("%p", err) http.NotFound(w, r) return } prev, _ := models.PrevComic(db, id) next, _ := models.NextComic(db, id) if prev == nil { prev = &models.Comic{} } if next == nil { next = &models.Comic{} } data := struct { Comic, Prev, Next *models.Comic }{c, prev, next} renderTemplate(w, data, tplName, tplPath) }
func ListAPIView(w http.ResponseWriter, r *http.Request) { db, err := models.NewConnect(dbPath) defer db.Close() url := r.URL.Query().Get("url") c, err := models.AllComic(db, url) if err != nil || c == nil { fmt.Printf("%p", err) http.Error(w, http.StatusText(404), 404) return } jsonReponse(w, c, 200) }
func RandomComicView(w http.ResponseWriter, r *http.Request) { db, err := models.NewConnect(dbPath) defer db.Close() c, err := models.RandomComic(db) if err != nil { fmt.Printf("%p", err) http.NotFound(w, r) return } url := urlFor(r, "/"+strconv.FormatInt(c.ID, 10)) http.Redirect(w, r, url, 302) }
func GetAPIView(w http.ResponseWriter, r *http.Request) { db, _ := models.NewConnect(dbPath) defer db.Close() n := r.URL.Query().Get(":id") id, _ := strconv.ParseInt(n, 10, 64) c, err := getComic(w, r, db, id) if err != nil { http.Error(w, http.StatusText(404), 404) return } jsonReponse(w, c, 200) }
func ListView(w http.ResponseWriter, r *http.Request) { db, err := models.NewConnect(dbPath) defer db.Close() c, err := models.AllComic(db, "") if err != nil { fmt.Printf("%p", err) http.Error(w, http.StatusText(404), 404) return } type data struct { Comics []*models.Comic } renderTemplate(w, data{c}, "admin.html", "template/admin.html") }
func CreateAPIView(w http.ResponseWriter, r *http.Request) { db, err := models.NewConnect(dbPath) defer db.Close() c := models.NewComic( r.PostFormValue("title"), r.PostFormValue("url"), r.PostFormValue("description"), r.PostFormValue("date"), db, ) if err != nil { fmt.Printf("%p", err) http.Error(w, http.StatusText(404), 404) } c.Save() jsonReponse(w, c, 201) }
func UpdateAPIView(w http.ResponseWriter, r *http.Request) { db, _ := models.NewConnect(dbPath) defer db.Close() n := r.URL.Query().Get(":id") id, _ := strconv.ParseInt(n, 10, 64) c, err := getComic(w, r, db, id) if err != nil { http.Error(w, http.StatusText(404), 404) return } c.Title = r.PostFormValue("title") c.ImageURL = r.PostFormValue("url") c.Description = r.PostFormValue("description") c.Date = r.PostFormValue("date") c.Save() jsonReponse(w, c, 200) }