mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-13 12:29:53 +01:00
DELETE watching
This commit is contained in:
2
huso.go
2
huso.go
@@ -57,7 +57,7 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer cache.Close()
|
||||
animeCache, err = bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour))
|
||||
animeCache, err = bigcache.NewBigCache(bigcache.DefaultConfig(6 * time.Hour))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
45
nuss.go
45
nuss.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/xujiajun/nutsdb"
|
||||
)
|
||||
@@ -75,6 +76,50 @@ func AddUserToAnime(username string, userId, animeId int64) (*Anime, error) {
|
||||
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) {
|
||||
var animes []Anime
|
||||
err := db.View(
|
||||
|
||||
67
ober.go
67
ober.go
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user