Skip to content

Commit ad26c25

Browse files
committed
Grouped handlers by name
1 parent 416b91a commit ad26c25

File tree

4 files changed

+61
-59
lines changed

4 files changed

+61
-59
lines changed
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
package handlers
2-
3-
import (
4-
"net/http"
5-
"strconv"
6-
7-
"github.com/eoussama/anusic-api/src/shared/models"
8-
"github.com/eoussama/anusic-api/src/shared/utils"
9-
10-
"github.com/gorilla/mux"
11-
)
12-
13-
// AnimeHandler handles the anime request (/api/v1/anime/{id:[0-9]+})
14-
func AnimeHandler(w http.ResponseWriter, r *http.Request) {
15-
16-
vars := mux.Vars(r)
17-
id, _ := strconv.Atoi(vars["id"])
18-
19-
// Getting the respective anime
20-
anime := utils.Cache.GetAnimeByMALID(id)
21-
22-
// Returning the response value
23-
if anime == nil {
24-
utils.ReturnResponse(w, nil, models.Error.AnimeNotFound(models.Error{}, id))
25-
} else {
26-
utils.ReturnResponse(w, utils.FormatAnime(*anime), nil)
27-
}
28-
}
1+
package anime
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
7+
"github.com/eoussama/anusic-api/src/shared/models"
8+
"github.com/eoussama/anusic-api/src/shared/utils"
9+
10+
"github.com/gorilla/mux"
11+
)
12+
13+
// AnimeHandler handles the anime request (/api/v1/anime/{id:[0-9]+})
14+
func AnimeHandler(w http.ResponseWriter, r *http.Request) {
15+
16+
vars := mux.Vars(r)
17+
id, _ := strconv.Atoi(vars["id"])
18+
19+
// Getting the respective anime
20+
anime := utils.Cache.GetAnimeByMALID(id)
21+
22+
// Returning the response value
23+
if anime == nil {
24+
utils.ReturnResponse(w, nil, models.Error.AnimeNotFound(models.Error{}, id))
25+
} else {
26+
utils.ReturnResponse(w, utils.FormatAnime(*anime), nil)
27+
}
28+
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
package handlers
2-
3-
import (
4-
"net/http"
5-
6-
"github.com/eoussama/anusic-api/src/shared/models"
7-
"github.com/eoussama/anusic-api/src/shared/utils"
8-
)
9-
10-
// AnimeListHandler handles the anime list request (/api/v1/anime/)
11-
func AnimeListHandler(w http.ResponseWriter, r *http.Request) {
12-
13-
// Getting the name query
14-
qName := r.URL.Query().Get("name")
15-
qYear := r.URL.Query().Get("year")
16-
17-
// Export Anime list
18-
animeTitles := []models.AnimeEx{}
19-
20-
// Sanitizing the export struct
21-
for _, anime := range utils.Cache.FilterAnime(qName, qYear) {
22-
animeTitles = append(animeTitles, anime.FormatEx())
23-
}
24-
25-
// Returning the response
26-
utils.ReturnResponse(w, animeTitles, nil)
27-
}
1+
package anime
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/eoussama/anusic-api/src/shared/models"
7+
"github.com/eoussama/anusic-api/src/shared/utils"
8+
)
9+
10+
// AnimeListHandler handles the anime list request (/api/v1/anime/)
11+
func AnimeListHandler(w http.ResponseWriter, r *http.Request) {
12+
13+
// Getting the name query
14+
qName := r.URL.Query().Get("name")
15+
qYear := r.URL.Query().Get("year")
16+
17+
// Export Anime list
18+
animeTitles := []models.AnimeEx{}
19+
20+
// Sanitizing the export struct
21+
for _, anime := range utils.Cache.FilterAnime(qName, qYear) {
22+
animeTitles = append(animeTitles, anime.FormatEx())
23+
}
24+
25+
// Returning the response
26+
utils.ReturnResponse(w, animeTitles, nil)
27+
}

src/v1/handlers/log/logs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package log

src/v1/v1.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v1
22

33
import (
44
hdlr "github.com/eoussama/anusic-api/src/v1/handlers"
5+
"github.com/eoussama/anusic-api/src/v1/handlers/anime"
56
"github.com/gorilla/mux"
67
)
78

@@ -16,10 +17,10 @@ func Init(r *mux.Router) {
1617
v1Route.HandleFunc("", hdlr.IndexHandler).Methods("GET")
1718

1819
// Anime list
19-
v1Route.HandleFunc("/anime", hdlr.AnimeListHandler).Methods("GET")
20-
v1Route.HandleFunc("/anime/", hdlr.AnimeListHandler).Methods("GET")
20+
v1Route.HandleFunc("/anime", anime.AnimeListHandler).Methods("GET")
21+
v1Route.HandleFunc("/anime/", anime.AnimeListHandler).Methods("GET")
2122

2223
// // Anime by ID
23-
v1Route.HandleFunc("/anime/{id:[0-9]+}", hdlr.AnimeHandler).Methods("GET")
24-
v1Route.HandleFunc("/anime/{id:[0-9]+}/", hdlr.AnimeHandler).Methods("GET")
24+
v1Route.HandleFunc("/anime/{id:[0-9]+}", anime.AnimeHandler).Methods("GET")
25+
v1Route.HandleFunc("/anime/{id:[0-9]+}/", anime.AnimeHandler).Methods("GET")
2526
}

0 commit comments

Comments
 (0)