From ef7ef16e90924085d25070a7c795169bb72ba01f Mon Sep 17 00:00:00 2001 From: anirudh Date: Wed, 18 Oct 2023 01:34:01 +0530 Subject: [PATCH] feat(team): :card_file_box: get all unprotected teams --- controllers/team.controller.go | 12 ++++++++++++ repository/team.repository.go | 9 +++++++++ routers/index.go | 3 +++ 3 files changed, 24 insertions(+) diff --git a/controllers/team.controller.go b/controllers/team.controller.go index 59e6089..069104f 100644 --- a/controllers/team.controller.go +++ b/controllers/team.controller.go @@ -29,6 +29,18 @@ func NewTeamController() *TeamController { // --- Can be done by any logged in user --- +// GetUnprotectedTeams retrieves all unprotected teams. +func (tc *TeamController) GetUnprotectedTeams(c *gin.Context) { + // Retrieve the unprotected teams + teams, err := tc.teamRepo.GetUnprotectedTeams() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve teams"}) + return + } + + c.JSON(http.StatusOK, teams) +} + // CreateTeam creates a new team in the database and designates creator as super admin. func (tc *TeamController) CreateTeam(c *gin.Context) { // Extract data from the JSON request diff --git a/repository/team.repository.go b/repository/team.repository.go index 8ab6f38..e1b0753 100644 --- a/repository/team.repository.go +++ b/repository/team.repository.go @@ -52,3 +52,12 @@ func (tr *TeamRepository) UpdateTeam(team models.Team) (models.Team, error) { func (tr *TeamRepository) DeleteTeamByID(id uint) error { return tr.db.Unscoped().Delete(&models.Team{}, id).Error } + +// GetUnprotectedTeams retrieves all unprotected teams. +func (tr *TeamRepository) GetUnprotectedTeams() ([]models.Team, error) { + var teams []models.Team + if err := tr.db.Find(&teams, "protected = ?", false).Error; err != nil { + return nil, err + } + return teams, nil +} diff --git a/routers/index.go b/routers/index.go index 3d01451..2dc1884 100644 --- a/routers/index.go +++ b/routers/index.go @@ -79,6 +79,9 @@ func RegisterRoutes(route *gin.Engine) { { teamController := controllers.NewTeamController() + // Get all unprotected teams + team.GET("/", middleware.BaseAuthMiddleware(), teamController.GetUnprotectedTeams) + // Create a team team.POST("/", middleware.BaseAuthMiddleware(), teamController.CreateTeam)