Added: downloading and returning wikipedia articles
This commit is contained in:
78
api/article/handler.go
Normal file
78
api/article/handler.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package article
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"scrap/api/httpio"
|
||||
"scrap/internal/article"
|
||||
"scrap/internal/db"
|
||||
)
|
||||
|
||||
func ArticleDownloadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
dbInstance := db.GetInstance()
|
||||
txRepo := db.NewTxRepository(dbInstance)
|
||||
articleRepo := article.NewArticleRepository()
|
||||
|
||||
service := article.NewArticleService(txRepo, articleRepo)
|
||||
if err := service.DownloadArticles(); err != nil {
|
||||
switch err {
|
||||
default:
|
||||
httpio.RaiseOnlyStatusCode(w, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func ArticleQueryHandler(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := httpio.ParseURLQuery[ArticleQueryRequest](
|
||||
r,
|
||||
httpio.URLQueryKey[string]("title"),
|
||||
)
|
||||
if err != nil {
|
||||
httpio.RaiseOnlyStatusCode(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if httpErr := body.Validate(); httpErr != nil {
|
||||
httpErr.Raise(w)
|
||||
return
|
||||
}
|
||||
|
||||
dbInstance := db.GetInstance()
|
||||
txRepo := db.NewTxRepository(dbInstance)
|
||||
articleRepo := article.NewArticleRepository()
|
||||
|
||||
service := article.NewArticleService(txRepo, articleRepo)
|
||||
|
||||
articleQueryData := article.ArticleQueryDTO{
|
||||
Title: body.Title,
|
||||
}
|
||||
|
||||
articles, err := service.QueryArticles(articleQueryData)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case article.ErrArticleTitleInvalidLength:
|
||||
ErrHttpArticleTitleInvalidLength.Raise(w)
|
||||
default:
|
||||
httpio.RaiseOnlyStatusCode(w, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
articlesOut := make([]ArticleResponse, 0, len(articles))
|
||||
for _, a := range articles {
|
||||
ar := ArticleResponse{
|
||||
Uuid: a.Uuid,
|
||||
Title: a.Title,
|
||||
Content: a.Content,
|
||||
}
|
||||
|
||||
articlesOut = append(articlesOut, ar)
|
||||
}
|
||||
|
||||
if err = ArticleQueryResponse(articlesOut).Return(w, http.StatusOK); err != nil {
|
||||
httpio.RaiseOnlyStatusCode(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
14
api/article/httperror.go
Normal file
14
api/article/httperror.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package article
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"scrap/api/httpio"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHttpArticleTitleInvalidLength = httpio.HTTPError{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
ErrorCode: "ARTICLE_TITLE_LENGTH",
|
||||
Message: "Invalid title length.",
|
||||
}
|
||||
)
|
||||
16
api/article/request.go
Normal file
16
api/article/request.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package article
|
||||
|
||||
import "scrap/api/httpio"
|
||||
|
||||
type ArticleQueryRequest struct {
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
func (a ArticleQueryRequest) Validate() *httpio.HTTPError {
|
||||
titleLength := len(a.Title)
|
||||
if titleLength < 1 || titleLength > 255 {
|
||||
return &ErrHttpArticleTitleInvalidLength
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
15
api/article/response.go
Normal file
15
api/article/response.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package article
|
||||
|
||||
import "scrap/api/httpio"
|
||||
|
||||
type ArticleResponse struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func ArticleQueryResponse(articles []ArticleResponse) httpio.ResponseIO {
|
||||
return httpio.ResponseIO{
|
||||
"articles": articles,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user