From 3974982e602b14ac4e9766ac12c618e5e10b2adf Mon Sep 17 00:00:00 2001 From: ken aqshal Date: Tue, 25 Jan 2022 18:57:28 +0700 Subject: [PATCH] feat: initial commit without update --- .idea/.gitignore | 8 +++++ .idea/bookstore_exercise.iml | 10 +++++++ .idea/dictionaries/ken.xml | 3 ++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ bookstore_exercise.go | 19 ++++++++++++ controllers/books.go | 57 ++++++++++++++++++++++++++++++++++++ models/book.go | 27 +++++++++++++++++ models/setup.go | 24 +++++++++++++++ 10 files changed, 168 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/bookstore_exercise.iml create mode 100644 .idea/dictionaries/ken.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 bookstore_exercise.go create mode 100644 controllers/books.go create mode 100644 models/book.go create mode 100644 models/setup.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/bookstore_exercise.iml b/.idea/bookstore_exercise.iml new file mode 100644 index 0000000..25ed3f6 --- /dev/null +++ b/.idea/bookstore_exercise.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dictionaries/ken.xml b/.idea/dictionaries/ken.xml new file mode 100644 index 0000000..0bb569b --- /dev/null +++ b/.idea/dictionaries/ken.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c91c0e6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/bookstore_exercise.go b/bookstore_exercise.go new file mode 100644 index 0000000..0db9c8a --- /dev/null +++ b/bookstore_exercise.go @@ -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") +} diff --git a/controllers/books.go b/controllers/books.go new file mode 100644 index 0000000..152782e --- /dev/null +++ b/controllers/books.go @@ -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}) +} diff --git a/models/book.go b/models/book.go new file mode 100644 index 0000000..3f733b2 --- /dev/null +++ b/models/book.go @@ -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 +} diff --git a/models/setup.go b/models/setup.go new file mode 100644 index 0000000..3c50963 --- /dev/null +++ b/models/setup.go @@ -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 + +}