Refactor URL table row creation into the actions file

This commit is contained in:
Arturo Filastò 2018-09-07 15:23:29 +02:00
parent c2ea0c2a63
commit 6f0defc672
2 changed files with 48 additions and 33 deletions

View File

@ -125,3 +125,49 @@ func CreateNetwork(sess sqlbuilder.Database, location *utils.LocationInfo) (*Net
network.ID = newID.(int64) network.ID = newID.(int64)
return &network, nil return &network, nil
} }
// CreateOrUpdateURL will create a new URL entry to the urls table if it doesn't
// exists, otherwise it will update the category code of the one already in
// there.
func CreateOrUpdateURL(sess sqlbuilder.Database, url string, categoryCode string, countryCode string) (int64, error) {
var urlID int64
res, err := sess.Update("urls").Set(
"url", url,
"category_code", categoryCode,
"country_code", countryCode,
).Where("url = ? AND country_code = ?", url, countryCode).Exec()
if err != nil {
log.Error("Failed to write to the URL table")
return 0, err
}
affected, err := res.RowsAffected()
if err != nil {
log.Error("Failed to get affected row count")
return 0, err
}
if affected == 0 {
newID, err := sess.Collection("urls").Insert(
URL{
URL: sql.NullString{String: url, Valid: true},
CategoryCode: sql.NullString{String: categoryCode, Valid: true},
CountryCode: sql.NullString{String: countryCode, Valid: true},
})
if err != nil {
log.Error("Failed to insert into the URLs table")
return 0, err
}
urlID = newID.(int64)
} else {
lastID, err := res.LastInsertId()
if err != nil {
log.Error("failed to get URL ID")
return 0, err
}
urlID = lastID
}
return urlID, nil
}

View File

@ -53,41 +53,10 @@ func lookupURLs(ctl *nettests.Controller) ([]string, map[int64]int64, error) {
} }
for idx, url := range parsed.Results { for idx, url := range parsed.Results {
var urlID int64 urlID, err := database.CreateOrUpdateURL(ctl.Ctx.DB, url.URL, url.CategoryCode, url.CountryCode)
res, err := ctl.Ctx.DB.Update("urls").Set(
"url", url.URL,
"category_code", url.CategoryCode,
"country_code", url.CountryCode,
).Where("url = ? AND country_code = ?", url.URL, url.CountryCode).Exec()
if err != nil { if err != nil {
log.Error("Failed to write to the URL table") log.Error("failed to add to the URL table")
} else {
affected, err := res.RowsAffected()
if err != nil {
log.Error("Failed to get affected row count")
} else if affected == 0 {
newID, err := ctl.Ctx.DB.Collection("urls").Insert(
database.URL{
URL: url.URL,
CategoryCode: url.CategoryCode,
CountryCode: url.CountryCode,
})
if err != nil {
log.Error("Failed to insert into the URLs table")
}
urlID = newID.(int64)
} else {
lastID, err := res.LastInsertId()
if err != nil {
log.Error("failed to get URL ID")
}
urlID = lastID
}
} }
urlIDMap[int64(idx)] = urlID urlIDMap[int64(idx)] = urlID
urls = append(urls, url.URL) urls = append(urls, url.URL)
} }