Created
July 15, 2024 23:28
-
-
Save BK1031/20408ba167b95c2ca8d8f1bd09b1bc35 to your computer and use it in GitHub Desktop.
singlestore-go-bookstore rest of book service
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 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