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 ( ...@@ -11,8 +11,11 @@ import (
) )
func GetMatchesOfApi(apiUrl string) ([]*model.Match, error) { func GetMatchesOfApi(apiUrl string) ([]*model.Match, error) {
data := getData(apiUrl) data, err := getData(apiUrl)
matches, err := formatJsonCreateMatch(data) if err != nil {
return []*model.Match{}, err
}
matches, err := formatJsonToMatch(data)
if err != nil { if err != nil {
return []*model.Match{}, err return []*model.Match{}, err
} }
...@@ -20,11 +23,28 @@ func GetMatchesOfApi(apiUrl string) ([]*model.Match, error) { ...@@ -20,11 +23,28 @@ func GetMatchesOfApi(apiUrl string) ([]*model.Match, error) {
return matches, nil return matches, nil
} }
func getData(apiUrl string) []byte { func GetlatestMatchesOfApi(url string, updateChan chan<- int64) error {
request, error := http.NewRequest("GET", apiUrl, nil) 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 { time.Sleep(5 * time.Minute)
fmt.Println(error) return nil
}
func getData(apiUrl string) ([]byte, error) {
request, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
return []byte{}, err
} }
client := &http.Client{} client := &http.Client{}
response, error := client.Do(request) response, error := client.Do(request)
...@@ -33,17 +53,17 @@ func getData(apiUrl string) []byte { ...@@ -33,17 +53,17 @@ func getData(apiUrl string) []byte {
fmt.Println(error) fmt.Println(error)
} }
responseBody, error := io.ReadAll(response.Body) responseBody, err := io.ReadAll(response.Body)
if error != nil { if err != nil {
fmt.Println(error) return []byte{}, err
} }
defer response.Body.Close() 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 matches []*model.Match
var results []map[string]interface{} var results []map[string]interface{}
......
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"os" "os"
...@@ -15,12 +16,22 @@ import ( ...@@ -15,12 +16,22 @@ import (
) )
func main() { func main() {
updateChan := make(chan int64)
/*service.DeleteAllUsers() /*service.DeleteAllUsers()
var userArray []model.User var userArray []model.User
userArray, _ = service.GetAllUsers() userArray, _ = service.GetAllUsers()
fmt.Println(userArray) 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") matches, errMatches := api.GetMatchesOfApi("https://api.openligadb.de/getmatchesbyteamid/16/10/0")
if errMatches != nil { if errMatches != nil {
return return
...@@ -28,6 +39,7 @@ func main() { ...@@ -28,6 +39,7 @@ func main() {
for _, match := range matches { for _, match := range matches {
service.CreateMatch(match) service.CreateMatch(match)
} }
*/
if err := godotenv.Load(".env"); err != nil { if err := godotenv.Load(".env"); err != nil {
log.Fatalf("Error loading .env file") 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