diff --git a/docs/swagger.yaml b/docs/swagger.yaml index d2b7a99..13bf4db 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -9,7 +9,7 @@ definitions: handler.PingServerResponse: properties: data: - $ref: '#/definitions/schemas.PingResponse' + $ref: "#/definitions/schemas.PingResponse" message: type: string type: object @@ -65,50 +65,111 @@ definitions: type: string type: object type: object + schema.TeamNamePayload: + properties: + teamname: + type: string + schema.TeamMembersResponse: + properties: + data: + type: array + items: + properties: + id: + type: integer + githubId: + type: integer + url: + type: string + login: + type: string + role: + type: string + team: + type: string + createdAt: + type: string + updatedAt: + type: string + deletedAt: + type: string + message: + type: string + type: object info: contact: {} paths: /ping: get: consumes: - - application/json + - application/json description: Ping the server to check if it is running produces: - - application/json + - application/json responses: "200": description: OK schema: - $ref: '#/definitions/handler.PingServerResponse' + $ref: "#/definitions/handler.PingServerResponse" summary: Ping the server tags: - - Ping + - Ping /webhook: post: consumes: - - application/json + - application/json description: Handle with GitHub Webhook Payload parameters: - - description: Github webhook payload - in: body - name: request - required: true - schema: - $ref: '#/definitions/service.GithubWebhookUserManagePayload' + - description: Github webhook payload + in: body + name: request + required: true + schema: + $ref: "#/definitions/service.GithubWebhookUserManagePayload" produces: - - application/json + - application/json responses: "200": description: OK "400": description: Bad Request schema: - $ref: '#/definitions/handler.ErrorResponse' + $ref: "#/definitions/handler.ErrorResponse" "500": description: Server Error schema: - $ref: '#/definitions/handler.ErrorResponse' + $ref: "#/definitions/handler.ErrorResponse" summary: Github Webhook Membership Receiver tags: - - Webhook + - Webhook + /teams/:teamname/members: + get: + consumes: + - application/json + description: Returns list of members from a team + parameters: + - description: Team name + in: body + name: teamname + required: true + schema: + $ref: "#/definitions/schema.TeamNamePayload" + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: "#/definitions/schema.TeamMembersResponse" + "400": + description: Bad Request + schema: + $ref: "#/definitions/handler.ErrorResponse" + "500": + description: Server Error + schema: + $ref: "#/definitions/handler.ErrorResponse" + summary: Team members list + tags: + - Team swagger: "2.0" diff --git a/handler/getTeamMembers.go b/handler/getTeamMembers.go new file mode 100644 index 0000000..39e167e --- /dev/null +++ b/handler/getTeamMembers.go @@ -0,0 +1,32 @@ +package handler + +import ( + "github.com/forjadev/gun-organization/schemas" + "github.com/gin-gonic/gin" +) + +// @Summary Returns the list of team members +// @Description Returns the list of members of specific team +// @Tags Teams +// @Accept json +// @Produce json +// @Success 200 {object} ListTeamMembersResponse +// @Router /team/{teamname}/members [get] +func ListTeamMembersHandler(ctx *gin.Context) { + teamName := ctx.Param("teamname") + + if teamName == "" { + sendError(ctx, 404, "Team name is required") + return + } + + members := []schemas.Member{} + + err := db.Joins("JOIN teams ON teams.team_id = member.team_id").Where("teams.name = ?", teamName).Find(&schemas.Member{}) + + if err != nil { + sendError(ctx, 404, "Team not found") + } + + sendSuccess(ctx, "ListTeamMembers", members) +} diff --git a/handler/handler.go b/handler/handler.go index 98ad295..30145b6 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -3,10 +3,18 @@ package handler import ( "net/http" + "github.com/forjadev/gun-organization/config" "github.com/forjadev/gun-organization/schemas" "github.com/gin-gonic/gin" + "gorm.io/gorm" ) +var db *gorm.DB + +func InitiliazeHandler() { + db = config.GetDatabase() +} + // @BasePath /api/v1 // @Summary Ping the server diff --git a/router/routes.go b/router/routes.go index f40097d..25238a3 100644 --- a/router/routes.go +++ b/router/routes.go @@ -10,6 +10,9 @@ import ( // initializeRoutes sets up the routes for the application func initializeRoutes(r *gin.Engine) { + // Initialize handler + handler.InitiliazeHandler() + // Set the base path for all routes basePath := "/api/v1" @@ -24,6 +27,7 @@ func initializeRoutes(r *gin.Engine) { // Define a GET route for the /ping endpoint v1.GET("/ping", handler.PingServerHandler) v1.POST("/webhook", handler.GitHubWebhookHandler) + v1.GET("/teams/:teamname/members", handler.ListTeamMembersHandler) } // Initialize Swagger documentation diff --git a/schemas/team.go b/schemas/team.go index d946765..20f82bf 100644 --- a/schemas/team.go +++ b/schemas/team.go @@ -11,6 +11,10 @@ type Team struct { Name string } +type TeamNameRequest struct { + name string +} + type TeamResponse struct { ID uint `json:"id"` CreatedAt time.Time `json:"createdAt"`