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
// import "myapp/model" | |
func (app *App) HandleCreateBook(w http.ResponseWriter, r *http.Request) { | |
form := &model.BookForm{} | |
if err := json.NewDecoder(r.Body).Decode(form); err != nil { | |
app.logger.Warn().Err(err).Msg("") | |
w.WriteHeader(http.StatusUnprocessableEntity) | |
fmt.Fprintf(w, `{"error": "%v"}`, appErrFormDecodingFailure) | |
return |
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
type BookForm struct { | |
Title string `json:"title"` | |
Author string `json:"author"` | |
PublishedDate string `json:"published_date"` | |
ImageUrl string `json:"image_url"` | |
Description string `json:"description"` | |
} | |
func (f *BookForm) ToModel() (*Book, error) { | |
pubDate, err := time.Parse("2006-01-02", f.PublishedDate) |
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
func CreateBook(db *gorm.DB, book *model.Book) (*model.Book, error) { | |
if err := db.Create(book).Error; err != nil { | |
return nil, err | |
} | |
return book, nil | |
} |
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
func CreateBook(db *gorm.DB, book *model.Book) (*model.Book, error) { | |
if err := db.Create(book).Error; err != nil { | |
return nil, err | |
} | |
return book, nil | |
} |
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
func (app *App) HandleDeleteBook(w http.ResponseWriter, r *http.Request) { | |
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 0, 64) | |
if err != nil || id == 0 { | |
app.logger.Info().Msgf("can not parse ID: %v", id) | |
w.WriteHeader(http.StatusUnprocessableEntity) | |
return | |
} | |
if err := repository.DeleteBook(app.db, uint(id)); err != nil { |
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
func DeleteBook(db *gorm.DB, id uint) error { | |
book := &model.Book{} | |
if err := db.Where("id = ?", id).Delete(&book).Error; err != nil { | |
return err | |
} | |
return nil | |
} |
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
import ( | |
"github.com/jinzhu/gorm" | |
"myapp/model" | |
) | |
func ReadBook(db *gorm.DB, id uint) (*model.Book, error) { | |
book := &model.Book{} | |
if err := db.Where("id = ?", id).First(&book).Error; err != nil { | |
return nil, err |
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
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"strconv" | |
"github.com/go-chi/chi" | |
"github.com/jinzhu/gorm" | |
"myapp/repository" |
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
const ( | |
appErrDataAccessFailure = "data access failure" | |
appErrJsonCreationFailure = "json creation failure" | |
) |
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
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"myapp/repository" | |
) | |
func (app *App) HandleListBooks(w http.ResponseWriter, r *http.Request) { | |
books, err := repository.ListBooks(app.db) |