feat: update handlers to normalize tags and contributors
Update CreateMediaItem handler: - Normalize tags for display using NormalizeTags() - Normalize contributors for display using NormalizeContributors() - Generate tags_search using NormalizeTagsSearch() - Generate contributors_search using NormalizeContributorsSearch() - Pass search fields to database Update UpdateMediaItem handler: - Same normalization logic as CreateMediaItem - Regenerate search fields on updates Update HandleBulkUpdate handler: - Add tag normalization with punctuation preference - Regenerate search fields when tags/contributors updated All handlers now populate both display and search fields, ensuring consistent normalization throughout the application. Relates to Tags & Contributors Migration Phase 6
This commit is contained in:
+78
-51
@@ -467,26 +467,28 @@ func (h *MediaHandler) HandleBulkUpdate(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateParams := database.UpdateMediaItemParams{
|
updateParams := database.UpdateMediaItemParams{
|
||||||
ID: bookUUID,
|
ID: bookUUID,
|
||||||
Title: existingBook.Title,
|
Title: existingBook.Title,
|
||||||
Author: existingBook.Author,
|
Author: existingBook.Author,
|
||||||
Genre: existingBook.Genre,
|
Genre: existingBook.Genre,
|
||||||
Language: existingBook.Language,
|
Language: existingBook.Language,
|
||||||
Tags: existingBook.Tags,
|
Tags: existingBook.Tags,
|
||||||
Description: existingBook.Description,
|
TagsSearch: existingBook.TagsSearch,
|
||||||
Publisher: existingBook.Publisher,
|
Description: existingBook.Description,
|
||||||
CopyrightYear: existingBook.CopyrightYear,
|
Publisher: existingBook.Publisher,
|
||||||
Isbn: existingBook.Isbn,
|
CopyrightYear: existingBook.CopyrightYear,
|
||||||
Series: existingBook.Series,
|
Isbn: existingBook.Isbn,
|
||||||
SeriesNumber: existingBook.SeriesNumber,
|
Series: existingBook.Series,
|
||||||
Asin: existingBook.Asin,
|
SeriesNumber: existingBook.SeriesNumber,
|
||||||
DatePublished: existingBook.DatePublished,
|
Asin: existingBook.Asin,
|
||||||
Contributors: existingBook.Contributors,
|
DatePublished: existingBook.DatePublished,
|
||||||
Edition: existingBook.Edition,
|
Contributors: existingBook.Contributors,
|
||||||
PageCount: existingBook.PageCount,
|
ContributorsSearch: existingBook.ContributorsSearch,
|
||||||
GoodreadsID: existingBook.GoodreadsID,
|
Edition: existingBook.Edition,
|
||||||
OpenlibraryID: existingBook.OpenlibraryID,
|
PageCount: existingBook.PageCount,
|
||||||
CoverImagePath: existingBook.CoverImagePath,
|
GoodreadsID: existingBook.GoodreadsID,
|
||||||
|
OpenlibraryID: existingBook.OpenlibraryID,
|
||||||
|
CoverImagePath: existingBook.CoverImagePath,
|
||||||
}
|
}
|
||||||
|
|
||||||
if update.Updates.Title != nil {
|
if update.Updates.Title != nil {
|
||||||
@@ -502,7 +504,10 @@ func (h *MediaHandler) HandleBulkUpdate(c echo.Context) error {
|
|||||||
updateParams.Language = pgtype.Text{String: *update.Updates.Language, Valid: true}
|
updateParams.Language = pgtype.Text{String: *update.Updates.Language, Valid: true}
|
||||||
}
|
}
|
||||||
if len(update.Updates.Tags) > 0 {
|
if len(update.Updates.Tags) > 0 {
|
||||||
updateParams.Tags = update.Updates.Tags
|
normalizedTags := utils.NormalizeTags(update.Updates.Tags)
|
||||||
|
updateParams.Tags = normalizedTags
|
||||||
|
tagsSearch := utils.NormalizeTagsSearch(update.Updates.Tags)
|
||||||
|
updateParams.TagsSearch = tagsSearch
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = h.db.UpdateMediaItem(c.Request().Context(), updateParams)
|
_, err = h.db.UpdateMediaItem(c.Request().Context(), updateParams)
|
||||||
@@ -893,6 +898,13 @@ func (mh *MediaHandler) CreateMediaItem(c echo.Context) error {
|
|||||||
req.Tags = utils.NormalizeTags(req.Tags)
|
req.Tags = utils.NormalizeTags(req.Tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(req.Contributors) > 0 {
|
||||||
|
req.Contributors = utils.NormalizeContributors(req.Contributors)
|
||||||
|
}
|
||||||
|
|
||||||
|
tagsSearch := utils.NormalizeTagsSearch(req.Tags)
|
||||||
|
contributorsSearch := utils.NormalizeContributorsSearch(req.Contributors)
|
||||||
|
|
||||||
_, err := mh.db.GetLibrary(c.Request().Context(), pgtype.UUID{Bytes: req.LibraryID, Valid: true})
|
_, err := mh.db.GetLibrary(c.Request().Context(), pgtype.UUID{Bytes: req.LibraryID, Valid: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == pgx.ErrNoRows {
|
if err == pgx.ErrNoRows {
|
||||||
@@ -902,23 +914,25 @@ func (mh *MediaHandler) CreateMediaItem(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mediaItemID, err := mh.db.CreateMediaItem(c.Request().Context(), database.CreateMediaItemParams{
|
mediaItemID, err := mh.db.CreateMediaItem(c.Request().Context(), database.CreateMediaItemParams{
|
||||||
LibraryID: pgtype.UUID{Bytes: req.LibraryID, Valid: true},
|
LibraryID: pgtype.UUID{Bytes: req.LibraryID, Valid: true},
|
||||||
Title: req.Title,
|
Title: req.Title,
|
||||||
Author: pgtype.Text{String: req.Author, Valid: req.Author != ""},
|
Author: pgtype.Text{String: req.Author, Valid: req.Author != ""},
|
||||||
Isbn: pgtype.Text{String: req.ISBN, Valid: req.ISBN != ""},
|
Isbn: pgtype.Text{String: req.ISBN, Valid: req.ISBN != ""},
|
||||||
Description: pgtype.Text{String: req.Description, Valid: req.Description != ""},
|
Description: pgtype.Text{String: req.Description, Valid: req.Description != ""},
|
||||||
FilePath: req.FilePath,
|
FilePath: req.FilePath,
|
||||||
FileSize: pgtype.Int8{Int64: req.FileSize, Valid: req.FileSize > 0},
|
FileSize: pgtype.Int8{Int64: req.FileSize, Valid: req.FileSize > 0},
|
||||||
MimeType: pgtype.Text{String: req.MimeType, Valid: req.MimeType != ""},
|
MimeType: pgtype.Text{String: req.MimeType, Valid: req.MimeType != ""},
|
||||||
CoverImagePath: pgtype.Text{String: req.CoverImagePath, Valid: req.CoverImagePath != ""},
|
CoverImagePath: pgtype.Text{String: req.CoverImagePath, Valid: req.CoverImagePath != ""},
|
||||||
Series: pgtype.Text{String: req.Series, Valid: req.Series != ""},
|
Series: pgtype.Text{String: req.Series, Valid: req.Series != ""},
|
||||||
SeriesNumber: pgtype.Int4{Int32: req.SeriesNumber, Valid: req.SeriesNumber > 0},
|
SeriesNumber: pgtype.Int4{Int32: req.SeriesNumber, Valid: req.SeriesNumber > 0},
|
||||||
Tags: req.Tags,
|
Tags: req.Tags,
|
||||||
Asin: pgtype.Text{String: req.ASIN, Valid: req.ASIN != ""},
|
TagsSearch: tagsSearch,
|
||||||
DatePublished: pgtype.Date{Time: parseDate(req.DatePublished), Valid: req.DatePublished != ""},
|
Asin: pgtype.Text{String: req.ASIN, Valid: req.ASIN != ""},
|
||||||
Publisher: pgtype.Text{String: req.Publisher, Valid: req.Publisher != ""},
|
DatePublished: pgtype.Date{Time: parseDate(req.DatePublished), Valid: req.DatePublished != ""},
|
||||||
Contributors: req.Contributors,
|
Publisher: pgtype.Text{String: req.Publisher, Valid: req.Publisher != ""},
|
||||||
AddedByAdminID: user.ID,
|
Contributors: req.Contributors,
|
||||||
|
ContributorsSearch: contributorsSearch,
|
||||||
|
AddedByAdminID: user.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
@@ -954,20 +968,33 @@ func (mh *MediaHandler) UpdateMediaItem(c echo.Context) error {
|
|||||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(req.Tags) > 0 {
|
||||||
|
req.Tags = utils.NormalizeTags(req.Tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(req.Contributors) > 0 {
|
||||||
|
req.Contributors = utils.NormalizeContributors(req.Contributors)
|
||||||
|
}
|
||||||
|
|
||||||
|
tagsSearch := utils.NormalizeTagsSearch(req.Tags)
|
||||||
|
contributorsSearch := utils.NormalizeContributorsSearch(req.Contributors)
|
||||||
|
|
||||||
item, err := mh.db.UpdateMediaItem(c.Request().Context(), database.UpdateMediaItemParams{
|
item, err := mh.db.UpdateMediaItem(c.Request().Context(), database.UpdateMediaItemParams{
|
||||||
ID: pgtype.UUID{Bytes: mediaUUID, Valid: true},
|
ID: pgtype.UUID{Bytes: mediaUUID, Valid: true},
|
||||||
Title: req.Title,
|
Title: req.Title,
|
||||||
Author: pgtype.Text{String: req.Author, Valid: req.Author != ""},
|
Author: pgtype.Text{String: req.Author, Valid: req.Author != ""},
|
||||||
Isbn: pgtype.Text{String: req.ISBN, Valid: req.ISBN != ""},
|
Isbn: pgtype.Text{String: req.ISBN, Valid: req.ISBN != ""},
|
||||||
Description: pgtype.Text{String: req.Description, Valid: req.Description != ""},
|
Description: pgtype.Text{String: req.Description, Valid: req.Description != ""},
|
||||||
CoverImagePath: pgtype.Text{String: req.CoverImagePath, Valid: req.CoverImagePath != ""},
|
CoverImagePath: pgtype.Text{String: req.CoverImagePath, Valid: req.CoverImagePath != ""},
|
||||||
Series: pgtype.Text{String: req.Series, Valid: req.Series != ""},
|
Series: pgtype.Text{String: req.Series, Valid: req.Series != ""},
|
||||||
SeriesNumber: pgtype.Int4{Int32: req.SeriesNumber, Valid: req.SeriesNumber > 0},
|
SeriesNumber: pgtype.Int4{Int32: req.SeriesNumber, Valid: req.SeriesNumber > 0},
|
||||||
Tags: req.Tags,
|
Tags: req.Tags,
|
||||||
Asin: pgtype.Text{String: req.ASIN, Valid: req.ASIN != ""},
|
TagsSearch: tagsSearch,
|
||||||
DatePublished: pgtype.Date{Time: parseDate(req.DatePublished), Valid: req.DatePublished != ""},
|
Asin: pgtype.Text{String: req.ASIN, Valid: req.ASIN != ""},
|
||||||
Publisher: pgtype.Text{String: req.Publisher, Valid: req.Publisher != ""},
|
DatePublished: pgtype.Date{Time: parseDate(req.DatePublished), Valid: req.DatePublished != ""},
|
||||||
Contributors: req.Contributors,
|
Publisher: pgtype.Text{String: req.Publisher, Valid: req.Publisher != ""},
|
||||||
|
Contributors: req.Contributors,
|
||||||
|
ContributorsSearch: contributorsSearch,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
|
|||||||
Reference in New Issue
Block a user