feat: seed default dataset sources
This commit is contained in:
@@ -22,6 +22,7 @@ type SourceDTO struct {
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind"`
|
||||
URL string `json:"url"`
|
||||
IconURL string `json:"iconUrl"`
|
||||
Description string `json:"description"`
|
||||
Enabled bool `json:"enabled"`
|
||||
LastSyncedAt *time.Time `json:"lastSyncedAt"`
|
||||
@@ -61,6 +62,7 @@ type sourceRequest struct {
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind"`
|
||||
URL string `json:"url"`
|
||||
IconURL string `json:"iconUrl"`
|
||||
Description string `json:"description"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
}
|
||||
@@ -113,7 +115,8 @@ func (h *Handler) createSource(c *gin.Context) {
|
||||
enabled = *input.Enabled
|
||||
}
|
||||
source, err := h.service.CreateSource(userID, SourceInput{
|
||||
Name: input.Name, Kind: input.Kind, URL: input.URL, Description: input.Description, Enabled: enabled,
|
||||
Name: input.Name, Kind: input.Kind, URL: input.URL, IconURL: input.IconURL,
|
||||
Description: input.Description, Enabled: enabled,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
@@ -141,7 +144,8 @@ func (h *Handler) updateSource(c *gin.Context) {
|
||||
enabled = *input.Enabled
|
||||
}
|
||||
source, err := h.service.UpdateSource(userID, identity, SourceInput{
|
||||
Name: input.Name, Kind: input.Kind, URL: input.URL, Description: input.Description, Enabled: enabled,
|
||||
Name: input.Name, Kind: input.Kind, URL: input.URL, IconURL: input.IconURL,
|
||||
Description: input.Description, Enabled: enabled,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
@@ -308,7 +312,7 @@ func writeInvalidRequest(c *gin.Context) {
|
||||
func sourceDTO(source models.SaDatasetSource, itemCount int64) SourceDTO {
|
||||
return SourceDTO{
|
||||
ID: source.Identity, Name: source.Name, Kind: source.Kind, URL: source.URL,
|
||||
Description: source.Description, Enabled: source.Enabled,
|
||||
IconURL: source.IconURL, Description: source.Description, Enabled: source.Enabled,
|
||||
LastSyncedAt: utcTime(source.LastSyncedAt), ItemCount: itemCount,
|
||||
CreatedAt: source.CreatedAt.UTC(), UpdatedAt: source.UpdatedAt.UTC(),
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ func TestDatasetAPIConnectsSourcesItemsAndSyncTasks(t *testing.T) {
|
||||
require.NoError(t, json.Unmarshal(createdSource.Body.Bytes(), &source))
|
||||
require.NotEmpty(t, source.ID)
|
||||
require.Equal(t, "Industry feed", source.Name)
|
||||
require.Empty(t, source.IconURL)
|
||||
|
||||
createdItem := performDatasetRequest(t, ownerRouter, http.MethodPost, "/api/v1/dataset-items", map[string]any{
|
||||
"sourceId": source.ID, "title": "A collected article", "summary": "Summary",
|
||||
|
||||
@@ -26,6 +26,7 @@ type SourceInput struct {
|
||||
Name string
|
||||
Kind string
|
||||
URL string
|
||||
IconURL string
|
||||
Description string
|
||||
Enabled bool
|
||||
}
|
||||
@@ -93,7 +94,8 @@ func (s *Service) CreateSource(userID uint, input SourceInput) (*models.SaDatase
|
||||
}
|
||||
source := &models.SaDatasetSource{
|
||||
CreatedBy: userID, Name: normalized.Name, Kind: normalized.Kind,
|
||||
URL: normalized.URL, Description: normalized.Description, Enabled: normalized.Enabled,
|
||||
URL: normalized.URL, IconURL: normalized.IconURL,
|
||||
Description: normalized.Description, Enabled: normalized.Enabled,
|
||||
}
|
||||
return source, s.db.Create(source).Error
|
||||
}
|
||||
@@ -109,7 +111,7 @@ func (s *Service) UpdateSource(userID uint, identity string, input SourceInput)
|
||||
}
|
||||
if err := s.db.Model(&source).Updates(map[string]any{
|
||||
"name": normalized.Name, "kind": normalized.Kind, "url": normalized.URL,
|
||||
"description": normalized.Description, "enabled": normalized.Enabled,
|
||||
"icon_url": normalized.IconURL, "description": normalized.Description, "enabled": normalized.Enabled,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -248,6 +250,7 @@ func normalizeSourceInput(input SourceInput) (SourceInput, error) {
|
||||
input.Name = strings.TrimSpace(input.Name)
|
||||
input.Kind = strings.ToLower(strings.TrimSpace(input.Kind))
|
||||
input.URL = strings.TrimSpace(input.URL)
|
||||
input.IconURL = strings.TrimSpace(input.IconURL)
|
||||
input.Description = strings.TrimSpace(input.Description)
|
||||
if input.Name == "" {
|
||||
return input, ErrNameRequired
|
||||
@@ -258,6 +261,9 @@ func normalizeSourceInput(input SourceInput) (SourceInput, error) {
|
||||
if input.URL != "" && !validHTTPURL(input.URL) {
|
||||
return input, ErrURLInvalid
|
||||
}
|
||||
if input.IconURL != "" && !validHTTPURL(input.IconURL) {
|
||||
return input, ErrURLInvalid
|
||||
}
|
||||
if input.Kind != "manual" && input.URL == "" {
|
||||
return input, ErrURLInvalid
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user