-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expose list of a user's roles to them
- Loading branch information
1 parent
28c12a4
commit 6f73622
Showing
8 changed files
with
138 additions
and
11 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
backend/application/dashboard/profile/getRoles/response.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package getRoles | ||
|
||
import "github.com/khanzadimahdi/testproject/domain/role" | ||
|
||
type roleResponse struct { | ||
UUID string `json:"uuid"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
} | ||
|
||
type Response struct { | ||
Items []roleResponse `json:"items"` | ||
} | ||
|
||
func NewResponse(r []role.Role) *Response { | ||
items := make([]roleResponse, len(r)) | ||
|
||
for i := range r { | ||
items[i].UUID = r[i].UUID | ||
items[i].Name = r[i].Name | ||
items[i].Description = r[i].Description | ||
} | ||
|
||
return &Response{ | ||
Items: items, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package getRoles | ||
|
||
import ( | ||
"github.com/khanzadimahdi/testproject/domain/role" | ||
) | ||
|
||
const limit = 10 | ||
|
||
type UseCase struct { | ||
roleRepository role.Repository | ||
} | ||
|
||
func NewUseCase(roleRepository role.Repository) *UseCase { | ||
return &UseCase{ | ||
roleRepository: roleRepository, | ||
} | ||
} | ||
|
||
func (uc *UseCase) GetRoles(UUID string) (*Response, error) { | ||
roles, err := uc.roleRepository.GetByUserUUID(UUID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewResponse(roles), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package getRoles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
backend/presentation/http/api/dashboard/profile/getroles.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package profile | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/khanzadimahdi/testproject/application/auth" | ||
getroles "github.com/khanzadimahdi/testproject/application/dashboard/profile/getRoles" | ||
) | ||
|
||
type getRolesHandler struct { | ||
getRolesUseCase *getroles.UseCase | ||
} | ||
|
||
func NewGetRolesHandler(getRolesUseCase *getroles.UseCase) *getRolesHandler { | ||
return &getRolesHandler{ | ||
getRolesUseCase: getRolesUseCase, | ||
} | ||
} | ||
|
||
func (h *getRolesHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | ||
userUUID := auth.FromContext(r.Context()).UUID | ||
|
||
response, err := h.getRolesUseCase.GetRoles(userUUID) | ||
switch true { | ||
case err != nil: | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
default: | ||
rw.Header().Add("Content-Type", "application/json") | ||
rw.WriteHeader(http.StatusOK) | ||
json.NewEncoder(rw).Encode(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters