Skip to content
12 changes: 8 additions & 4 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package controller

import (
"go-api-template/model/commonerrors"
"go-api-template/model/swagger"
"go-api-template/service"

"github.com/gin-gonic/gin"
"github.com/go-openapi/strfmt"
)

// To avoid unused dependency
var _ = swagger.BadRequestResponse{}

type IUser interface {
UserByID(ctx *gin.Context)
}
Expand All @@ -30,10 +34,10 @@ func NewUser(service service.IUser) IUser {
// @Accept json
// @Produce json
// @Param user_id path string true "User ID" format(uuid)
// @Success 200 {object} model.UserByIDResponseSwagger "Get user by ID"
// @Failure 400 {object} model.BadRequestResponse
// @Failure 403 {object} model.ForbiddenResponse
// @Failure 500 {object} model.InternalErrorResponse
// @Success 200 {object} swagger.UserByIDResponseSwagger "Get user by ID"
// @Failure 400 {object} swagger.BadRequestResponse
// @Failure 403 {object} swagger.ForbiddenResponse
// @Failure 500 {object} swagger.InternalErrorResponse
// @Router /users/{user_id} [get]
func (controller *user) UserByID(ctx *gin.Context) {
// Validate path params
Expand Down
5 changes: 3 additions & 2 deletions controller/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"go-api-template/model"
"go-api-template/model/commonerrors"
"go-api-template/model/swagger"
"go-api-template/pkg/random"
"go-api-template/service"
"net/http"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (suite *UserTestSuite) Test_UserByID_ReturnsBadRequest_InCaseOfUserIDIsNotV

// Assert
suite.Equal(http.StatusBadRequest, suite.responseRecorder.Code)
var actualResponse model.BadRequestResponse
var actualResponse swagger.BadRequestResponse
err := json.NewDecoder(suite.responseRecorder.Body).Decode(&actualResponse)
suite.NoError(err)
suite.Equal(commonerrors.ErrInvalidUserID.Error(), actualResponse.Message)
Expand All @@ -79,7 +80,7 @@ func (suite *UserTestSuite) Test_UserByID_ReturnsUnprocessableEntity_InCaseOfUse

// Assert
suite.Equal(http.StatusUnprocessableEntity, suite.responseRecorder.Code)
var actualResponse model.UnprocessableEntityResponse
var actualResponse swagger.UnprocessableEntityResponse
err := json.NewDecoder(suite.responseRecorder.Body).Decode(&actualResponse)
suite.NoError(err)
suite.Equal(commonerrors.ErrUserNotFound.Error(), actualResponse.Message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type BadRequestResponse struct {
Message string `json:"message" example:"Bad request"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type CreatedResponse struct {
Message string `json:"message" example:"Resource created successfully"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type ForbiddenResponse struct {
Message string `json:"message" example:"You don't have permission to access this resource"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type InternalErrorResponse struct {
Message string `json:"message" example:"Internal server error"`
Expand Down
2 changes: 1 addition & 1 deletion model/ok_response.go → model/swagger/ok_response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type OKResponse struct {
Message string `json:"message" example:"Success"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type PartialSuccessResponse struct {
Message string `json:"message" example:"Some of the requested operations were successful"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type UnauthorizedResponse struct {
Message string `json:"message" example:"You are not authorized to access this resource"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type UnprocessableEntityResponse struct {
Message string `json:"message" example:"Posted data is not valid"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

type UserByIDResponseSwagger struct {
User *UserSwagger `json:"user"`
Expand Down
2 changes: 1 addition & 1 deletion model/user_swagger.go → model/swagger/user_swagger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package swagger

import "time"

Expand Down