refactor
This commit is contained in:
parent
7902f42c46
commit
231fce5077
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
preorder
|
preorder
|
||||||
|
.idea
|
||||||
|
65
authors/controllers.go
Normal file
65
authors/controllers.go
Normal file
@ -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})
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package models
|
package authors
|
||||||
|
|
||||||
type Author struct {
|
type Author struct {
|
||||||
ID uint `json:"id" gorm:"primary_key"`
|
ID uint `json:"id" gorm:"primary_key"`
|
12
authors/router.go
Normal file
12
authors/router.go
Normal file
@ -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
|
||||||
|
}
|
18
config/db.go
Normal file
18
config/db.go
Normal file
@ -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
|
||||||
|
}
|
@ -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
|
|
||||||
}
|
|
@ -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
|
|
||||||
}
|
|
@ -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
|
|
||||||
}
|
|
65
formats/controllers.go
Normal file
65
formats/controllers.go
Normal file
@ -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})
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package models
|
package formats
|
||||||
|
|
||||||
type Format struct {
|
type Format struct {
|
||||||
ID uint `json:"id" gorm:"primary_key"`
|
ID uint `json:"id" gorm:"primary_key"`
|
12
formats/router.go
Normal file
12
formats/router.go
Normal file
@ -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
|
||||||
|
}
|
24
main.go
24
main.go
@ -1,9 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
"net/http"
|
"net/http"
|
||||||
"preorder/controllers"
|
"preorder/authors"
|
||||||
"preorder/models"
|
"preorder/config"
|
||||||
|
"preorder/formats"
|
||||||
|
"preorder/orders"
|
||||||
|
"preorder/users"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -13,17 +17,25 @@ func SetupRouter() *gin.Engine {
|
|||||||
router.GET("/ping", func(c *gin.Context) {
|
router.GET("/ping", func(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{"message": "pong"})
|
c.JSON(200, gin.H{"message": "pong"})
|
||||||
})
|
})
|
||||||
router = controllers.ApplyAuthorRouter(router)
|
router = authors.ApplyAuthorRouter(router)
|
||||||
router = controllers.ApplyFormatRouter(router)
|
router = formats.ApplyFormatRouter(router)
|
||||||
router = controllers.ApplyOrderRouter(router)
|
router = orders.ApplyOrderRouter(router)
|
||||||
|
|
||||||
return 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() {
|
func main() {
|
||||||
router := SetupRouter()
|
router := SetupRouter()
|
||||||
|
|
||||||
models.ConnectDatabase()
|
config.ConnectDatabase()
|
||||||
|
Migrate(config.DB)
|
||||||
|
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: ":8123",
|
Addr: ":8123",
|
||||||
|
@ -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
|
|
||||||
}
|
|
65
orders/controllers.go
Normal file
65
orders/controllers.go
Normal file
@ -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})
|
||||||
|
}
|
@ -1,16 +1,20 @@
|
|||||||
package models
|
package orders
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"preorder/authors"
|
||||||
|
"preorder/formats"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Order struct {
|
type Order struct {
|
||||||
ID uint `json:"id" gorm:"primary_key"`
|
ID uint `json:"id" gorm:"primary_key"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
AuthorID uint `json:"author"`
|
AuthorID uint `json:"author"`
|
||||||
Author Author
|
Author authors.Author
|
||||||
ReleaseDate time.Time `json:"release_date" gorm:"column:release_date"`
|
ReleaseDate time.Time `json:"release_date" gorm:"column:release_date"`
|
||||||
ISBN13 uint `json:"isbn_13" gorm:"column:isbn_13"`
|
ISBN13 uint `json:"isbn_13" gorm:"column:isbn_13"`
|
||||||
FormatID uint `json:"format"`
|
FormatID uint `json:"format"`
|
||||||
Format Format
|
Format formats.Format
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateOrderInput struct {
|
type CreateOrderInput struct {
|
12
orders/router.go
Normal file
12
orders/router.go
Normal file
@ -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
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package models
|
package users
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
@ -23,9 +23,9 @@ type UpdateUserInput struct {
|
|||||||
|
|
||||||
func NewUser(id uint, username, password string, createdAt, updatedAt time.Time) User {
|
func NewUser(id uint, username, password string, createdAt, updatedAt time.Time) User {
|
||||||
user := User{
|
user := User{
|
||||||
ID: id,
|
ID: id,
|
||||||
Username: username,
|
Username: username,
|
||||||
Password: password,
|
Password: password,
|
||||||
CreatedAt: createdAt,
|
CreatedAt: createdAt,
|
||||||
UpdatedAt: updatedAt,
|
UpdatedAt: updatedAt,
|
||||||
}
|
}
|
1
users/router.go
Normal file
1
users/router.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package users
|
Loading…
Reference in New Issue
Block a user