ADD: added team management
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
|
||||
"volleyball/internal/player"
|
||||
"volleyball/internal/team"
|
||||
|
||||
_ "github.com/lib/pq" // Import the PostgreSQL driver
|
||||
)
|
||||
@@ -54,6 +55,8 @@ func CheckIfTablesExist(db *sql.DB) (bool, error) {
|
||||
func InitTables(d *sql.DB) error {
|
||||
CreateOrUpdateTablePG(d, "users", player.User{})
|
||||
CreateOrUpdateTablePG(d, "roles", player.Roles{})
|
||||
createTable(d, "teams", team.Team{})
|
||||
createTable(d, "teams_player", team.TeamPlayerAssociation{})
|
||||
|
||||
tables := []string{
|
||||
// player.PlayerTable,
|
||||
|
||||
136
backend/internal/player/handler_test.go
Normal file
136
backend/internal/player/handler_test.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestGetPlayers(t *testing.T) {
|
||||
// Set Gin to Test Mode
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
t.Run("successful retrieval", func(t *testing.T) {
|
||||
// Create a new HTTP recorder and context
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
// Create a mock database connection
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Define the expected rows and columns
|
||||
rows := sqlmock.NewRows([]string{"uuid", "email", "username", "firstname", "lastname", "birthday", "is_active", "created_at", "role"}).
|
||||
AddRow("uuid-1", "player1@example.com", "player1", "first1", "last1", nil, true, nil, "{player}")
|
||||
|
||||
// Expect a query to be made
|
||||
mock.ExpectQuery(getPLayerWithRolesQuery).WillReturnRows(rows)
|
||||
|
||||
// Call the handler
|
||||
GetPlayers(c, db)
|
||||
|
||||
// Check the status code and response body
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status code %d but got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
// TODO: Add assertion for the response body
|
||||
|
||||
// Ensure all expectations were met
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("database error", func(t *testing.T) {
|
||||
// Create a new HTTP recorder and context
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
// Create a mock database connection
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Expect a query to fail
|
||||
mock.ExpectQuery(getPLayerWithRolesQuery).WillReturnError(sql.ErrConnDone)
|
||||
|
||||
// Call the handler
|
||||
GetPlayers(c, db)
|
||||
|
||||
// Check the status code and response body
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected status code %d but got %d", http.StatusInternalServerError, w.Code)
|
||||
}
|
||||
|
||||
// Ensure all expectations were met
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetPlayer(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
t.Run("successful retrieval", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{gin.Param{Key: "id", Value: "uuid-1"}}
|
||||
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
rows := sqlmock.NewRows([]string{"uuid", "username", "email"}).
|
||||
AddRow("uuid-1", "player1", "player1@example.com")
|
||||
|
||||
mock.ExpectQuery("SELECT id, name, email FROM users WHERE id = \\$1").WithArgs("uuid-1").WillReturnRows(rows)
|
||||
mock.ExpectQuery("SELECT role FROM roles WHERE player_id = \\$1").WithArgs("uuid-1").WillReturnRows(sqlmock.NewRows([]string{"role"}).AddRow("player"))
|
||||
|
||||
GetPlayer(c, db, "uuid-1")
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status code %d but got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("not found", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{gin.Param{Key: "id", Value: "uuid-not-found"}}
|
||||
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectQuery("SELECT id, name, email FROM users WHERE id = \\$1").WithArgs("uuid-not-found").WillReturnError(sql.ErrNoRows)
|
||||
|
||||
GetPlayer(c, db, "uuid-not-found")
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected status code %d but got %d", http.StatusInternalServerError, w.Code)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -16,16 +16,46 @@ func GetTeams(c *gin.Context, db *sql.DB) {
|
||||
teams, err := GetAllTeams(db)
|
||||
if err != nil {
|
||||
log.Printf("Error retrieving teams: %v", err)
|
||||
common.RespondError(c, http.StatusInternalServerError, "Failed to retrieve players")
|
||||
common.RespondError(c, http.StatusInternalServerError, "Failed to retrieve teams")
|
||||
return
|
||||
}
|
||||
|
||||
if len(teams) > 0 {
|
||||
log.Printf("User %s (%s) requested players", c.GetString("userId"), c.GetString("email"))
|
||||
c.JSON(http.StatusOK, teams)
|
||||
return
|
||||
}
|
||||
log.Printf("User %s (%s) requested players, but none found", c.GetString("userId"), c.GetString("email"))
|
||||
c.JSON(http.StatusOK, teams)
|
||||
// if len(teams) > 0 {
|
||||
// return
|
||||
// }
|
||||
|
||||
// common.RespondMessage(c, "No Players found")
|
||||
}
|
||||
|
||||
func CreateTeam(c *gin.Context, db *sql.DB) {
|
||||
var team Team
|
||||
if err := c.ShouldBindJSON(&team); err != nil {
|
||||
common.RespondError(c, http.StatusBadRequest, "Invalid request payload")
|
||||
return
|
||||
}
|
||||
|
||||
err := saveTeam(db, team)
|
||||
if err != nil {
|
||||
log.Printf("Error saving team: %v", err)
|
||||
common.RespondError(c, http.StatusBadRequest, "Error saving Team")
|
||||
}
|
||||
common.RespondSuccess(c, http.StatusOK)
|
||||
}
|
||||
|
||||
func UpdateTeam(c *gin.Context, db *sql.DB) {
|
||||
log.Println("team id: ", c.Param("uuid"))
|
||||
var team Team
|
||||
if err := c.ShouldBindJSON(&team); err != nil {
|
||||
common.RespondError(c, http.StatusBadRequest, "Error updating Team; Could not bind Params")
|
||||
return
|
||||
}
|
||||
team.UUID = c.Param("uuid")
|
||||
err := updateTeam(db, team)
|
||||
if err != nil {
|
||||
log.Printf("Error updating team: %v", err)
|
||||
common.RespondError(c, http.StatusBadRequest, "Error updating Team")
|
||||
}
|
||||
common.RespondSuccess(c, http.StatusOK)
|
||||
|
||||
common.RespondError(c, http.StatusNotFound, "No Players found")
|
||||
}
|
||||
|
||||
@@ -2,24 +2,63 @@ package team
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
UUID string `db:"uuid" sql:"VARCHAR(255)" index:"true"`
|
||||
Email string `db:"email" sql:"VARCHAR(255)" index:"true"`
|
||||
Username string `db:"username" sql:"VARCHAR(100)"`
|
||||
FirstName string `db:"firstname" sql:"VARCHAR(100)"`
|
||||
LastName string `db:"lastname" sql:"VARCHAR(100)"`
|
||||
type Team struct {
|
||||
UUID string `db:"uuid" sql:"VARCHAR(255)" index:"true"`
|
||||
Name string `db:"name" sql:"VARCHAR(100)"`
|
||||
OwnerUUID string `db:"owner_uuid" sql:"VARCHAR(255)" index:"true"`
|
||||
Description string `db:"describtion" sql:"VARCHAR(255)"`
|
||||
}
|
||||
|
||||
type Team struct {
|
||||
UUID string `db:"uuid" sql:"VARCHAR(255)" index:"true"`
|
||||
Name string `db:"username" sql:"VARCHAR(100)"`
|
||||
Email string `db:"email" sql:"VARCHAR(255)" index:"true"`
|
||||
PlayersUUID []Player `db:"players" sql:"JSVARCHAR(255)"`
|
||||
type TeamPlayerAssociation struct {
|
||||
TeamUUID string `db:"team_uuid" sql:"VARCHAR(255)" index:"true"`
|
||||
PlayerUUID string `db:"player_uuid" sql:"VARCHAR(255)" index:"true"`
|
||||
}
|
||||
|
||||
func GetAllTeams(db *sql.DB) ([]Team, error) {
|
||||
// Implementation to retrieve all teams from the database
|
||||
return []Team{}, nil
|
||||
var teams []Team
|
||||
rows, err := db.Query("SELECT * FROM teams")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var team Team
|
||||
if err := rows.Scan(&team.UUID, &team.Name, &team.OwnerUUID, &team.Description); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
teams = append(teams, team)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return teams, nil
|
||||
}
|
||||
|
||||
func saveTeam(db *sql.DB, team Team) error {
|
||||
|
||||
stmt := "INSERT INTO public.teams (name, owner_uuid,description) VALUES ($1, $2, $3)"
|
||||
log.Printf("Generated SQL statement: %s", stmt)
|
||||
_, err := db.Exec(stmt, team.Name, team.OwnerUUID, team.Description)
|
||||
if err != nil {
|
||||
log.Printf("Error saving team to database: %v", err)
|
||||
}
|
||||
log.Printf("Team %s saved to database", team.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateTeam(db *sql.DB, team Team) error {
|
||||
stmt := "UPDATE public.teams SET name = $1, description = $2 WHERE uuid = $3"
|
||||
_, err := db.Exec(stmt, team.Name, team.Description, team.UUID)
|
||||
if err != nil {
|
||||
log.Printf("Error updating team in database: %v", err)
|
||||
return err
|
||||
}
|
||||
log.Printf("Team %s updated successfully", team.Name)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user