grim/devweb

move the book api to the top v1 package
draft
2021-01-25, Gary Kramlich
3ea71921fd5f
Parents 97a0f1ad2718
Children 533ca8fcc53b
move the book api to the top v1 package
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/v1/book.go Mon Jan 25 04:36:48 2021 -0600
@@ -0,0 +1,56 @@
+package v1
+
+import (
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ log "github.com/sirupsen/logrus"
+
+ "keep.imfreedom.org/grim/devweb/db"
+)
+
+func getBook(c *gin.Context) {
+ bookID := c.Params.ByName("id")
+
+ book, err := db.FindBook(bookID)
+ if err != nil {
+ c.AbortWithStatus(http.StatusNotFound)
+ } else {
+ if c.Request.Method == http.MethodHead {
+ c.Status(http.StatusOK)
+ } else {
+ c.JSON(http.StatusOK, book)
+ }
+ }
+}
+
+func updateBook(c *gin.Context) {
+ var book db.Book
+ c.Bind(&book)
+ book.ID = c.Params.ByName("id")
+
+ err := book.Update()
+ if err != nil {
+ log.Errorf("failed to update book %q: %v", book.ID, err)
+ c.AbortWithStatus(http.StatusBadRequest)
+ } else {
+ c.JSON(http.StatusOK, book)
+ }
+}
+
+func deleteBook(c *gin.Context) {
+ bookID := c.Params.ByName("id")
+
+ book, err := db.FindBook(bookID)
+ if err != nil {
+ log.Errorf("failed to delete book %q: %v", bookID, err)
+ c.AbortWithStatus(http.StatusNotFound)
+ } else {
+ if err := book.Delete(); err != nil {
+ log.Errorf("failed to delete book %q: %v", bookID, err)
+ c.AbortWithStatus(http.StatusInternalServerError)
+ } else {
+ c.Status(http.StatusNoContent)
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/v1/books.go Mon Jan 25 04:36:48 2021 -0600
@@ -0,0 +1,40 @@
+package v1
+
+import (
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ log "github.com/sirupsen/logrus"
+
+ "keep.imfreedom.org/grim/devweb/db"
+)
+
+func getBooks(c *gin.Context) {
+ books, err := db.ListBooks()
+
+ if err != nil {
+ log.Errorf("failed to get books: %v", err)
+ c.AbortWithStatus(http.StatusInternalServerError)
+ } else {
+ d := struct {
+ Books []db.Book `json:"books"`
+ }{
+ books,
+ }
+
+ c.JSON(http.StatusOK, d)
+ }
+}
+
+func createBook(c *gin.Context) {
+ var book db.Book
+ c.Bind(&book)
+
+ if err := book.Create(); err != nil {
+ c.AbortWithStatus(http.StatusBadRequest)
+
+ return
+ }
+
+ c.Redirect(http.StatusFound, book.ID)
+}
--- a/v1/books/book.go Mon Jan 25 04:16:42 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-package books
-
-import (
- "net/http"
-
- "github.com/gin-gonic/gin"
- log "github.com/sirupsen/logrus"
-
- "keep.imfreedom.org/grim/devweb/db"
-)
-
-func getBook(c *gin.Context) {
- bookID := c.Params.ByName("id")
-
- book, err := db.FindBook(bookID)
- if err != nil {
- c.AbortWithStatus(http.StatusNotFound)
- } else {
- if c.Request.Method == http.MethodHead {
- c.Status(http.StatusOK)
- } else {
- c.JSON(http.StatusOK, book)
- }
- }
-}
-
-func updateBook(c *gin.Context) {
- var book db.Book
- c.Bind(&book)
- book.ID = c.Params.ByName("id")
-
- err := book.Update()
- if err != nil {
- log.Errorf("failed to update book %q: %v", book.ID, err)
- c.AbortWithStatus(http.StatusBadRequest)
- } else {
- c.JSON(http.StatusOK, book)
- }
-}
-
-func deleteBook(c *gin.Context) {
- bookID := c.Params.ByName("id")
-
- book, err := db.FindBook(bookID)
- if err != nil {
- log.Errorf("failed to delete book %q: %v", bookID, err)
- c.AbortWithStatus(http.StatusNotFound)
- } else {
- if err := book.Delete(); err != nil {
- log.Errorf("failed to delete book %q: %v", bookID, err)
- c.AbortWithStatus(http.StatusInternalServerError)
- } else {
- c.Status(http.StatusNoContent)
- }
- }
-}
--- a/v1/books/books.go Mon Jan 25 04:16:42 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-package books
-
-import (
- "net/http"
-
- "github.com/gin-gonic/gin"
- log "github.com/sirupsen/logrus"
-
- "keep.imfreedom.org/grim/devweb/db"
-)
-
-func getBooks(c *gin.Context) {
- books, err := db.ListBooks()
-
- if err != nil {
- log.Errorf("failed to get books: %v", err)
- c.AbortWithStatus(http.StatusInternalServerError)
- } else {
- d := struct {
- Books []db.Book `json:"books"`
- }{
- books,
- }
-
- c.JSON(http.StatusOK, d)
- }
-}
-
-func createBook(c *gin.Context) {
- var book db.Book
- c.Bind(&book)
-
- if err := book.Create(); err != nil {
- c.AbortWithStatus(http.StatusBadRequest)
-
- return
- }
-
- c.Redirect(http.StatusFound, book.ID)
-}
--- a/v1/books/routes.go Mon Jan 25 04:16:42 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-package books
-
-import (
- "github.com/gin-gonic/gin"
-
- "keep.imfreedom.org/grim/devweb/access"
-)
-
-func Routes(router *gin.RouterGroup) {
- books := router.Group("/books")
-
- books.GET("/:id", getBook)
- books.HEAD("/:id", getBook)
- books.GET("/", getBooks)
-
- authed := books.Group("", access.Middleware())
- authed.PUT("/:id", updateBook)
- authed.DELETE("/:id", deleteBook)
- authed.POST("/", createBook)
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/v1/routes.go Mon Jan 25 04:36:48 2021 -0600
@@ -0,0 +1,20 @@
+package v1
+
+import (
+ "github.com/gin-gonic/gin"
+
+ "keep.imfreedom.org/grim/devweb/access"
+)
+
+func Routes(router *gin.RouterGroup) {
+ v1 := router.Group("/v1")
+
+ v1.GET("/books/", getBooks)
+ v1.GET("/books/:id", getBook)
+ v1.HEAD("/books/:id", getBook)
+
+ authed := v1.Group("", access.Middleware())
+ authed.PUT("/books/:id", updateBook)
+ authed.DELETE("/books/:id", deleteBook)
+ authed.POST("/books/", createBook)
+}
--- a/v1/v1.go Mon Jan 25 04:16:42 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
-package v1
-
-import (
- "github.com/gin-gonic/gin"
-
- "keep.imfreedom.org/grim/devweb/v1/books"
-)
-
-func Routes(router *gin.RouterGroup) {
- v1 := router.Group("/v1")
-
- books.Routes(v1)
-}