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

View File

@@ -57,7 +57,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
defer cache.Close() defer cache.Close()
animeCache, err = bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour)) animeCache, err = bigcache.NewBigCache(bigcache.DefaultConfig(6 * time.Hour))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

45
nuss.go
View File

@@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"github.com/xujiajun/nutsdb" "github.com/xujiajun/nutsdb"
) )
@@ -75,6 +76,50 @@ func AddUserToAnime(username string, userId, animeId int64) (*Anime, error) {
return &anime, err return &anime, err
} }
func DeleteUserFromAnime(username string, userId, animeId int64) (*Anime, error) {
var anime Anime
err := db.Update(
func(tx *nutsdb.Tx) error {
keyBytes := Int64ToByte(animeId)
e, err := tx.Get(bucketAnime, keyBytes)
var users []WatchUser
if err != nil {
users = make([]WatchUser, 0)
} else {
// parse user list
users, err = parseUserList(e.Value)
if err != nil {
return err
}
}
// check if user already part
for i, u := range users {
// early terminate
if u.MalID == userId {
// delete user from list
users[i] = users[len(users)-1]
users = users[:len(users)-1]
// check if anime needs recycling
anime = Anime{
Anime: animeId,
Users: users,
}
if len(users) == 0 {
return tx.Delete(bucketAnime, keyBytes)
}
newData, err := json.Marshal(users)
if err != nil {
return err
}
return tx.Put(bucketAnime, keyBytes, newData, nutsdb.Persistent)
}
}
return fmt.Errorf("%s %d schaut nicht %d", username, userId, animeId)
})
return &anime, err
}
func ReadAnimes() ([]Anime, error) { func ReadAnimes() ([]Anime, error) {
var animes []Anime var animes []Anime
err := db.View( err := db.View(

55
ober.go
View File

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