diff options
| author | Bobby <[email protected]> | 2026-02-05 15:56:01 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-02-05 15:56:01 +0530 |
| commit | 111ddd8b5fca2612256a7bd31781c149f10f83d8 (patch) | |
| tree | 23256435338a22f31f3dc52331ae2f04705ec1f8 /repositories/mapping.go | |
| parent | b0f01eea9d61aa4d05b0fe253c8a32e35fa95e28 (diff) | |
| download | metachan-111ddd8b5fca2612256a7bd31781c149f10f83d8.tar.xz metachan-111ddd8b5fca2612256a7bd31781c149f10f83d8.zip | |
Refactor Jikan API types: remove unused structures and add HTTP client configuration
Diffstat (limited to 'repositories/mapping.go')
| -rw-r--r-- | repositories/mapping.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/repositories/mapping.go b/repositories/mapping.go new file mode 100644 index 0000000..e797bc3 --- /dev/null +++ b/repositories/mapping.go @@ -0,0 +1,43 @@ +package repositories + +import ( + "errors" + "fmt" + "metachan/database" + "metachan/entities" + "metachan/enums" + "metachan/utils/logger" + + "gorm.io/gorm/clause" +) + +func GetAnimeMapping[T idType](maptype enums.MappingType, id T) (entities.Mapping, error) { + var mapping entities.Mapping + + result := database.DB.Where(fmt.Sprintf("%s = ?", maptype), id).First(&mapping) + + if result.Error != nil { + logger.Errorf("Mapping", "Failed to get mapping for %s with ID %v: %v", maptype, id, result.Error) + return entities.Mapping{}, errors.New("mapping not found") + } + + return mapping, nil +} + +func CreateOrUpdateMapping(mapping *entities.Mapping) error { + result := database.DB.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "mal"}}, + DoUpdates: clause.AssignmentColumns([]string{ + "ani_db", "anilist", "anime_countdown", "anime_planet", + "ani_search", "imdb", "kitsu", "live_chart", "notify_moe", + "simkl", "tmdb", "tvdb", "type", "mal_anilist_composite", + }), + }).Create(mapping) + + if result.Error != nil { + logger.Errorf("Mapping", "Failed to create or update mapping: %v", result.Error) + return errors.New("failed to create or update mapping") + } + + return nil +} |
