bulk actions

This commit is contained in:
5rahim
2023-11-19 20:33:05 -05:00
parent 0751504c64
commit aac52e97ff
3 changed files with 43 additions and 116 deletions

View File

@@ -19,6 +19,46 @@ func HandleGetLocalFiles(c *RouteCtx) error {
//----------------------------------------------------------------------------------------------------------------------
func HandleLocalFileBulkAction(c *RouteCtx) error {
type body struct {
Action string `json:"action"`
}
b := new(body)
if err := c.Fiber.BodyParser(b); err != nil {
return c.RespondWithError(err)
}
// Get all the local files
lfs, dbId, err := getLocalFilesAndIdFromDB(c.App.Database)
if err != nil {
return c.RespondWithError(err)
}
switch b.Action {
case "lock":
for _, lf := range lfs {
lf.Locked = true
}
case "unlock":
for _, lf := range lfs {
lf.Locked = false
}
}
// Save the local files
retLfs, err := saveLocalFilesInDB(c.App.Database, dbId, lfs)
if err != nil {
return c.RespondWithError(err)
}
return c.RespondWithData(retLfs)
}
//----------------------------------------------------------------------------------------------------------------------
// HandleUpdateLocalFileData
// POST
func HandleUpdateLocalFileData(c *RouteCtx) error {

View File

@@ -86,6 +86,9 @@ func InitRoutes(app *core.App, fiberApp *fiber.App) {
// GET /v1/library/local-files
v1Library.Get("/local-files", makeHandler(app, HandleGetLocalFiles))
// POST /v1/library/local-files
v1Library.Post("/local-files", makeHandler(app, HandleLocalFileBulkAction))
// Get the library collection
// GET /v1/library/collection
v1Library.Get("/collection", makeHandler(app, HandleGetLibraryCollection))

View File

@@ -1,116 +0,0 @@
package test_helper
import (
"github.com/goccy/go-json"
"github.com/seanime-app/seanime-server/internal/anilist"
"github.com/seanime-app/seanime-server/internal/entities"
"io"
"os"
)
func MockGetTestLocalFiles() ([]*entities.LocalFile, bool) {
// Open the JSON file
file, err := os.Open("../../test/sample/localfiles.json")
if err != nil {
println("Error opening file:", err.Error())
return nil, false
}
defer file.Close()
jsonData, err := io.ReadAll(file)
if err != nil {
println("Error reading file:", err.Error())
return nil, false
}
var data []*entities.LocalFile
if err := json.Unmarshal(jsonData, &data); err != nil {
println("Error unmarshaling JSON:", err.Error())
return nil, false
}
return data, true
}
func MockGetSelectTestLocalFiles() ([]*entities.LocalFile, bool) {
// Open the JSON file
file, err := os.Open("../../test/sample/localfiles_selected.json")
if err != nil {
println("Error opening file:", err.Error())
return nil, false
}
defer file.Close()
jsonData, err := io.ReadAll(file)
if err != nil {
println("Error reading file:", err.Error())
return nil, false
}
var data []*entities.LocalFile
if err := json.Unmarshal(jsonData, &data); err != nil {
println("Error unmarshaling JSON:", err.Error())
return nil, false
}
return data, true
}
type JWT struct {
JWT string `json:"jwt"`
}
func MockGetAnilistClient() *anilist.Client {
// Open the JSON file
file, err := os.Open("../../test/sample/jwt.json")
if err != nil {
println("Error opening file:", err.Error())
return nil
}
defer file.Close()
jsonData, err := io.ReadAll(file)
if err != nil {
println("Error reading file:", err.Error())
return nil
}
var data *JWT
if err := json.Unmarshal(jsonData, &data); err != nil {
println("Error unmarshaling JSON:", err.Error())
return nil
}
anilistClient := anilist.NewAuthedClient(data.JWT)
return anilistClient
}
func MockAllMedia() *[]*anilist.BaseMedia {
// Open the JSON file
file, err := os.Open("../../test/sample/media.json")
if err != nil {
println("Error opening file:", err.Error())
return nil
}
defer file.Close()
jsonData, err := io.ReadAll(file)
if err != nil {
println("Error reading file:", err.Error())
return nil
}
var data []*anilist.BaseMedia
if err := json.Unmarshal(jsonData, &data); err != nil {
println("Error unmarshaling JSON:", err.Error())
return nil
}
return &data
}