Skip to content
Snippets Groups Projects
Commit 9d76126c authored by Yege1893's avatar Yege1893
Browse files

chanel watching for new matches

parent 16f6ce92
No related branches found
No related tags found
1 merge request!4Master
......@@ -11,8 +11,11 @@ import (
)
func GetMatchesOfApi(apiUrl string) ([]*model.Match, error) {
data := getData(apiUrl)
matches, err := formatJsonCreateMatch(data)
data, err := getData(apiUrl)
if err != nil {
return []*model.Match{}, err
}
matches, err := formatJsonToMatch(data)
if err != nil {
return []*model.Match{}, err
}
......@@ -20,11 +23,28 @@ func GetMatchesOfApi(apiUrl string) ([]*model.Match, error) {
return matches, nil
}
func getData(apiUrl string) []byte {
request, error := http.NewRequest("GET", apiUrl, nil)
func GetlatestMatchesOfApi(url string, updateChan chan<- int64) error {
data, err := getData(url)
if err != nil {
return err
}
matches, err := formatJsonToMatch(data)
if err != nil {
return err
}
for _, match := range matches {
updateChan <- match.ExternalID
}
if error != nil {
fmt.Println(error)
time.Sleep(5 * time.Minute)
return nil
}
func getData(apiUrl string) ([]byte, error) {
request, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
return []byte{}, err
}
client := &http.Client{}
response, error := client.Do(request)
......@@ -33,17 +53,17 @@ func getData(apiUrl string) []byte {
fmt.Println(error)
}
responseBody, error := io.ReadAll(response.Body)
responseBody, err := io.ReadAll(response.Body)
if error != nil {
fmt.Println(error)
if err != nil {
return []byte{}, err
}
defer response.Body.Close()
return responseBody
return responseBody, nil
}
func formatJsonCreateMatch(jsonArray []byte) ([]*model.Match, error) {
func formatJsonToMatch(jsonArray []byte) ([]*model.Match, error) {
var matches []*model.Match
var results []map[string]interface{}
......
package main
import (
"fmt"
"net/http"
"os"
......@@ -15,12 +16,22 @@ import (
)
func main() {
updateChan := make(chan int64)
/*service.DeleteAllUsers()
var userArray []model.User
userArray, _ = service.GetAllUsers()
fmt.Println(userArray)
*/
service.DeleteAllMatches()
go api.GetlatestMatchesOfApi("https://api.openligadb.de/getmatchesbyteamid/2/8/0", updateChan)
go func() {
for {
message := <-updateChan
fmt.Println("Empfangene Nachricht:", message)
}
}()
/*service.DeleteAllMatches()
matches, errMatches := api.GetMatchesOfApi("https://api.openligadb.de/getmatchesbyteamid/16/10/0")
if errMatches != nil {
return
......@@ -28,6 +39,7 @@ func main() {
for _, match := range matches {
service.CreateMatch(match)
}
*/
if err := godotenv.Load(".env"); err != nil {
log.Fatalf("Error loading .env file")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment