Skip to content

Commit

Permalink
use sites table
Browse files Browse the repository at this point in the history
  • Loading branch information
rupinr committed Oct 29, 2024
1 parent 14754ee commit 68383ae
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
6 changes: 4 additions & 2 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func main() {
}

s := dto.Subscription{
Sites: sub.Sites,
Sites: dto.MapToUrls(sub.Sites),
SubscriptionScheduleData: subData,
Confirmed: true,
}
Expand All @@ -195,9 +195,11 @@ func main() {
c.JSON(404, gin.H{"error": "Invalid request"})
} else {
sub, err := repository.GetSubscriptionByEmail(email)

fmt.Println(sub.Sites)
if err == nil {
subData := dto.Subscription{
Sites: sub.Sites,
Sites: dto.MapToUrls(sub.Sites),
Confirmed: sub.Confirmed,
SubscriptionScheduleData: dto.SubscriptionSchedule{
DailyFrequency: dto.DailyFrequency{
Expand Down
8 changes: 8 additions & 0 deletions datamodels/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"news-master/datamodels/entity"
)

func MapToUrls(sites []entity.Site) []string {
urls := make([]string, len(sites))
for i, site := range sites {
urls[i] = site.Url
}
return urls
}

type Topic struct {
Name string `json:"name" binding:"required"`
Visible bool `json:"visible"`
Expand Down
2 changes: 1 addition & 1 deletion datamodels/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ type Subscription struct {
UserID uint `gorm:"uniqueIndex"`
User User
Topics pq.StringArray `gorm:"type:text[]"`
Sites pq.StringArray `gorm:"type:text[]"`
SubscriptionScheduleID uint
SubscriptionSchedule SubscriptionSchedule
Confirmed bool
LastProcessedAt time.Time
Sites []Site `gorm:"many2many:subscription_sites;"`
}
type SubscriptionSchedule struct {
gorm.Model
Expand Down
23 changes: 14 additions & 9 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func GetSubscriptionByEmail(email string) (entity.Subscription, error) {
}
var subscription entity.Subscription
db().
Preload("Sites").
Joins("SubscriptionSchedule").
Joins("User").
Find(&subscription, entity.Subscription{UserID: user.ID})
Expand Down Expand Up @@ -190,11 +191,11 @@ func SetLastProcessedAt(subscriptionId uint) {
db().Save(&sub)
}

func GetArticlesAfterLastProcessedTime(fromDate time.Time, sites []string) []entity.Article {
func GetArticlesAfterLastProcessedTime(fromDate time.Time, sites []entity.Site) []entity.Article {
var articles []entity.Article
db().Where("created_at > ?", fromDate).Where("site IN ?", sites).Find(&articles)
dto.MapToUrls(sites)
db().Where("created_at > ?", fromDate).Where("site IN ?", dto.MapToUrls(sites)).Find(&articles)
return articles

}

func CreateSubscriptionSchedule(subscriptionScheduleData dto.SubscriptionSchedule) entity.SubscriptionSchedule {
Expand Down Expand Up @@ -235,16 +236,20 @@ func CreateSubscriptionSchedule(subscriptionScheduleData dto.SubscriptionSchedul
}

func CreateSubscription(user entity.User, sites []string, subscriptionScheduleID uint, isConfirmed bool) entity.Subscription {
attrs := entity.Subscription{
UserID: user.ID,
}
var subscription entity.Subscription
values := entity.Subscription{
Sites: pq.StringArray(sites),
SubscriptionScheduleID: subscriptionScheduleID,
Confirmed: isConfirmed,
}
var subscription entity.Subscription
db().Where(attrs).Assign(values).FirstOrCreate(&subscription)
db().Transaction(func(tx *gorm.DB) error {
var foundSites []entity.Site
tx.Where("url IN ?", sites).Where("active = ?", true).Find(&foundSites)

tx.Where(entity.Subscription{
UserID: user.ID,
}).Assign(values).FirstOrCreate(&subscription).Association("Sites").Append(foundSites)
return nil
})
return subscription
}

Expand Down

0 comments on commit 68383ae

Please sign in to comment.