From 3eb8e61b195f0daeba1be94bc930ebe759842c88 Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Fri, 19 Feb 2021 11:27:36 +0530 Subject: [PATCH] working on tags --- db/db.go | 2 +- db/dbfunctions.go | 26 ++++++++++++++++++++++++++ db/podcast.go | 9 +++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/db/db.go b/db/db.go index e866dc8..0d3f703 100644 --- a/db/db.go +++ b/db/db.go @@ -35,7 +35,7 @@ func Init() (*gorm.DB, error) { //Migrate Database func Migrate() { - DB.AutoMigrate(&Podcast{}, &PodcastItem{}, &Setting{}, &Migration{}, &JobLock{}) + DB.AutoMigrate(&Podcast{}, &PodcastItem{}, &Setting{}, &Migration{}, &JobLock{}, &Tag{}) RunMigrations() } diff --git a/db/dbfunctions.go b/db/dbfunctions.go index 6d92eaf..cbbf52d 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -236,3 +236,29 @@ func UnlockMissedJobs() { } } } + +func GetAllTags(sorting string) (*[]Tag, error) { + var tags []Tag + if sorting == "" { + sorting = "created_at" + } + result := DB.Debug().Order(sorting).Find(&tags) + return &tags, result.Error +} + +func GetTagById(id string) (*Tag, error) { + var tag Tag + result := DB.Preload(clause.Associations). + First(&tag, "id=?", id) + + return &tag, result.Error +} + +func CreateTag(tag *Tag) error { + tx := DB.Omit("Podcasts").Create(&tag) + return tx.Error +} +func UpdateTag(tag *Tag) error { + tx := DB.Omit("Podcast").Save(&tag) + return tx.Error +} diff --git a/db/podcast.go b/db/podcast.go index 54bc557..42e5606 100644 --- a/db/podcast.go +++ b/db/podcast.go @@ -21,6 +21,8 @@ type Podcast struct { PodcastItems []PodcastItem + Tag []*Tag `gorm:"many2many:podcast_tags;"` + DownloadedEpisodesCount int `gorm:"-"` DownloadingEpisodesCount int `gorm:"-"` AllEpisodesCount int `gorm:"-"` @@ -85,6 +87,13 @@ type JobLock struct { Duration int } +type Tag struct { + Base + Label string + Description string `gorm:"type:text"` + Podcasts []*Podcast `gorm:"many2many:podcast_tags;"` +} + func (lock *JobLock) IsLocked() bool { return lock != nil && lock.Date != time.Time{} }