From 85decb819a48a41f11b2960e6ac87e7da8baddae Mon Sep 17 00:00:00 2001 From: daru Date: Sun, 17 Apr 2022 20:05:19 +0200 Subject: [PATCH] GET watch extended --- klotz.go | 2 +- ober.go | 145 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 96 insertions(+), 51 deletions(-) diff --git a/klotz.go b/klotz.go index 973554f..80a71e4 100644 --- a/klotz.go +++ b/klotz.go @@ -7,7 +7,7 @@ type AnimeUser struct { Users []WatchUser `json:"users"` } -type AnimeUserDetail struct { +type AnimeUserExtended struct { Anime int64 `json:"anime"` Users []WatchUser `json:"users"` Data Anime `json:"data"` diff --git a/ober.go b/ober.go index a41c574..21a5502 100644 --- a/ober.go +++ b/ober.go @@ -19,6 +19,7 @@ func RunWebserv() { r.GET("/api/anime/{id}", Headers(AnimeGet)) r.GET("/api/user/{user?}", Headers(UserGet)) r.GET("/api/watch/{user?}", Headers(WatchGet)) + r.GET("/api/watchext/{user?}", Headers(WatchExtendedGet)) r.POST("/api/register", Headers(Register)) r.POST("/api/watch/{user}", Headers(WatchPost)) r.DELETE("/api/register", Headers(UnRegister)) @@ -143,43 +144,48 @@ func UserGet(ctx *fasthttp.RequestCtx) { } func WatchGet(ctx *fasthttp.RequestCtx) { - usrVal := ctx.UserValue("user") - var userId int64 - if usrVal != nil { - // check user exists - user, err := ReadUser(fmt.Sprintf("%s", usrVal)) - if err != nil { - ctx.WriteString("Dich gibts nicht") - ctx.SetStatusCode(fasthttp.StatusNotFound) - return - } - userId = user.MalID - } - animes, err := ReadAnimeUsers() + animeUsers, err := watchGetLogic(ctx) if err != nil { - // check if no anime were inserted - if err != nutsdb.ErrBucketEmpty { + return + } + + bytes, err := json.Marshal(animeUsers) + if err != nil { + addErrorToCtx(ctx, err) + return + } + _, err = ctx.Write(bytes) + if err != nil { + addErrorToCtx(ctx, err) + return + } + + ctx.SetContentType("application/json; charset=utf-8") + ctx.SetStatusCode(fasthttp.StatusOK) +} + +func WatchExtendedGet(ctx *fasthttp.RequestCtx) { + animeUsers, err := watchGetLogic(ctx) + if err != nil { + return + } + + animeUsersExtended := make([]AnimeUserExtended, 0) + // make list advanced + for _, a := range animeUsers { + data, _, err := GetAnimeDetailData(a.Anime) + if err != nil { addErrorToCtx(ctx, err) return } - animes = make([]AnimeUser, 0) + animeUsersExtended = append(animeUsersExtended, AnimeUserExtended{ + Anime: a.Anime, + Users: a.Users, + Data: *data, + }) } - // apply single user logic - if usrVal != nil { - filteredAnimes := make([]AnimeUser, 0) - for _, a := range animes { - for _, u := range a.Users { - if u.MalID == userId { - filteredAnimes = append(filteredAnimes, a) - break - } - } - } - animes = filteredAnimes - } - - bytes, err := json.Marshal(animes) + bytes, err := json.Marshal(animeUsersExtended) if err != nil { addErrorToCtx(ctx, err) return @@ -305,6 +311,64 @@ func WatchDelete(ctx *fasthttp.RequestCtx) { processUpdateReq(ctx, false) } +func GheddoAuth(username, auth string) (bool, int64) { + user, err := ReadUser(username) + if err != nil { + return false, 0 + } + return user.Secret == auth, user.MalID +} + +func Headers(h fasthttp.RequestHandler) fasthttp.RequestHandler { + return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) { + if *localServer { + ctx.Response.Header.Set("Access-Control-Allow-Origin", "*") + ctx.Response.Header.Set("Access-Control-Allow-Headers", "*") + ctx.Response.Header.Set("Access-Control-Allow-Methods", "*") + } + h(ctx) + }) +} + +func watchGetLogic(ctx *fasthttp.RequestCtx) ([]AnimeUser, error) { + usrVal := ctx.UserValue("user") + var userId int64 + if usrVal != nil { + // check user exists + user, err := ReadUser(fmt.Sprintf("%s", usrVal)) + if err != nil { + ctx.WriteString("Dich gibts nicht") + ctx.SetStatusCode(fasthttp.StatusNotFound) + return nil, err + } + userId = user.MalID + } + animesUsers, err := ReadAnimeUsers() + if err != nil { + // check if no anime were inserted + if err != nutsdb.ErrBucketEmpty { + addErrorToCtx(ctx, err) + return nil, err + } + animesUsers = make([]AnimeUser, 0) + } + + // apply single user logic + if usrVal != nil { + filteredAnimes := make([]AnimeUser, 0) + for _, a := range animesUsers { + for _, u := range a.Users { + if u.MalID == userId { + filteredAnimes = append(filteredAnimes, a) + break + } + } + } + animesUsers = filteredAnimes + } + return animesUsers, err +} + func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) { auth := ctx.Request.Header.Peek("X-HUSO-AUTH") if ctx.UserValue("user") == nil || auth == nil || string(auth) == "" || string(ctx.Request.Header.ContentType()) != "application/json" { @@ -370,25 +434,6 @@ func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) { ctx.SetContentType("application/json; charset=utf-8") } -func GheddoAuth(username, auth string) (bool, int64) { - user, err := ReadUser(username) - if err != nil { - return false, 0 - } - return user.Secret == auth, user.MalID -} - -func Headers(h fasthttp.RequestHandler) fasthttp.RequestHandler { - return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) { - if *localServer { - ctx.Response.Header.Set("Access-Control-Allow-Origin", "*") - ctx.Response.Header.Set("Access-Control-Allow-Headers", "*") - ctx.Response.Header.Set("Access-Control-Allow-Methods", "*") - } - h(ctx) - }) -} - func writeResponseBody(ctx *fasthttp.RequestCtx, bytes []byte) error { _, err := ctx.Write(bytes) if err != nil {