Skip to content

Commit

Permalink
feat(team): 🗃️ get all unprotected teams
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhgray committed Oct 17, 2023
1 parent 32fe540 commit ef7ef16
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions controllers/team.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions repository/team.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions routers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ef7ef16

Please sign in to comment.