Skip to content

Commit

Permalink
fixes conflicts in team and branch,implements updatePlayer prototype-…
Browse files Browse the repository at this point in the history
…bugs remain unfixed [TBD]
  • Loading branch information
zakhaev26 committed Nov 30, 2023
1 parent c06eb3b commit 5e5da50
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 55 deletions.
41 changes: 16 additions & 25 deletions src/api/basketball/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,24 @@ func UploadTeamPlayer(w http.ResponseWriter, r *http.Request) {

json.NewEncoder(w).Encode(res)
}
type RequestBody struct {
Name string `json:"name"`
CollegeID string `json:"college_id"`
}

func UpdateTeamPlayer(w http.ResponseWriter, r *http.Request) {
fmt.Println("Update Request Incoming.")
var updatePlayer playerModel.Player
_ = json.NewDecoder(r.Body).Decode(&updatePlayer)

res := map[string]interface{}{}

for key, val := range map[string]interface{}{
"Name": updatePlayer.Name,
"ID": updatePlayer.ID,
"ImageLink": updatePlayer.ImageLink,
"Position": updatePlayer.Position,
"Branch": updatePlayer.Branch,
"Year": updatePlayer.Year,
"Age": updatePlayer.Age,
"Instagram": updatePlayer.Instagram,
"Minutes": updatePlayer.Minutes,
"Rebounds": updatePlayer.Rebounds,
"Assists": updatePlayer.Assists,
"Points": updatePlayer.Points,
} {
if val == 0 || val == "" {
continue
}
res[key] = val
var requestBody RequestBody
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
http.Error(w, "Error decoding request body", http.StatusBadRequest)
return
}

json.NewEncoder(w).Encode(updatePlayer)
fmt.Println("In Controller,name::", requestBody.Name)
player,err := helper.GetPlayerByName(requestBody.Name)
if err != nil {
http.Error(w, "Error decoding request body", http.StatusBadRequest)
return
}
json.NewEncoder(w).Encode(player)
}
2 changes: 1 addition & 1 deletion src/api/basketball/database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var Collection *mongo.Collection
// Initiator function which runs automatically at the start of application
// This function connects to the DB and returns collection instances which is used for DB actions.
func init() {
const connectionString = "mongodb+srv://linux-skg:1TuX01zH2y3tjUFV@sports.vj9j4tb.mongodb.net/?retryWrites=true&w=majority"
const connectionString = ""
const databaseName = "basketball"
const collection1_Name = "in-match-scoring"

Expand Down
63 changes: 35 additions & 28 deletions src/api/basketball/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ import (
@Params:
team string --> extracted from URL
*/
func TeamWisePlayers(team string) []primitive.M {
func TeamWisePlayers(branch string) []primitive.M {

//creating a cursor instance for query's matching documents
cur, err := basketballdb.Collection.Find(context.Background(), bson.D{{Key: "team", Value: team}})
cur, err := basketballdb.Collection.Find(context.Background(), bson.D{{Key: "branch", Value: branch}})

//handling error
if err != nil {
log.Fatal("Error:", err)
}

// data structure to be sent as response
var Players []primitive.M

Expand Down Expand Up @@ -68,34 +67,42 @@ func UploadTeamPlayer(player playerModel.Player) *mongo.InsertOneResult {
return inserted
}

func UpdateTeamPlayer(updatePlayer playerModel.Player) {

res := map[string]interface{}{}

for key, val := range map[string]interface{}{
"Name": updatePlayer.Name,
"ID": updatePlayer.ID,
"ImageLink": updatePlayer.ImageLink,
"Position": updatePlayer.Position,
"Branch": updatePlayer.Branch,
"Year": updatePlayer.Year,
"Age": updatePlayer.Age,
"Instagram": updatePlayer.Instagram,
"Minutes": updatePlayer.Minutes,
"Rebounds": updatePlayer.Rebounds,
"Assists": updatePlayer.Assists,
"Points": updatePlayer.Points,
} {
if val == 0 || val == "" {
continue
func GetPlayerByName(name string) (playerModel.Player, error) {
var player playerModel.Player

// Construct the filter
filter := bson.D{{Key: "name", Value: name}}
fmt.Println("Filter:", filter)

// Find the player in the database
result := basketballdb.Collection.FindOne(context.TODO(), filter)
if result.Err() != nil {
if result.Err() == mongo.ErrNoDocuments {
fmt.Println("Player not found")
return player, fmt.Errorf("Player not found")
}
fmt.Println("Key , Value:", key, val)
res[key] = val
log.Println("Error finding player:", result.Err())
return player, result.Err()
}

fmt.Println("Updated Player -->", res)
// Decode the player data
err := result.Decode(&player)
if err != nil {
log.Println("Error decoding player:", err)
return player, err
}

// updateDoc := bson.M{"$set": bson.M{"team": newTeam, "title": newTitle}}
// Log the retrieved player
fmt.Println("Retrieved Player:", player)
return player, nil
}

// updated, err := basketballdb.Collection.UpdateOne(context.TODO(), bson.D{})
func UpdateTeamPlayer(name string, college_id string) playerModel.Player {
updatePlayer, err := GetPlayerByName(name)
if err != nil {
log.Fatal(err)
}
fmt.Println("Player to be Updated :", updatePlayer)
fmt.Println("Returning...")
return updatePlayer
}
1 change: 1 addition & 0 deletions src/api/basketball/model/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type Player struct {
Rebounds int `json:"reb,omitempty"`
Assists int `json:"ast,omitempty"`
Points int `json:"pts,omitempty"`
CollegeId string `json:"college_id,omitempty"`
}
2 changes: 1 addition & 1 deletion src/api/basketball/routes/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func Router() *mux.Router {

r.HandleFunc("/get-teamwise-players", controller.GetTeamWisePlayers).Methods("GET")
r.HandleFunc("/upload-player", controller.UploadTeamPlayer).Methods("POST")
r.HandleFunc("/update-player", controller.UpdateTeamPlayer).Methods("PUT")
r.HandleFunc("/update-player", controller.UpdateTeamPlayer).Methods("POST")
return r
}

0 comments on commit 5e5da50

Please sign in to comment.