Skip to content

Instantly share code, notes, and snippets.

@BK1031
Created July 15, 2024 23:28
Show Gist options
  • Save BK1031/20408ba167b95c2ca8d8f1bd09b1bc35 to your computer and use it in GitHub Desktop.
Save BK1031/20408ba167b95c2ca8d8f1bd09b1bc35 to your computer and use it in GitHub Desktop.
singlestore-go-bookstore rest of book service
package service
import (
"bookstore/database"
"bookstore/model"
)
func CreateBook(book model.Book) (model.Book, error) {
result := database.DB.Create(&book)
if result.Error != nil {
return model.Book{}, result.Error
}
return book, nil
}
func UpdateBook(book model.Book) (model.Book, error) {
result := database.DB.Model(&book).Updates(book)
if result.Error != nil {
return model.Book{}, result.Error
}
return book, nil
}
func DeleteBook(book model.Book) error {
result := database.DB.Delete(&book)
if result.Error != nil {
return result.Error
}
return nil
}
func GetBook(id uint) (model.Book, error) {
var book model.Book
result := database.DB.First(&book, "id = ?", id)
if result.Error != nil {
return model.Book{}, result.Error
}
return book, nil
}
func GetAllBooks() []model.Book {
var books []model.Book
database.DB.Find(&books)
return books
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment