Skip to content

Commit

Permalink
feature. refactoring repository interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Apr 11, 2023
1 parent 1b84880 commit 6de8240
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 391 deletions.
8 changes: 0 additions & 8 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1606,14 +1606,6 @@ const docTemplate = `{
"Stacks"
],
"summary": "Get Stacks",
"parameters": [
{
"type": "string",
"description": "show all organizations",
"name": "all",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
Expand Down
8 changes: 0 additions & 8 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1599,14 +1599,6 @@
"Stacks"
],
"summary": "Get Stacks",
"parameters": [
{
"type": "string",
"description": "show all organizations",
"name": "all",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
Expand Down
5 changes: 0 additions & 5 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2096,11 +2096,6 @@ paths:
consumes:
- application/json
description: Get Stacks
parameters:
- description: show all organizations
in: query
name: all
type: string
produces:
- application/json
responses:
Expand Down
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

func init() {
flag.String("external-address", "http://tks-api.tks.svc:9110", "service address")
flag.Int("port", 8080, "service port")
flag.String("web-root", "../../web", "path of root path for web")
flag.String("argo-address", "http://localhost", "service address for argoworkflow")
Expand Down
114 changes: 6 additions & 108 deletions internal/delivery/http/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"fmt"
"net/http"

"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/openinfradev/tks-api/internal/auth/request"
"github.com/openinfradev/tks-api/internal/usecase"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/log"
"github.com/pkg/errors"
)

type StackHandler struct {
Expand Down Expand Up @@ -47,16 +45,13 @@ func (h *StackHandler) CreateStack(w http.ResponseWriter, r *http.Request) {
log.Info(err)
}

stackId, err := h.usecase.Create(r.Context(), dto)
err = h.usecase.Create(r.Context(), dto)
if err != nil {
ErrorJSON(w, err)
return
}

var out domain.CreateStackResponse
out.ID = stackId.String()

ResponseJSON(w, http.StatusOK, out)
ResponseJSON(w, http.StatusOK, nil)
}

// GetStack godoc
Expand All @@ -65,7 +60,6 @@ func (h *StackHandler) CreateStack(w http.ResponseWriter, r *http.Request) {
// @Description Get Stacks
// @Accept json
// @Produce json
// @Param all query string false "show all organizations"
// @Success 200 {object} domain.GetStacksResponse
// @Router /stacks [get]
// @Security JWT
Expand Down Expand Up @@ -121,13 +115,7 @@ func (h *StackHandler) GetStack(w http.ResponseWriter, r *http.Request) {
return
}

stackId, err := uuid.Parse(strId)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(errors.Wrap(err, "Failed to parse uuid %s")))
return
}

stack, err := h.usecase.Get(stackId)
stack, err := h.usecase.Get(domain.StackId(strId))
if err != nil {
ErrorJSON(w, err)
return
Expand All @@ -152,39 +140,7 @@ func (h *StackHandler) GetStack(w http.ResponseWriter, r *http.Request) {
// @Router /stacks/{stackId} [put]
// @Security JWT
func (h *StackHandler) UpdateStack(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strId, ok := vars["stackId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid stackId")))
return
}

cloudSeetingId, err := uuid.Parse(strId)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(errors.Wrap(err, "Failed to parse uuid %s")))
return
}

input := domain.UpdateStackRequest{}
err = UnmarshalRequestInput(r, &input)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

var dto domain.Stack
if err = domain.Map(input, &dto); err != nil {
log.Info(err)
}
dto.ID = cloudSeetingId

err = h.usecase.Update(r.Context(), dto)
if err != nil {
ErrorJSON(w, err)
return
}

ResponseJSON(w, http.StatusOK, nil)
ErrorJSON(w, httpErrors.NewInternalServerError(fmt.Errorf("Need implementaion")))
}

// DeleteStack godoc
Expand All @@ -199,39 +155,7 @@ func (h *StackHandler) UpdateStack(w http.ResponseWriter, r *http.Request) {
// @Router /stacks/{stackId} [delete]
// @Security JWT
func (h *StackHandler) DeleteStack(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
stackId, ok := vars["stackId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid stackId")))
return
}

parsedId, err := uuid.Parse(stackId)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(errors.Wrap(err, "Failed to parse uuid")))
return
}

input := domain.DeleteStackRequest{}
err = UnmarshalRequestInput(r, &input)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

var dto domain.Stack
if err = domain.Map(input, &dto); err != nil {
log.Info(err)
}
dto.ID = parsedId

err = h.usecase.Delete(r.Context(), dto)
if err != nil {
ErrorJSON(w, err)
return
}

ResponseJSON(w, http.StatusOK, nil)
ErrorJSON(w, httpErrors.NewInternalServerError(fmt.Errorf("Need implementaion")))
}

// CheckStackName godoc
Expand All @@ -245,32 +169,6 @@ func (h *StackHandler) DeleteStack(w http.ResponseWriter, r *http.Request) {
// @Router /stacks/name/{name}/existence [GET]
// @Security JWT
func (h *StackHandler) CheckStackName(w http.ResponseWriter, r *http.Request) {
user, ok := request.UserFrom(r.Context())
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid token")))
return
}
ErrorJSON(w, httpErrors.NewInternalServerError(fmt.Errorf("Need implementaion")))

vars := mux.Vars(r)
name, ok := vars["name"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid name")))
return
}

exist := true
_, err := h.usecase.GetByName(user.GetOrganizationId(), name)
if err != nil {
if _, code := httpErrors.ErrorResponse(err); code == http.StatusNotFound {
exist = false
} else {
ErrorJSON(w, err)
return
}
}

var out domain.CheckStackNameResponse
out.Existed = exist

ResponseJSON(w, http.StatusOK, out)
}
13 changes: 0 additions & 13 deletions internal/repository/cloud-setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,3 @@ func reflectCloudSetting(cloudSetting CloudSetting) domain.CloudSetting {
UpdatedAt: cloudSetting.UpdatedAt,
}
}

func reflectUser(user User) domain.User {
return domain.User{
ID: user.ID.String(),
AccountId: user.AccountId,
Name: user.Name,
Email: user.Email,
Department: user.Department,
Description: user.Description,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
}
}
11 changes: 11 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ package repository
import "gorm.io/gorm"

type FilterFunc func(user *gorm.DB) *gorm.DB

type Repository struct {
User IUserRepository
Cluster IClusterRepository
Organization IOrganizationRepository
AppGroup IAppGroupRepository
AppServeApp IAppServeAppRepository
CloudSetting ICloudSettingRepository
StackTemplate IStackTemplateRepository
History IHistoryRepository
}
13 changes: 13 additions & 0 deletions internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,16 @@ func (r *UserRepository) reflectRole(role Role) domain.Role {
UpdatedAt: role.UpdatedAt,
}
}

func reflectUser(user User) domain.User {
return domain.User{
ID: user.ID.String(),
AccountId: user.AccountId,
Name: user.Name,
Email: user.Email,
Department: user.Department,
Description: user.Description,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
}
}
Loading

0 comments on commit 6de8240

Please sign in to comment.