Skip to content

Commit

Permalink
Feat: update PK
Browse files Browse the repository at this point in the history
  • Loading branch information
junho100 committed Dec 22, 2024
1 parent 18bb613 commit 3eece77
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
11 changes: 6 additions & 5 deletions internal/domain/entity/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 6 additions & 5 deletions internal/http/dto/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 13 additions & 10 deletions internal/infrastructure/crawler/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/PuerkitoBio/goquery"
"github.com/google/uuid"
)

const (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions internal/repository/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 3eece77

Please sign in to comment.