Skip to content

Commit

Permalink
Merge pull request #24 from fy23-gw-gackathon/feature/#23_add_user_apis
Browse files Browse the repository at this point in the history
user周りのAPI追加
  • Loading branch information
gari8 committed May 8, 2023
2 parents fc55bf5 + a8dc0fb commit 084c078
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 30 deletions.
2 changes: 2 additions & 0 deletions controller/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type UserUseCase interface {
GetUserFromToken(ctx context.Context, token string) (*entity.User, error)
GetUsers(ctx context.Context, organizationID string) ([]*entity.User, error)
InviteUser(ctx context.Context, email, organizationID string) (*entity.User, error)
UpdateUserRole(ctx context.Context, organizationID, userID string, role bool) error
DeleteUser(ctx context.Context, organizationID, userID string) error
}

type OrganizationUseCase interface {
Expand Down
67 changes: 65 additions & 2 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type UsersResponse struct {
// @Tags User
// @Accept json
// @Produce json
// @Param organizationCode path string true "組織コード"
// @Param organizationCode path string true "組織コード"
// @Success 200 {object} UsersResponse "OK"
// @Failure 401 {object} entity.ErrorResponse "Unauthorized"
// @Failure 403 {object} entity.ErrorResponse "Forbidden"
Expand All @@ -52,7 +52,7 @@ type InviteUserRequest struct {
// @Tags User
// @Accept json
// @Produce json
// @Param organizationCode path string true "組織コード"
// @Param organizationCode path string true "組織コード"
// @Param request body InviteUserRequest true "メンバー招待リクエスト"
// @Success 200 {object} entity.User "OK"
// @Failure 400 {object} entity.ErrorResponse "BadRequest"
Expand Down Expand Up @@ -91,3 +91,66 @@ func (c UserController) GetMe(ctx *gin.Context) (interface{}, error) {
token := strings.Replace(bearerKey, "Bearer ", "", 1)
return c.UserUseCase.GetUserFromToken(ctx, token)
}

// UpdateUserRoleRequest - ユーザーロール更新リクエスト
type UpdateUserRoleRequest struct {
// ロール
Role bool `json:"role"`
}

// UpdateUserRole godoc
// @Summary ユーザーロール更新API
// @Tags User
// @Accept json
// @Produce json
// @Param organizationCode path string true "組織コード"
// @Param userId path string true "ユーザーID"
// @Param request body UpdateUserRoleRequest true "ユーザーロール更新リクエスト"
// @Success 200 "OK"
// @Failure 400 {object} entity.ErrorResponse "BadRequest"
// @Failure 401 {object} entity.ErrorResponse "Unauthorized"
// @Failure 403 {object} entity.ErrorResponse "Forbidden"
// @Failure 404 {object} entity.ErrorResponse "Not Found"
// @Failure 409 {object} entity.ErrorResponse "Conflict"
// @Router /organizations/{organizationCode}/users/{userId} [put]
// @Security Bearer
func (c UserController) UpdateUserRole(ctx *gin.Context) (interface{}, error) {
var req UpdateUserRoleRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
return nil, entity.NewError(http.StatusBadRequest, err)
}
user, _ := ctx.Get(entity.ContextKeyUser)
oUser := user.(*entity.OrganizationUser)

if !oUser.IsAdmin {
return nil, entity.NewError(http.StatusForbidden, errors.New("you are not admin"))
}

userID := ctx.Param("userId")
return nil, c.UserUseCase.UpdateUserRole(ctx, oUser.OrganizationID, userID, req.Role)
}

// DeleteUser godoc
// @Summary ユーザー削除API
// @Tags User
// @Accept json
// @Produce json
// @Param organizationCode path string true "組織コード"
// @Param userId path string true "ユーザーID"
// @Success 200 "OK"
// @Failure 401 {object} entity.ErrorResponse "Unauthorized"
// @Failure 403 {object} entity.ErrorResponse "Forbidden"
// @Failure 404 {object} entity.ErrorResponse "Not Found"
// @Router /organizations/{organizationCode}/users/{userId} [delete]
// @Security Bearer
func (c UserController) DeleteUser(ctx *gin.Context) (interface{}, error) {
user, _ := ctx.Get(entity.ContextKeyUser)
oUser := user.(*entity.OrganizationUser)

if !oUser.IsAdmin {
return nil, entity.NewError(http.StatusForbidden, errors.New("you are not admin"))
}

userID := ctx.Param("userId")
return nil, c.UserUseCase.DeleteUser(ctx, oUser.OrganizationID, userID)
}
156 changes: 150 additions & 6 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,141 @@ const docTemplate = `{
}
}
},
"/organizations/{organizationCode}/users/{userId}": {
"put": {
"security": [
{
"Bearer": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "ユーザーロール更新API",
"parameters": [
{
"type": "string",
"description": "組織コード",
"name": "organizationCode",
"in": "path",
"required": true
},
{
"type": "string",
"description": "ユーザーID",
"name": "userId",
"in": "path",
"required": true
},
{
"description": "ユーザーロール更新リクエスト",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/controller.UpdateUserRoleRequest"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "BadRequest",
"schema": {
"$ref": "#/definitions/entity.ErrorResponse"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/entity.ErrorResponse"
}
},
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/entity.ErrorResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/entity.ErrorResponse"
}
},
"409": {
"description": "Conflict",
"schema": {
"$ref": "#/definitions/entity.ErrorResponse"
}
}
}
},
"delete": {
"security": [
{
"Bearer": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "ユーザー削除API",
"parameters": [
{
"type": "string",
"description": "組織コード",
"name": "organizationCode",
"in": "path",
"required": true
},
{
"type": "string",
"description": "ユーザーID",
"name": "userId",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/entity.ErrorResponse"
}
},
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/entity.ErrorResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/entity.ErrorResponse"
}
}
}
}
},
"/reports/{reportId}": {
"put": {
"consumes": [
Expand Down Expand Up @@ -692,6 +827,15 @@ const docTemplate = `{
}
}
},
"controller.UpdateUserRoleRequest": {
"type": "object",
"properties": {
"role": {
"description": "ロール",
"type": "boolean"
}
}
},
"controller.UsersResponse": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -824,12 +968,12 @@ const docTemplate = `{

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "localhost:8080",
BasePath: "/",
Schemes: []string{"http"},
Title: "Reportify",
Description: "Reportify",
Version: "",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "",
Description: "",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
}
Expand Down
Loading

0 comments on commit 084c078

Please sign in to comment.