-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheroes.go
More file actions
41 lines (34 loc) · 896 Bytes
/
Copy pathheroes.go
File metadata and controls
41 lines (34 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"errors"
"net/http"
"github.com/philjestin/ranked-talishar/internal/data"
)
func (app *application) showAllHeroesHandler(w http.ResponseWriter, r *http.Request) {
heroes, err := app.models.Heroes.GetAllHeroes()
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
err = app.writeJSON(w, http.StatusOK, envelope{"heroes": heroes}, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}
func (app *application) showHeroHandler(w http.ResponseWriter, r *http.Request) {
id := app.readUuidParam(r)
hero, err := app.models.Heroes.GetHeroById(id)
if err != nil {
switch {
case errors.Is(err, data.ErrRecordNotFound):
app.notFoundResponse(w, r)
default:
app.serverErrorResponse(w, r, err)
}
return
}
err = app.writeJSON(w, http.StatusOK, envelope{"hero": hero}, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}