directory/internal/store/website.go

52 lines
981 B
Go
Raw Permalink Normal View History

2024-06-01 23:43:00 +00:00
package store
import (
"crypto/rand"
"time"
"github.com/oklog/ulid/v2"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
type Site struct {
2024-06-15 14:41:37 +00:00
ID ulid.ULID `gorm:"primaryKey"`
Domain string `gorm:"unique"`
2024-06-01 23:43:00 +00:00
URL string
CreatedAt *time.Time
FirstSeen *time.Time
LastSeen *time.Time
Tags []SiteTag `gorm:"foreignKey:SiteID"`
db *gorm.DB `gorm:"-"`
}
func (Site) TableName() string {
return "sites"
}
func (s *Store) AddSite(url string) (*Site, error) {
2024-06-15 14:41:37 +00:00
ID := ulid.MustNew(ulid.Timestamp(time.Now()), rand.Reader)
2024-06-01 23:43:00 +00:00
createdAt := time.Now()
site := &Site{
ID: ID,
URL: url,
CreatedAt: &createdAt,
db: s.db,
}
result := s.db.Create(site)
if result.Error != nil {
log.Error().
2024-06-15 14:41:37 +00:00
Str("siteid", ID.String()).
2024-06-01 23:43:00 +00:00
Err(result.Error).
Msg("unable to insert site into db")
return nil, result.Error
}
log.Debug().
2024-06-15 14:41:37 +00:00
Str("siteid", ID.String()).
2024-06-01 23:43:00 +00:00
Str("url", url).
Msg("site added to db")
return site, nil
}