V3Show gets a book
(w http.ResponseWriter, r *http.Request)
| 80 | |
| 81 | // V3Show gets a book |
| 82 | func (b *Books) V3Show(w http.ResponseWriter, r *http.Request) { |
| 83 | user := context.User(r.Context()) |
| 84 | if user == nil { |
| 85 | handleJSONError(w, app.ErrLoginRequired, "login required") |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | vars := mux.Vars(r) |
| 90 | bookUUID := vars["bookUUID"] |
| 91 | |
| 92 | if !helpers.ValidateUUID(bookUUID) { |
| 93 | handleJSONError(w, app.ErrInvalidUUID, "login required") |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | var book database.Book |
| 98 | err := b.app.DB.Where("uuid = ? AND user_id = ?", bookUUID, user.ID).First(&book).Error |
| 99 | |
| 100 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 101 | w.WriteHeader(http.StatusNotFound) |
| 102 | return |
| 103 | } |
| 104 | if err != nil { |
| 105 | handleJSONError(w, err, "finding the book") |
| 106 | return |
| 107 | } |
| 108 | |
| 109 | respondJSON(w, http.StatusOK, presenters.PresentBook(book)) |
| 110 | } |
| 111 | |
| 112 | type createBookPayload struct { |
| 113 | Name string `schema:"name" json:"name"` |
nothing calls this directly
no test coverage detected