* TP-47335| created teamservice version 2 for get teams api * TP-47335| modified the getusers info function to handle nil error * refactored the structure of team service and created interfaces * TP-47335| created unit tests * TP-47335| added unit tests for get teams api * resolved PR comments * created custom error types * made some changes in unit tests * added unit tests for team handler * solved merge conflicts * solved invalid users bug * resolved merge conflicts * restricting incident title length to 100 characters * removed unecessary comments
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"houston/common/util"
|
|
"houston/logger"
|
|
"houston/model/customErrors"
|
|
common "houston/service/response/common"
|
|
"houston/service/teamService"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type TeamHandler struct {
|
|
gin *gin.Engine
|
|
service teamService.ITeamServiceV2
|
|
}
|
|
|
|
func NewTeamHandler(gin *gin.Engine, service teamService.ITeamServiceV2) *TeamHandler {
|
|
return &TeamHandler{gin: gin, service: service}
|
|
}
|
|
|
|
func (handler *TeamHandler) HandleGetTeamDetails(c *gin.Context) {
|
|
id := c.Param(util.IdParam)
|
|
|
|
teamId, err := strconv.Atoi(id)
|
|
if err != nil || teamId <= 0 {
|
|
logger.Error(fmt.Sprintf("invalid team id: %d", teamId))
|
|
c.JSON(http.StatusBadRequest, common.ErrorResponse(errors.New(fmt.Sprintf("invalid team id %d", teamId)), http.StatusBadRequest, nil))
|
|
return
|
|
}
|
|
teamResponse, err := handler.service.GetTeamDetails(uint(teamId))
|
|
if err != nil {
|
|
handler.handleErrorResponse(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, common.SuccessResponse(teamResponse, http.StatusOK))
|
|
}
|
|
|
|
func (handler *TeamHandler) HandleGetAllTeams(c *gin.Context) {
|
|
teamResponses, err := handler.service.GetAllTeams()
|
|
|
|
if err != nil {
|
|
handler.handleErrorResponse(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, common.SuccessResponse(teamResponses, http.StatusOK))
|
|
}
|
|
|
|
func (handler *TeamHandler) handleErrorResponse(c *gin.Context, err error) {
|
|
var dataAccessError *customErrors.DataAccessError
|
|
if errors.As(err, &dataAccessError) {
|
|
c.JSON(http.StatusInternalServerError, common.ErrorResponse(err, http.StatusInternalServerError, nil))
|
|
return
|
|
}
|
|
var notFoundError *customErrors.NotFoundError
|
|
if errors.As(err, ¬FoundError) {
|
|
c.JSON(http.StatusNotFound, common.ErrorResponse(err, http.StatusNotFound, nil))
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, common.ErrorResponse(err, http.StatusInternalServerError, nil))
|
|
}
|