Skip to content

Commit

Permalink
feat: initial commit without update
Browse files Browse the repository at this point in the history
  • Loading branch information
ken aqshal committed Jan 25, 2022
0 parents commit 3974982
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/bookstore_exercise.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/dictionaries/ken.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions bookstore_exercise.go
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")
}
57 changes: 57 additions & 0 deletions controllers/books.go
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})
}
27 changes: 27 additions & 0 deletions models/book.go
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
}
24 changes: 24 additions & 0 deletions models/setup.go
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

}

0 comments on commit 3974982

Please sign in to comment.