87 lines
1.3 KiB
Go
87 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type repositorySummary struct {
|
|
ID int
|
|
Name string
|
|
Owner string
|
|
TotalStars int
|
|
}
|
|
|
|
type repositories struct {
|
|
Repositories []repositorySummary
|
|
}
|
|
|
|
func proba(w http.ResponseWriter, req *http.Request) {
|
|
repos := []repositorySummary{}
|
|
|
|
enableCors(&w)
|
|
|
|
rows, err := db.Query(`
|
|
SELECT
|
|
*
|
|
FROM repositories2`)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
repo := repositorySummary{}
|
|
err = rows.Scan(
|
|
&repo.ID,
|
|
&repo.Owner,
|
|
&repo.Name,
|
|
&repo.TotalStars,
|
|
)
|
|
if err != nil {
|
|
return
|
|
}
|
|
repos = append(repos, repo)
|
|
}
|
|
|
|
out, err := json.Marshal(repos)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
fmt.Fprintf(w, string(out))
|
|
}
|
|
|
|
func queryRepos(repos []repositorySummary) error {
|
|
rows, err := db.Query(`
|
|
SELECT
|
|
id,
|
|
repository_owner,
|
|
repository_name,
|
|
total_stars
|
|
FROM repositories2`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
repo := repositorySummary{}
|
|
err = rows.Scan(
|
|
&repo.ID,
|
|
&repo.Owner,
|
|
&repo.Name,
|
|
&repo.TotalStars,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
repos = append(repos, repo)
|
|
}
|
|
fmt.Println(repos)
|
|
err = rows.Err()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
} |