feat(collections,media): add bulk operations for books and collections
Collections: - HandleBulkAddBooks: add multiple books to multiple collections Media: - HandleBulkDelete: delete multiple media items - HandleBulkUpdate: bulk update book metadata (tags, status) - Individual result tracking for each operation - WebSocket broadcasts for collection updates
This commit is contained in:
@@ -260,3 +260,197 @@ func (h *MediaHandler) ClearShelf(c echo.Context) error {
|
||||
"message": result,
|
||||
})
|
||||
}
|
||||
|
||||
// Bulk Operations
|
||||
|
||||
// POST /api/books/bulk-delete
|
||||
// Bulk delete books
|
||||
func (h *MediaHandler) HandleBulkDelete(c echo.Context) error {
|
||||
var req struct {
|
||||
BookIDs []string `json:"book_ids" validate:"required"`
|
||||
}
|
||||
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request"})
|
||||
}
|
||||
|
||||
if len(req.BookIDs) == 0 {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "book_ids required"})
|
||||
}
|
||||
|
||||
results := make([]map[string]interface{}, 0)
|
||||
successCount := 0
|
||||
failedCount := 0
|
||||
|
||||
for _, bookIDStr := range req.BookIDs {
|
||||
bookID, err := uuid.Parse(bookIDStr)
|
||||
if err != nil {
|
||||
results = append(results, map[string]interface{}{
|
||||
"book_id": bookIDStr,
|
||||
"status": "error",
|
||||
"error": "Invalid UUID",
|
||||
})
|
||||
failedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
bookUUID := pgtype.UUID{Bytes: bookID, Valid: true}
|
||||
|
||||
book, err := h.db.GetMediaItem(c.Request().Context(), bookUUID)
|
||||
if err != nil {
|
||||
results = append(results, map[string]interface{}{
|
||||
"book_id": bookIDStr,
|
||||
"status": "error",
|
||||
"error": "Book not found",
|
||||
})
|
||||
failedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
err = h.db.DeleteMediaItem(c.Request().Context(), bookUUID)
|
||||
if err != nil {
|
||||
results = append(results, map[string]interface{}{
|
||||
"book_id": bookIDStr,
|
||||
"status": "error",
|
||||
"error": err.Error(),
|
||||
})
|
||||
failedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
if book.FilePath != "" {
|
||||
os.Remove(book.FilePath)
|
||||
}
|
||||
|
||||
results = append(results, map[string]interface{}{
|
||||
"book_id": bookIDStr,
|
||||
"status": "success",
|
||||
})
|
||||
successCount++
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"results": results,
|
||||
"total": len(req.BookIDs),
|
||||
"success": successCount,
|
||||
"failed": failedCount,
|
||||
})
|
||||
}
|
||||
|
||||
// POST /api/books/bulk-update
|
||||
// Bulk update book metadata
|
||||
func (h *MediaHandler) HandleBulkUpdate(c echo.Context) error {
|
||||
var req struct {
|
||||
Updates []struct {
|
||||
BookID string `json:"book_id" validate:"required"`
|
||||
Updates struct {
|
||||
Title *string `json:"title,omitempty"`
|
||||
Author *string `json:"author,omitempty"`
|
||||
Genre *string `json:"genre,omitempty"`
|
||||
Language *string `json:"language,omitempty"`
|
||||
Tags *string `json:"tags,omitempty"`
|
||||
} `json:"updates"`
|
||||
} `json:"updates" validate:"required"`
|
||||
}
|
||||
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request"})
|
||||
}
|
||||
|
||||
if len(req.Updates) == 0 {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "updates required"})
|
||||
}
|
||||
|
||||
results := make([]map[string]interface{}, 0)
|
||||
successCount := 0
|
||||
failedCount := 0
|
||||
|
||||
for _, update := range req.Updates {
|
||||
bookID, err := uuid.Parse(update.BookID)
|
||||
if err != nil {
|
||||
results = append(results, map[string]interface{}{
|
||||
"book_id": update.BookID,
|
||||
"status": "error",
|
||||
"error": "Invalid UUID",
|
||||
})
|
||||
failedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
bookUUID := pgtype.UUID{Bytes: bookID, Valid: true}
|
||||
|
||||
existingBook, err := h.db.GetMediaItem(c.Request().Context(), bookUUID)
|
||||
if err != nil {
|
||||
results = append(results, map[string]interface{}{
|
||||
"book_id": update.BookID,
|
||||
"status": "error",
|
||||
"error": "Book not found",
|
||||
})
|
||||
failedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
updateParams := database.UpdateMediaItemParams{
|
||||
ID: bookUUID,
|
||||
Title: existingBook.Title,
|
||||
Author: existingBook.Author,
|
||||
Genre: existingBook.Genre,
|
||||
Language: existingBook.Language,
|
||||
Tags: existingBook.Tags,
|
||||
Description: existingBook.Description,
|
||||
Publisher: existingBook.Publisher,
|
||||
CopyrightYear: existingBook.CopyrightYear,
|
||||
Isbn: existingBook.Isbn,
|
||||
Series: existingBook.Series,
|
||||
SeriesNumber: existingBook.SeriesNumber,
|
||||
Asin: existingBook.Asin,
|
||||
DatePublished: existingBook.DatePublished,
|
||||
Contributors: existingBook.Contributors,
|
||||
Edition: existingBook.Edition,
|
||||
PageCount: existingBook.PageCount,
|
||||
GoodreadsID: existingBook.GoodreadsID,
|
||||
OpenlibraryID: existingBook.OpenlibraryID,
|
||||
CoverImagePath: existingBook.CoverImagePath,
|
||||
}
|
||||
|
||||
if update.Updates.Title != nil {
|
||||
updateParams.Title = *update.Updates.Title
|
||||
}
|
||||
if update.Updates.Author != nil {
|
||||
updateParams.Author = pgtype.Text{String: *update.Updates.Author, Valid: true}
|
||||
}
|
||||
if update.Updates.Genre != nil {
|
||||
updateParams.Genre = pgtype.Text{String: *update.Updates.Genre, Valid: true}
|
||||
}
|
||||
if update.Updates.Language != nil {
|
||||
updateParams.Language = pgtype.Text{String: *update.Updates.Language, Valid: true}
|
||||
}
|
||||
if update.Updates.Tags != nil {
|
||||
updateParams.Tags = pgtype.Text{String: *update.Updates.Tags, Valid: true}
|
||||
}
|
||||
|
||||
_, err = h.db.UpdateMediaItem(c.Request().Context(), updateParams)
|
||||
if err != nil {
|
||||
results = append(results, map[string]interface{}{
|
||||
"book_id": update.BookID,
|
||||
"status": "error",
|
||||
"error": err.Error(),
|
||||
})
|
||||
failedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
results = append(results, map[string]interface{}{
|
||||
"book_id": update.BookID,
|
||||
"status": "success",
|
||||
})
|
||||
successCount++
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"results": results,
|
||||
"total": len(req.Updates),
|
||||
"success": successCount,
|
||||
"failed": failedCount,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user