0894d65ec5
# Conflicts: # internal/backend/music.go # internal/backend/sync.go # internal/server/server.go # internal/server/syncHandler.go # internal/server/sync_handler_test.go # internal/server/test_helpers.go # internal/server/zz_music_handler_test.go
95 lines
2.9 KiB
Go
95 lines
2.9 KiB
Go
package server
|
|
|
|
import (
|
|
"music-server/internal/backend"
|
|
"music-server/internal/logging"
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
)
|
|
|
|
type SyncHandler struct {
|
|
}
|
|
|
|
func NewSyncHandler() *SyncHandler {
|
|
return &SyncHandler{}
|
|
}
|
|
|
|
// SyncProgress godoc
|
|
// @Summary Get sync progress
|
|
// @Description Returns the current sync progress or result
|
|
// @Tags sync
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Router /sync/progress [get]
|
|
func (s *SyncHandler) SyncProgress(ctx *echo.Context) error {
|
|
if backend.Syncing {
|
|
logging.GetLogger().Info("Getting sync progress")
|
|
response := backend.SyncProgress()
|
|
return ctx.JSON(http.StatusOK, response)
|
|
}
|
|
logging.GetLogger().Info("Getting sync result")
|
|
response := backend.SyncResult()
|
|
return ctx.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// SyncSoundtracksNewOnlyChanges godoc
|
|
// @Summary Sync soundtracks with only changes
|
|
// @Description Starts syncing games with only new changes
|
|
// @Tags sync
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {string} string "Start syncing soundtracks"
|
|
// @Failure 423 {string} string "Syncing is in progress"
|
|
// @Router /sync [get]
|
|
func (s *SyncHandler) SyncSoundtracksNewOnlyChanges(ctx *echo.Context) error {
|
|
if backend.Syncing {
|
|
logging.GetLogger().Warn("Syncing is already in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
logging.GetLogger().Info("Starting sync with only changes")
|
|
backend.Syncing = true
|
|
go backend.SyncSoundtracksNewOnlyChanges()
|
|
return ctx.JSON(http.StatusOK, "Start syncing soundtracks")
|
|
}
|
|
|
|
// SyncSoundtracksNewFull godoc
|
|
// @Summary Sync all games fully
|
|
// @Description Starts a full sync of all games
|
|
// @Tags sync
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {string} string "Start syncing soundtracks full"
|
|
// @Failure 423 {string} string "Syncing is in progress"
|
|
// @Router /sync/full [get]
|
|
func (s *SyncHandler) SyncSoundtracksNewFull(ctx *echo.Context) error {
|
|
if backend.Syncing {
|
|
logging.GetLogger().Warn("Syncing is already in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
logging.GetLogger().Info("Starting full sync")
|
|
backend.Syncing = true
|
|
go backend.SyncSoundtracksNewFull()
|
|
return ctx.JSON(http.StatusOK, "Start syncing soundtracks full")
|
|
}
|
|
|
|
// ResetDB godoc
|
|
// @Summary Reset soundtracks database
|
|
// @Description Resets the games database by deleting all games and songs
|
|
// @Tags sync
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {string} string "Soundtracks and songs are deleted from the database"
|
|
// @Failure 423 {string} string "Syncing is in progress"
|
|
// @Router /sync/reset [get]
|
|
func (s *SyncHandler) ResetDB(ctx *echo.Context) error {
|
|
if backend.Syncing {
|
|
logging.GetLogger().Warn("Cannot reset - syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
logging.GetLogger().Info("Resetting soundtracks database")
|
|
backend.ResetDB()
|
|
return ctx.JSON(http.StatusOK, "Soundtracks and songs are deleted from the database")
|
|
}
|