DELETE watching

This commit is contained in:
daru
2022-04-16 01:14:34 +02:00
parent 6ba9176ca7
commit 940fc5008d
3 changed files with 78 additions and 36 deletions

67
ober.go
View File

@@ -163,6 +163,14 @@ func Register(ctx *fasthttp.RequestCtx) {
}
func WatchPost(ctx *fasthttp.RequestCtx) {
processUpdateReq(ctx, true)
}
func WatchDelete(ctx *fasthttp.RequestCtx) {
processUpdateReq(ctx, false)
}
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" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
@@ -188,53 +196,42 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
for i, anime := range animes {
// check if anime is in season
_, err = SearchSeason(anime.Anime)
if err == nil {
// anime exitsts => save
animeData, err := AddUserToAnime(username, userId, anime.Anime)
if err != nil {
// anime not in season => holen sie diese
_, _, err := GetAnimeDetailData(anime.Anime)
if err != nil {
addErrorToCtx(ctx, err)
ctx.WriteString(err.Error())
ctx.SetStatusCode(fasthttp.StatusNotFound)
return
}
animes[i].Users = animeData.Users
}
// TODO
detail, _, err := GetAnimeDetailData(anime.Anime)
if err != nil {
ctx.WriteString(err.Error())
ctx.SetStatusCode(fasthttp.StatusNotFound)
return
var animeData *Anime
// update or delete request
if update {
// anime exitsts => save
animeData, err = AddUserToAnime(username, userId, anime.Anime)
} else {
// anime exitsts => delete
animeData, err = DeleteUserFromAnime(username, userId, anime.Anime)
}
fmt.Printf("%+v\n", detail)
data, err := json.Marshal(animes)
if err != nil {
addErrorToCtx(ctx, err)
return
}
err = writeResponseBody(ctx, data)
if err != nil {
addErrorToCtx(ctx, err)
return
}
}
}
func WatchDelete(ctx *fasthttp.RequestCtx) {
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
if ctx.UserValue("user") == nil || auth == nil || string(auth) == "" || string(ctx.Request.Header.ContentType()) != "application/json" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
username := fmt.Sprintf("%s", ctx.UserValue("user"))
legit, userId := GheddoAuth(username, string(auth))
if !legit {
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
return
animes[i].Users = animeData.Users
}
// TODO
fmt.Printf("%+v\n", userId)
data, err := json.Marshal(animes)
if err != nil {
addErrorToCtx(ctx, err)
return
}
err = writeResponseBody(ctx, data)
if err != nil {
addErrorToCtx(ctx, err)
return
}
}
func GheddoAuth(username, auth string) (bool, int64) {