aboutsummaryrefslogtreecommitdiff
path: root/repositories/mapping.go
blob: 145bb43e44d57b16985cb859bfbc58bf4404c20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package repositories

import (
	"errors"
	"fmt"
	"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 := 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 := 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
}

func GetAllMappings() ([]entities.Mapping, error) {
	var mappings []entities.Mapping

	result := DB.Find(&mappings)
	if result.Error != nil {
		logger.Errorf("Mapping", "Failed to fetch all mappings: %v", result.Error)
		return nil, errors.New("failed to fetch mappings")
	}

	return mappings, nil
}