Skip to content

Instantly share code, notes, and snippets.

@ssaumyaranjan7
Created June 9, 2019 07:36
Show Gist options
  • Save ssaumyaranjan7/4877e4485e1456d2be4cb27757d85639 to your computer and use it in GitHub Desktop.
Save ssaumyaranjan7/4877e4485e1456d2be4cb27757d85639 to your computer and use it in GitHub Desktop.
This is the file for book model.
package models
import (
"github.com/example/simple-REST/pkg/config"
"github.com/jinzhu/gorm"
)
var db *gorm.DB
type Book struct {
gorm.Model
//Id string `json:"id"`
Name string `gorm:""json:"name"`
Author string `json:"author"`
Publication string `json:"publication"`
}
func init() {
config.Connect()
db = config.GetDB()
db.AutoMigrate(&Book{})
}
func (b *Book) CreateBook() *Book {
db.NewRecord(b)
db.Create(&b)
return b
}
func GetAllBooks() []Book {
var Books []Book
db.Find(&Books)
return Books
}
func GetBookById(Id int64) (*Book , *gorm.DB){
var getBook Book
db:=db.Where("ID = ?", Id).Find(&getBook)
return &getBook, db
}
func DeleteBook(ID int64) Book {
var book Book
db.Where("ID = ?", ID).Delete(book)
return book
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment