Added: downloading and returning wikipedia articles
This commit is contained in:
107
internal/article/service.go
Normal file
107
internal/article/service.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package article
|
||||
|
||||
import (
|
||||
"log"
|
||||
"scrap/internal/db"
|
||||
"scrap/internal/wikipediadl"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ArticleService struct {
|
||||
txRepo db.ITxRepository
|
||||
articleRepo IArticleRepository
|
||||
}
|
||||
|
||||
func NewArticleService(
|
||||
txRepo db.ITxRepository,
|
||||
articleRepo IArticleRepository,
|
||||
) IArticleService {
|
||||
return &ArticleService{
|
||||
txRepo: txRepo,
|
||||
articleRepo: articleRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (a ArticleService) QueryArticles(data ArticleQueryDTO) ([]ArticleDTO, error) {
|
||||
tx, err := a.txRepo.Begin()
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer a.txRepo.RollbackOnError(tx, &err)
|
||||
|
||||
articleTitleLength := len(data.Title)
|
||||
if articleTitleLength < 1 || articleTitleLength > 255 {
|
||||
return nil, ErrArticleTitleInvalidLength
|
||||
}
|
||||
|
||||
articles, err := a.articleRepo.GetArticlesByTitle(tx, data.Title)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return nil, ErrArticleQueryFailed
|
||||
}
|
||||
|
||||
articlesOut := make([]ArticleDTO, 0, len(articles))
|
||||
for _, am := range articles {
|
||||
a := ArticleDTO{
|
||||
Uuid: am.Uuid,
|
||||
Title: am.Title,
|
||||
Content: am.Content,
|
||||
}
|
||||
|
||||
articlesOut = append(articlesOut, a)
|
||||
}
|
||||
|
||||
return articlesOut, nil
|
||||
}
|
||||
|
||||
func (a ArticleService) DownloadArticles() error {
|
||||
tx, err := a.txRepo.Begin()
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return db.ErrTxBeginFailed
|
||||
}
|
||||
|
||||
defer a.txRepo.RollbackOnError(tx, &err)
|
||||
|
||||
if err = a.articleRepo.DeleteAllArticles(tx); err != nil {
|
||||
log.Println(err.Error())
|
||||
return ErrArticleDeleteAllFailed
|
||||
}
|
||||
|
||||
articleBundles, err := wikipediadl.FetchArticleBundles()
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return ErrArticleDownloadFailed
|
||||
}
|
||||
|
||||
for _, ab := range articleBundles {
|
||||
articles, err := wikipediadl.ExtractArticles(ab)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return ErrArticleDownloadFailed
|
||||
}
|
||||
|
||||
for _, article := range articles {
|
||||
articleData := ArticleCreateModel{
|
||||
Uuid: uuid.NewString(),
|
||||
Title: article.Title,
|
||||
Content: article.Revision.Text,
|
||||
}
|
||||
|
||||
if err = a.articleRepo.CreateArticle(tx, articleData); err != nil {
|
||||
log.Println(err.Error(), "tutaj ---------")
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err = a.txRepo.Commit(tx); err != nil {
|
||||
log.Println(err.Error())
|
||||
return db.ErrTxCommitFailed
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user