-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"bookstore/controllers" | ||
"bookstore/models" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
models.ConnectDatabase() | ||
|
||
router.GET("/books", controllers.AllBooks) | ||
router.POST("/books", controllers.CreateBook) | ||
router.GET("/books/:id", controllers.FindBook) | ||
router.PUT("/books/:id", controllers.UpdateBook) | ||
|
||
router.Run(":8080") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package controllers | ||
|
||
import ( | ||
"bookstore/models" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
func AllBooks(c *gin.Context) { | ||
var books []models.Book | ||
models.DB.Find(&books) | ||
c.JSON(http.StatusOK, gin.H{"data": books}) | ||
} | ||
|
||
func CreateBook(c *gin.Context) { | ||
var inputBook models.CreateBookInput | ||
if err := c.ShouldBindJSON(&inputBook); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
book := models.Book{ | ||
Title: inputBook.Title, | ||
Author: inputBook.Author, | ||
} | ||
models.DB.Create(&book) | ||
|
||
c.JSON(http.StatusOK, gin.H{"data": book}) | ||
} | ||
|
||
func FindBook(c *gin.Context) { | ||
var book models.Book | ||
|
||
if error := models.DB.Where("id = ? ", c.Param("id")).First(&book).Error; error != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record Not Found"}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{"data": book}) | ||
} | ||
|
||
func UpdateBook(c *gin.Context) { | ||
var book models.Book | ||
if error := models.DB.Where("id = ? ", c.Param("id")).First(&book).Error; error != nil { | ||
c.JSON(http.StatusBadRequest, "Record Not Found") | ||
return | ||
} | ||
|
||
var input models.UpdateBookInput | ||
if err := c.ShouldBindJSON(&input); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
} | ||
|
||
if err := models.DB.Model(&book).Updates(input); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err}) | ||
} | ||
c.JSON(http.StatusOK, gin.H{"data": book}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package models | ||
|
||
import ( | ||
"github.com/satori/go.uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Book struct { | ||
ID uuid.UUID `json:"id" gorm:"type:uuid;primary_key;default:uuid_generate_v4()"` | ||
Title string `json:"title"` | ||
Author string `json:"author"` | ||
} | ||
|
||
type CreateBookInput struct { | ||
Title string `json:"title" binding:"required"` | ||
Author string `json:"author" binding:"required" ` | ||
} | ||
|
||
type UpdateBookInput struct { | ||
Title string `json:"title"` | ||
Author string `json:"author"` | ||
} | ||
|
||
func (book *Book) BeforeCreate(scope *gorm.DB) error { | ||
scope.Statement.SetColumn("id", uuid.NewV4().String()) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package models | ||
|
||
import ( | ||
_ "github.com/jinzhu/gorm/dialects/postgres" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func ConnectDatabase() { | ||
var err error | ||
|
||
Dsn := "host=localhost port=5432 user=postgres dbname=bookstore_go password=123 sslmode=disable" | ||
database, err := gorm.Open(postgres.Open(Dsn), &gorm.Config{}) | ||
if err != nil { | ||
panic(err) | ||
|
||
} | ||
database.AutoMigrate(&Book{}) | ||
|
||
DB = database | ||
|
||
} |