From 231fce5077290e11dfb8d360684fb4b31ef0c305 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 19 Jun 2024 22:49:55 -0400 Subject: [PATCH] refactor --- .gitignore | 1 + authors/controllers.go | 65 +++++++++++++++++++++++ models/author.go => authors/models.go | 2 +- authors/router.go | 12 +++++ config/db.go | 18 +++++++ controllers/author.go | 74 --------------------------- controllers/format.go | 74 --------------------------- controllers/order.go | 74 --------------------------- formats/controllers.go | 65 +++++++++++++++++++++++ models/format.go => formats/models.go | 2 +- formats/router.go | 12 +++++ main.go | 24 ++++++--- models/models.go | 27 ---------- orders/controllers.go | 65 +++++++++++++++++++++++ models/order.go => orders/models.go | 12 +++-- orders/router.go | 12 +++++ models/user.go => users/models.go | 8 +-- users/router.go | 1 + 18 files changed, 283 insertions(+), 265 deletions(-) create mode 100644 authors/controllers.go rename models/author.go => authors/models.go (96%) create mode 100644 authors/router.go create mode 100644 config/db.go delete mode 100644 controllers/author.go delete mode 100644 controllers/format.go delete mode 100644 controllers/order.go create mode 100644 formats/controllers.go rename models/format.go => formats/models.go (96%) create mode 100644 formats/router.go delete mode 100644 models/models.go create mode 100644 orders/controllers.go rename models/order.go => orders/models.go (88%) create mode 100644 orders/router.go rename models/user.go => users/models.go (88%) create mode 100644 users/router.go diff --git a/.gitignore b/.gitignore index 7d91189..c274cbf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ preorder +.idea diff --git a/authors/controllers.go b/authors/controllers.go new file mode 100644 index 0000000..8531ff9 --- /dev/null +++ b/authors/controllers.go @@ -0,0 +1,65 @@ +package authors + +import ( + "net/http" + "preorder/config" + + "github.com/gin-gonic/gin" +) + +func FindAuthors(c *gin.Context) { + var authors []Author + config.DB.Find(&authors) + + c.JSON(http.StatusOK, gin.H{"data": authors}) +} + +func FindAuthor(c *gin.Context) { + var author Author + + if err := config.DB.Where("id = ?", c.Param("id")).First(&author).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + } + + c.JSON(http.StatusOK, gin.H{"data": author}) +} + +func CreateAuthor(c *gin.Context) { + var input CreateAuthorInput + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + author := NewAuthor(input.ID, input.FullName) + config.DB.Create(&author) + c.JSON(http.StatusOK, gin.H{"data": author}) +} + +func UpdateAuthor(c *gin.Context) { + var author Author + if err := config.DB.Where("id = ?", c.Param("id")).First(&author).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + } + + var input UpdateAuthorInput + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + config.DB.Model(&author).Updates(input) + c.JSON(http.StatusOK, gin.H{"data": author}) +} + +func DeleteAuthor(c *gin.Context) { + var author Author + if err := config.DB.Where("id = ?", c.Param("id")).First(&author).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + return + } + + config.DB.Delete(&author) + + c.JSON(http.StatusOK, gin.H{"data": true}) +} diff --git a/models/author.go b/authors/models.go similarity index 96% rename from models/author.go rename to authors/models.go index 83419ff..9c9f0e5 100644 --- a/models/author.go +++ b/authors/models.go @@ -1,4 +1,4 @@ -package models +package authors type Author struct { ID uint `json:"id" gorm:"primary_key"` diff --git a/authors/router.go b/authors/router.go new file mode 100644 index 0000000..d6c6eb9 --- /dev/null +++ b/authors/router.go @@ -0,0 +1,12 @@ +package authors + +import "github.com/gin-gonic/gin" + +func ApplyAuthorRouter(router *gin.Engine) *gin.Engine { + router.GET("/authors", FindAuthors) + router.POST("/authors", CreateAuthor) + router.GET("/authors/:id", FindAuthor) + router.PATCH("/authors/:id", UpdateAuthor) + router.DELETE("/authors/:id", DeleteAuthor) + return router +} diff --git a/config/db.go b/config/db.go new file mode 100644 index 0000000..5d4c180 --- /dev/null +++ b/config/db.go @@ -0,0 +1,18 @@ +package config + +import ( + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func ConnectDatabase() { + dsn := "root:password@tcp(db:3306)/db?charset=utf8mb4&parseTime=True&loc=Local" + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + panic("Failed to connect to database") + } + + DB = db +} diff --git a/controllers/author.go b/controllers/author.go deleted file mode 100644 index 1d0eaba..0000000 --- a/controllers/author.go +++ /dev/null @@ -1,74 +0,0 @@ -package controllers - -import ( - "net/http" - "preorder/models" - - "github.com/gin-gonic/gin" -) - -func FindAuthors(c *gin.Context) { - var Authors []models.Author - models.DB.Find(&Authors) - - c.JSON(http.StatusOK, gin.H{"data": Authors}) -} - -func FindAuthor(c *gin.Context) { - var Author models.Author - - if err := models.DB.Where("id = ?", c.Param("id")).First(&Author).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - } - - c.JSON(http.StatusOK, gin.H{"data": Author}) -} - -func CreateAuthor(c *gin.Context) { - var input models.CreateAuthorInput - if err := c.ShouldBindJSON(&input); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - author := models.NewAuthor(input.ID, input.FullName) - models.DB.Create(&author) - c.JSON(http.StatusOK, gin.H{"data": author}) -} - -func UpdateAuthor(c *gin.Context) { - var Author models.Author - if err := models.DB.Where("id = ?", c.Param("id")).First(&Author).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - } - - var input models.UpdateAuthorInput - if err := c.ShouldBindJSON(&input); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - models.DB.Model(&Author).Updates(input) - c.JSON(http.StatusOK, gin.H{"data": Author}) -} - -func DeleteAuthor(c *gin.Context) { - var Author models.Author - if err := models.DB.Where("id = ?", c.Param("id")).First(&Author).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - return - } - - models.DB.Delete(&Author) - - c.JSON(http.StatusOK, gin.H{"data": true}) -} - -func ApplyAuthorRouter(router *gin.Engine) *gin.Engine { - router.GET("/authors", FindAuthors) - router.POST("/authors", CreateAuthor) - router.GET("/authors/:id", FindAuthor) - router.PATCH("/authors/:id", UpdateAuthor) - router.DELETE("/authors/:id", DeleteAuthor) - return router -} diff --git a/controllers/format.go b/controllers/format.go deleted file mode 100644 index 669ebc4..0000000 --- a/controllers/format.go +++ /dev/null @@ -1,74 +0,0 @@ -package controllers - -import ( - "net/http" - "preorder/models" - - "github.com/gin-gonic/gin" -) - -func FindFormats(c *gin.Context) { - var Formats []models.Format - models.DB.Find(&Formats) - - c.JSON(http.StatusOK, gin.H{"data": Formats}) -} - -func FindFormat(c *gin.Context) { - var Format models.Format - - if err := models.DB.Where("id = ?", c.Param("id")).First(&Format).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - } - - c.JSON(http.StatusOK, gin.H{"data": Format}) -} - -func CreateFormat(c *gin.Context) { - var input models.CreateFormatInput - if err := c.ShouldBindJSON(&input); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - format := models.NewFormat(input.ID, input.Format) - models.DB.Create(&format) - c.JSON(http.StatusOK, gin.H{"data": format}) -} - -func UpdateFormat(c *gin.Context) { - var Format models.Format - if err := models.DB.Where("id = ?", c.Param("id")).First(&Format).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - } - - var input models.UpdateFormatInput - if err := c.ShouldBindJSON(&input); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - models.DB.Model(&Format).Updates(input) - c.JSON(http.StatusOK, gin.H{"data": Format}) -} - -func DeleteFormat(c *gin.Context) { - var Format models.Format - if err := models.DB.Where("id = ?", c.Param("id")).First(&Format).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - return - } - - models.DB.Delete(&Format) - - c.JSON(http.StatusOK, gin.H{"data": true}) -} - -func ApplyFormatRouter(router *gin.Engine) *gin.Engine { - router.GET("/formats", FindFormats) - router.POST("/formats", CreateFormat) - router.GET("/formats/:id", FindFormat) - router.PATCH("/formats/:id", UpdateFormat) - router.DELETE("/formats/:id", DeleteFormat) - return router -} diff --git a/controllers/order.go b/controllers/order.go deleted file mode 100644 index 3ceea75..0000000 --- a/controllers/order.go +++ /dev/null @@ -1,74 +0,0 @@ -package controllers - -import ( - "net/http" - "preorder/models" - - "github.com/gin-gonic/gin" -) - -func FindOrders(c *gin.Context) { - var Orders []models.Order - models.DB.Find(&Orders) - - c.JSON(http.StatusOK, gin.H{"data": Orders}) -} - -func FindOrder(c *gin.Context) { - var Order models.Order - - if err := models.DB.Where("id = ?", c.Param("id")).First(&Order).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - } - - c.JSON(http.StatusOK, gin.H{"data": Order}) -} - -func CreateOrder(c *gin.Context) { - var input models.CreateOrderInput - if err := c.ShouldBindJSON(&input); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - order := models.NewOrder(input.ID, input.Title, input.Author, input.Format, input.ISBN13, input.ReleaseDate) - models.DB.Create(&order) - c.JSON(http.StatusOK, gin.H{"data": order}) -} - -func UpdateOrder(c *gin.Context) { - var Order models.Order - if err := models.DB.Where("id = ?", c.Param("id")).First(&Order).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - } - - var input models.UpdateOrderInput - if err := c.ShouldBindJSON(&input); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - models.DB.Model(&Order).Updates(input) - c.JSON(http.StatusOK, gin.H{"data": Order}) -} - -func DeleteOrder(c *gin.Context) { - var Order models.Order - if err := models.DB.Where("id = ?", c.Param("id")).First(&Order).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) - return - } - - models.DB.Delete(&Order) - - c.JSON(http.StatusOK, gin.H{"data": true}) -} - -func ApplyOrderRouter(router *gin.Engine) *gin.Engine { - router.GET("/orders", FindOrders) - router.POST("/orders", CreateOrder) - router.GET("/orders/:id", FindOrder) - router.PATCH("/orders/:id", UpdateOrder) - router.DELETE("/orders/:id", DeleteOrder) - return router -} diff --git a/formats/controllers.go b/formats/controllers.go new file mode 100644 index 0000000..861c24d --- /dev/null +++ b/formats/controllers.go @@ -0,0 +1,65 @@ +package formats + +import ( + "net/http" + "preorder/config" + + "github.com/gin-gonic/gin" +) + +func FindFormats(c *gin.Context) { + var formats []Format + config.DB.Find(&formats) + + c.JSON(http.StatusOK, gin.H{"data": formats}) +} + +func FindFormat(c *gin.Context) { + var format Format + + if err := config.DB.Where("id = ?", c.Param("id")).First(&format).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + } + + c.JSON(http.StatusOK, gin.H{"data": format}) +} + +func CreateFormat(c *gin.Context) { + var input CreateFormatInput + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + format := NewFormat(input.ID, input.Format) + config.DB.Create(&format) + c.JSON(http.StatusOK, gin.H{"data": format}) +} + +func UpdateFormat(c *gin.Context) { + var format Format + if err := config.DB.Where("id = ?", c.Param("id")).First(&format).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + } + + var input UpdateFormatInput + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + config.DB.Model(&format).Updates(input) + c.JSON(http.StatusOK, gin.H{"data": format}) +} + +func DeleteFormat(c *gin.Context) { + var format Format + if err := config.DB.Where("id = ?", c.Param("id")).First(&format).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + return + } + + config.DB.Delete(&format) + + c.JSON(http.StatusOK, gin.H{"data": true}) +} diff --git a/models/format.go b/formats/models.go similarity index 96% rename from models/format.go rename to formats/models.go index 41644e8..5b59b6c 100644 --- a/models/format.go +++ b/formats/models.go @@ -1,4 +1,4 @@ -package models +package formats type Format struct { ID uint `json:"id" gorm:"primary_key"` diff --git a/formats/router.go b/formats/router.go new file mode 100644 index 0000000..0d584e8 --- /dev/null +++ b/formats/router.go @@ -0,0 +1,12 @@ +package formats + +import "github.com/gin-gonic/gin" + +func ApplyFormatRouter(router *gin.Engine) *gin.Engine { + router.GET("/formats", FindFormats) + router.POST("/formats", CreateFormat) + router.GET("/formats/:id", FindFormat) + router.PATCH("/formats/:id", UpdateFormat) + router.DELETE("/formats/:id", DeleteFormat) + return router +} diff --git a/main.go b/main.go index f87cee3..750585d 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,13 @@ package main import ( + "gorm.io/gorm" "net/http" - "preorder/controllers" - "preorder/models" + "preorder/authors" + "preorder/config" + "preorder/formats" + "preorder/orders" + "preorder/users" "github.com/gin-gonic/gin" ) @@ -13,17 +17,25 @@ func SetupRouter() *gin.Engine { router.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{"message": "pong"}) }) - router = controllers.ApplyAuthorRouter(router) - router = controllers.ApplyFormatRouter(router) - router = controllers.ApplyOrderRouter(router) + router = authors.ApplyAuthorRouter(router) + router = formats.ApplyFormatRouter(router) + router = orders.ApplyOrderRouter(router) return router } +func Migrate(db *gorm.DB) { + _ = db.AutoMigrate(&authors.Author{}) + _ = db.AutoMigrate(&formats.Format{}) + _ = db.AutoMigrate(&orders.Order{}) + _ = db.AutoMigrate(&users.User{}) +} + func main() { router := SetupRouter() - models.ConnectDatabase() + config.ConnectDatabase() + Migrate(config.DB) s := &http.Server{ Addr: ":8123", diff --git a/models/models.go b/models/models.go deleted file mode 100644 index b9df6c9..0000000 --- a/models/models.go +++ /dev/null @@ -1,27 +0,0 @@ -package models - -import ( - "gorm.io/driver/mysql" - "gorm.io/gorm" -) - -var DB *gorm.DB - -func ConnectDatabase() { - // Obviously change this to your settings and get securely. - //dsn := "root@unix(/var/run/mysqld/mysqld.sock)/db?charset=utf8mb4&parseTime=True&loc=Local" - //db, err := gorm.Open(mysql.New(mysql.Config{ - // DSN: dsn, - //}), &gorm.Config{}) - dsn := "root:password@tcp(db:3306)/db?charset=utf8mb4&parseTime=True&loc=Local" - db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) - if err != nil { - panic("Failed to connect to database") - } - _ = db.AutoMigrate(&Author{}) - _ = db.AutoMigrate(&Format{}) - _ = db.AutoMigrate(&Order{}) - _ = db.AutoMigrate(&User{}) - - DB = db -} diff --git a/orders/controllers.go b/orders/controllers.go new file mode 100644 index 0000000..9f77fef --- /dev/null +++ b/orders/controllers.go @@ -0,0 +1,65 @@ +package orders + +import ( + "net/http" + "preorder/config" + + "github.com/gin-gonic/gin" +) + +func FindOrders(c *gin.Context) { + var Orders []Order + config.DB.Find(&Orders) + + c.JSON(http.StatusOK, gin.H{"data": Orders}) +} + +func FindOrder(c *gin.Context) { + var order Order + + if err := config.DB.Where("id = ?", c.Param("id")).First(&order).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + } + + c.JSON(http.StatusOK, gin.H{"data": order}) +} + +func CreateOrder(c *gin.Context) { + var input CreateOrderInput + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + order := NewOrder(input.ID, input.Title, input.Author, input.Format, input.ISBN13, input.ReleaseDate) + config.DB.Create(&order) + c.JSON(http.StatusOK, gin.H{"data": order}) +} + +func UpdateOrder(c *gin.Context) { + var order Order + if err := config.DB.Where("id = ?", c.Param("id")).First(&order).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + } + + var input UpdateOrderInput + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + config.DB.Model(&order).Updates(input) + c.JSON(http.StatusOK, gin.H{"data": order}) +} + +func DeleteOrder(c *gin.Context) { + var order Order + if err := config.DB.Where("id = ?", c.Param("id")).First(&order).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + return + } + + config.DB.Delete(&order) + + c.JSON(http.StatusOK, gin.H{"data": true}) +} diff --git a/models/order.go b/orders/models.go similarity index 88% rename from models/order.go rename to orders/models.go index 3e5d8be..d44190c 100644 --- a/models/order.go +++ b/orders/models.go @@ -1,16 +1,20 @@ -package models +package orders -import "time" +import ( + "preorder/authors" + "preorder/formats" + "time" +) type Order struct { ID uint `json:"id" gorm:"primary_key"` Title string `json:"title"` AuthorID uint `json:"author"` - Author Author + Author authors.Author ReleaseDate time.Time `json:"release_date" gorm:"column:release_date"` ISBN13 uint `json:"isbn_13" gorm:"column:isbn_13"` FormatID uint `json:"format"` - Format Format + Format formats.Format } type CreateOrderInput struct { diff --git a/orders/router.go b/orders/router.go new file mode 100644 index 0000000..29c5c99 --- /dev/null +++ b/orders/router.go @@ -0,0 +1,12 @@ +package orders + +import "github.com/gin-gonic/gin" + +func ApplyOrderRouter(router *gin.Engine) *gin.Engine { + router.GET("/orders", FindOrders) + router.POST("/orders", CreateOrder) + router.GET("/orders/:id", FindOrder) + router.PATCH("/orders/:id", UpdateOrder) + router.DELETE("/orders/:id", DeleteOrder) + return router +} diff --git a/models/user.go b/users/models.go similarity index 88% rename from models/user.go rename to users/models.go index d2d1934..869f218 100644 --- a/models/user.go +++ b/users/models.go @@ -1,4 +1,4 @@ -package models +package users import ( "time" @@ -23,9 +23,9 @@ type UpdateUserInput struct { func NewUser(id uint, username, password string, createdAt, updatedAt time.Time) User { user := User{ - ID: id, - Username: username, - Password: password, + ID: id, + Username: username, + Password: password, CreatedAt: createdAt, UpdatedAt: updatedAt, } diff --git a/users/router.go b/users/router.go new file mode 100644 index 0000000..82abcb9 --- /dev/null +++ b/users/router.go @@ -0,0 +1 @@ +package users