From 3eece77e8414cf79940c95b82012880034b37503 Mon Sep 17 00:00:00 2001 From: junho100 Date: Sun, 22 Dec 2024 15:34:30 +0900 Subject: [PATCH] Feat: update PK --- internal/domain/entity/notification.go | 11 ++++++----- internal/http/dto/notification.go | 11 ++++++----- internal/infrastructure/crawler/crawler.go | 23 ++++++++++++---------- internal/repository/notification.go | 12 +++++------ 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/internal/domain/entity/notification.go b/internal/domain/entity/notification.go index e365427..af756b6 100644 --- a/internal/domain/entity/notification.go +++ b/internal/domain/entity/notification.go @@ -24,9 +24,10 @@ type NotificationConfigKeyword struct { } type SchoolNotification struct { - ID string `gorm:"primary_key"` - Title string - Date time.Time `gorm:"column:date;type:date"` - Content string `gorm:"column:content;type:text"` - Url string + UniqueID string `gorm:"type:varchar(36);primaryKey"` + ID string `gorm:"index"` + Title string + Date time.Time `gorm:"column:date;type:date"` + Content string `gorm:"column:content;type:text"` + Url string } diff --git a/internal/http/dto/notification.go b/internal/http/dto/notification.go index b95b720..4aafc4e 100644 --- a/internal/http/dto/notification.go +++ b/internal/http/dto/notification.go @@ -62,11 +62,12 @@ type FetchSchoolNoticeDto struct { } type GetNotificationByIDResponse struct { - ID string `json:"id"` - Title string `json:"title"` - Date time.Time `json:"date"` - Content string `json:"content"` - Url string `json:"url"` + UniqueID string `json:"unique_id"` + ID string `json:"id"` + Title string `json:"title"` + Date time.Time `json:"date"` + Content string `json:"content"` + Url string `json:"url"` } type ValidateDiscordClientIDRequest struct { diff --git a/internal/infrastructure/crawler/crawler.go b/internal/infrastructure/crawler/crawler.go index b42a0a5..ca5ecb3 100644 --- a/internal/infrastructure/crawler/crawler.go +++ b/internal/infrastructure/crawler/crawler.go @@ -9,6 +9,7 @@ import ( "time" "github.com/PuerkitoBio/goquery" + "github.com/google/uuid" ) const ( @@ -123,11 +124,12 @@ func (c *crawler) FetchSchoolNotices() ([]entity.SchoolNotification, error) { } notice := entity.SchoolNotification{ - ID: id, - Title: title, - Url: fullURL, - Date: parseDate(dateStr), - Content: content, + UniqueID: uuid.New().String(), + ID: id, + Title: title, + Url: fullURL, + Date: parseDate(dateStr), + Content: content, } notices = append(notices, notice) @@ -236,11 +238,12 @@ func (c *crawler) fetchDeptNoticesFromURL(url string, prefix string) ([]entity.S } notice := entity.SchoolNotification{ - ID: id, - Title: title, - Url: fullURL, - Date: parseDate(dateStr), - Content: content, + UniqueID: uuid.New().String(), + ID: id, + Title: title, + Url: fullURL, + Date: parseDate(dateStr), + Content: content, } notices = append(notices, notice) diff --git a/internal/repository/notification.go b/internal/repository/notification.go index a643b93..724cfe1 100644 --- a/internal/repository/notification.go +++ b/internal/repository/notification.go @@ -15,7 +15,7 @@ type notificationRepository struct { func (r *notificationRepository) FindNotificationByID(notification *entity.SchoolNotification, id string) error { if err := r.db.Where(&entity.SchoolNotification{ ID: id, - }).First(notification).Error; err != nil { + }).Order("date DESC").First(notification).Error; err != nil { return err } @@ -130,26 +130,26 @@ func (r *notificationRepository) SaveNotifications(notifications []entity.School return nil } - // 중복 체크를 위한 기존 ID 조회 - var existingIDs []string + // 중복 체크를 위한 기존 UniqueID 조회 + var existingUniqueIDs []string for _, notice := range notifications { var exists bool err := r.db.Model(&entity.SchoolNotification{}). Select("count(*) > 0"). - Where("id = ?", notice.ID). + Where("unique_id = ?", notice.UniqueID). Find(&exists).Error if err != nil { return err } if exists { - existingIDs = append(existingIDs, notice.ID) + existingUniqueIDs = append(existingUniqueIDs, notice.UniqueID) } } // 중복되지 않은 공지사항만 필터링 var newNotifications []entity.SchoolNotification for _, notice := range notifications { - if !contains(existingIDs, notice.ID) { + if !contains(existingUniqueIDs, notice.UniqueID) { newNotifications = append(newNotifications, notice) } }