Created
June 9, 2019 07:36
-
-
Save ssaumyaranjan7/4877e4485e1456d2be4cb27757d85639 to your computer and use it in GitHub Desktop.
This is the file for book model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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