This file contains 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/util/validator" | |
func (app *App) HandleCreateBook(w http.ResponseWriter, r *http.Request) { | |
// after create form from decoding request Body | |
if err := app.validator.Struct(form); err != nil { | |
app.logger.Warn().Err(err).Msg("") | |
resp := validator.ToErrResponse(err) |
This file contains 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 validator | |
import ( | |
"fmt" | |
"reflect" | |
"strings" | |
"gopkg.in/go-playground/validator.v9" | |
) |
This file contains 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) HandleCreateBook(w http.ResponseWriter, r *http.Request) { | |
// after create form from decoding request Body | |
if err := app.validator.Struct(form); err != nil { | |
app.logger.Warn().Err(err).Msg("") | |
w.WriteHeader(http.StatusUnprocessableEntity) | |
fmt.Fprintf(w, `{"error": "%v"}`, err.Error()) | |
return |
This file contains 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" form:"required,max=255"` | |
Author string `json:"author" form:"required,max=255"` | |
PublishedDate string `json:"published_date" form:"required"` | |
ImageUrl string `json:"image_url" form:"url"` | |
Description string `json:"description"` | |
} |
This file contains 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 "gopkg.in/go-playground/validator.v9" | |
type App struct { | |
logger *logger.Logger | |
db *gorm.DB | |
validator *validator.Validate | |
} | |
func New( | |
logger *logger.Logger, |
This file contains 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 vr "myapp/util/validator" | |
func main() { | |
// ... | |
validator := vr.New() | |
application := app.New(logger, db, validator) | |
// ... |
This file contains 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 validator | |
import ( | |
"gopkg.in/go-playground/validator.v9" | |
) | |
func New() *validator.Validate { | |
validate := validator.New() | |
validate.SetTagName("form") |
This file contains 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) HandleUpdateBook(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 | |
} | |
form := &model.BookForm{} |
This file contains 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 UpdateBook(db *gorm.DB, book *model.Book) error { | |
if err := db.First(&model.Book{}, book.ID).Update(book).Error; err != nil { | |
return err | |
} | |
return nil | |
} |
This file contains 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 ( | |
appErrDataCreationFailure = "data creation failure" | |
appErrFormDecodingFailure = "form decoding failure" | |
) |