Skip to content
Snippets Groups Projects
Commit 0bcbbc0f authored by Yege1893's avatar Yege1893
Browse files

current

parent 3fba29d9
No related branches found
No related tags found
1 merge request!4Master
...@@ -10,11 +10,14 @@ import ( ...@@ -10,11 +10,14 @@ import (
"gitlab.reutlingen-university.de/ege/highlander-ticketing-go-ss2023/src/highlanderticketing/model" "gitlab.reutlingen-university.de/ege/highlander-ticketing-go-ss2023/src/highlanderticketing/model"
) )
func GetMatchesOfApi(apiUrl string) []*model.Match { func GetMatchesOfApi(apiUrl string) (error, []*model.Match) {
data := getData(apiUrl) data := getData(apiUrl)
fmt.Println(data) err, matches := formatJsonCreateMatch(data)
matches := formatJsonToMatches(data) if err != nil {
return matches return err, make([]*model.Match, 0)
}
fmt.Println(matches)
return nil, matches
} }
func getData(apiUrl string) []byte { func getData(apiUrl string) []byte {
...@@ -40,20 +43,21 @@ func getData(apiUrl string) []byte { ...@@ -40,20 +43,21 @@ func getData(apiUrl string) []byte {
return responseBody return responseBody
} }
func formatJsonToMatches(jsonArray []byte) []*model.Match { func formatJsonCreateMatch(jsonArray []byte) (error, []*model.Match) {
var match *model.Match var match model.Match
var matches []*model.Match var matches []*model.Match
var results []map[string]interface{} var results []map[string]interface{}
json.Unmarshal([]byte(jsonArray), &results) json.Unmarshal([]byte(jsonArray), &results)
for _, result := range results { for _, result := range results {
fmt.Println(result, "result")
match.ExternalID = int64(result["matchID"].(float64)) match.ExternalID = int64(result["matchID"].(float64))
fmt.Println(match.ExternalID)
match.LeagueName = result["leagueName"].(string) match.LeagueName = result["leagueName"].(string)
match.Date = result["matchDateTime"].(time.Time) matchDate, err := time.Parse("2006-01-02T15:04:05", result["matchDateTime"].(string))
fmt.Println(*match) if err != nil {
return err, matches
}
match.Date = matchDate
if team1, ok := result["team1"].(map[string]interface{}); ok { if team1, ok := result["team1"].(map[string]interface{}); ok {
if name, ok := team1["shortName"].(string); ok { if name, ok := team1["shortName"].(string); ok {
...@@ -78,8 +82,7 @@ func formatJsonToMatches(jsonArray []byte) []*model.Match { ...@@ -78,8 +82,7 @@ func formatJsonToMatches(jsonArray []byte) []*model.Match {
} }
} }
} }
fmt.Println(&matches) matches = append(matches, &match)
matches = append(matches, match)
} }
return matches return nil, matches
} }
...@@ -117,6 +117,32 @@ func DeleteMatch(w http.ResponseWriter, r *http.Request) { ...@@ -117,6 +117,32 @@ func DeleteMatch(w http.ResponseWriter, r *http.Request) {
sendJson(w, result{Success: "OK"}) sendJson(w, result{Success: "OK"})
} }
func UpdateTickets(w http.ResponseWriter, r *http.Request) {
if err := CheckAccessToken(w, r, true); err != nil {
log.Errorf("Eror checking AccessToken: %v", err)
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
id, err := getID(r)
if err != nil {
log.Errorf("Please parse in ID at the url %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
MatchToUpdate, err := getMatch(r)
if err != nil {
log.Errorf("Match not found %v", err)
return
}
MatchUpdated, err := service.UpdateTickets(id, MatchToUpdate)
if err != nil {
log.Errorf("Match could not be updated %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sendJson(w, MatchUpdated)
}
// nur intern // nur intern
func DeleteAllMatches(w http.ResponseWriter, r *http.Request) { func DeleteAllMatches(w http.ResponseWriter, r *http.Request) {
err := service.DeleteAllMatches() err := service.DeleteAllMatches()
......
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"os" "os"
...@@ -17,12 +16,18 @@ import ( ...@@ -17,12 +16,18 @@ import (
func main() { func main() {
/*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() service.DeleteAllMatches()
matches := api.GetMatchesOfApi("https://api.openligadb.de/getmatchesbyteamid/16/10/0") errMatches, matches := api.GetMatchesOfApi("https://api.openligadb.de/getmatchesbyteamid/16/10/0")
fmt.Println(&matches) if errMatches != nil {
return
}
for _, match := range matches {
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")
...@@ -52,6 +57,7 @@ func main() { ...@@ -52,6 +57,7 @@ func main() {
router.HandleFunc("/match/{id}", handler.GetMatchByID).Methods("GET") router.HandleFunc("/match/{id}", handler.GetMatchByID).Methods("GET")
router.HandleFunc("/match/{id}", handler.UpdateMatch).Methods("PUT") router.HandleFunc("/match/{id}", handler.UpdateMatch).Methods("PUT")
router.HandleFunc("/match/{id}", handler.DeleteMatch).Methods("DELETE") router.HandleFunc("/match/{id}", handler.DeleteMatch).Methods("DELETE")
router.HandleFunc("/match/{id}/updatetickets", handler.UpdateTickets).Methods("PUT")
router.HandleFunc("/match/{id}/matchorder", handler.AddMatchOrder).Methods("POST") router.HandleFunc("/match/{id}/matchorder", handler.AddMatchOrder).Methods("POST")
router.HandleFunc("/match/{id}/cancelorder/{orderid}", handler.CancelOrder).Methods("PUT") router.HandleFunc("/match/{id}/cancelorder/{orderid}", handler.CancelOrder).Methods("PUT")
if err := http.ListenAndServe(":8000", router); err != nil { if err := http.ListenAndServe(":8000", router); err != nil {
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
type Match struct { type Match struct {
ID primitive.ObjectID `bson:"_id, omitempty"` ID primitive.ObjectID `bson:"_id, omitempty"`
ExternalID int64 `bson:"externalID"` ExternalID int64 `bson:"externalID"`
Price int32 `bson:"price, omitempty"`
InitialTicketAmount int32 `bson:"initial_ticket_amount"` InitialTicketAmount int32 `bson:"initial_ticket_amount"`
AvailableTicketAmount int32 `bson:"available_ticket_amount"` AvailableTicketAmount int32 `bson:"available_ticket_amount"`
Opponenent string `bson:"opponent"` Opponenent string `bson:"opponent"`
...@@ -19,5 +20,3 @@ type Match struct { ...@@ -19,5 +20,3 @@ type Match struct {
OrderAmount int32 `bson:"orderamount"` OrderAmount int32 `bson:"orderamount"`
Orders []Order `bson:"orders"` Orders []Order `bson:"orders"`
} }
// muss nach jedem update , erstellen gemacht werden , funktion anpassen
...@@ -29,19 +29,61 @@ func CreateMatch(match *model.Match) error { ...@@ -29,19 +29,61 @@ func CreateMatch(match *model.Match) error {
func UpdateMatch(matchID primitive.ObjectID, match *model.Match) (*model.Match, error) { func UpdateMatch(matchID primitive.ObjectID, match *model.Match) (*model.Match, error) {
result := model.Match{} result := model.Match{}
existingMatch, err := GetMatchByID(matchID)
if existingMatch == nil || err != nil {
return existingMatch, err
}
filter := bson.D{primitive.E{Key: "_id", Value: matchID}} filter := bson.D{primitive.E{Key: "_id", Value: matchID}}
updater := bson.D{primitive.E{Key: "$set", Value: bson.D{ updater := bson.D{primitive.E{Key: "$set", Value: bson.D{
primitive.E{Key: "initial_ticket_amount", Value: match.InitialTicketAmount}, primitive.E{Key: "initial_ticket_amount", Value: match.InitialTicketAmount},
primitive.E{Key: "external_id", Value: match.ExternalID},
primitive.E{Key: "price", Value: match.Price},
primitive.E{Key: "opponent", Value: match.Opponenent},
primitive.E{Key: "league_name", Value: match.LeagueName},
primitive.E{Key: "available_ticket_amount", Value: match.AvailableTicketAmount}, primitive.E{Key: "available_ticket_amount", Value: match.AvailableTicketAmount},
primitive.E{Key: "away_match", Value: match.AwayMatch}, primitive.E{Key: "away_match", Value: match.AwayMatch},
primitive.E{Key: "location", Value: match.Location}, primitive.E{Key: "location", Value: match.Location},
//primitive.E{Key: "date", Value: match.Date}, primitive.E{Key: "date", Value: match.Date},
}}}
client, err := db.GetMongoClient()
if err != nil {
return nil, err
}
collection := client.Database(db.DB).Collection(db.MATCHES)
updateResult, err := collection.UpdateOne(context.TODO(), filter, updater)
if err != nil {
return nil, err
}
if updateResult.ModifiedCount == 0 {
return nil, fmt.Errorf("no document was updated")
}
err = collection.FindOne(context.TODO(), filter).Decode(&result)
if err != nil {
return nil, err
}
return &result, nil
}
func UpdateTickets(matchID primitive.ObjectID, match *model.Match) (*model.Match, error) {
result := model.Match{}
filter := bson.D{primitive.E{Key: "_id", Value: matchID}}
existingmatch, err := GetMatchByID(matchID)
if err != nil {
fmt.Println(existingmatch, "existingmatch")
return &result, err
}
match.AvailableTicketAmount = existingmatch.AvailableTicketAmount + match.InitialTicketAmount
match.InitialTicketAmount = existingmatch.InitialTicketAmount + match.InitialTicketAmount
updater := bson.D{primitive.E{Key: "$set", Value: bson.D{
primitive.E{Key: "initial_ticket_amount", Value: match.InitialTicketAmount},
primitive.E{Key: "price", Value: match.Price},
primitive.E{Key: "available_ticket_amount", Value: match.AvailableTicketAmount},
}}} }}}
client, err := db.GetMongoClient() client, err := db.GetMongoClient()
......
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