Skip to content

Commit

Permalink
Merge pull request #17 from openinfradev/refactoring_dto
Browse files Browse the repository at this point in the history
Refactoring dto
  • Loading branch information
ktkfree authored Apr 6, 2023
2 parents 95e53d3 + ef7b43b commit 398c2c3
Show file tree
Hide file tree
Showing 23 changed files with 3,127 additions and 608 deletions.
918 changes: 818 additions & 100 deletions api/swagger/docs.go

Large diffs are not rendered by default.

918 changes: 818 additions & 100 deletions api/swagger/swagger.json

Large diffs are not rendered by default.

559 changes: 518 additions & 41 deletions api/swagger/swagger.yaml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func migrateSchema(db *gorm.DB) error {
return err
}

// StackTemplate
if err := db.AutoMigrate(&repository.StackTemplate{}); err != nil {
return err
}

// Cluster
if err := db.AutoMigrate(&repository.Cluster{}); err != nil {
return err
Expand Down
123 changes: 71 additions & 52 deletions internal/delivery/http/app-group.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package http

import (
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/gorilla/mux"
"github.com/openinfradev/tks-api/internal/helper"
"github.com/openinfradev/tks-api/internal/usecase"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
Expand All @@ -30,37 +29,30 @@ func NewAppGroupHandler(h usecase.IAppGroupUsecase) *AppGroupHandler {
// @Accept json
// @Produce json
// @Param body body domain.CreateAppGroupRequest true "create appgroup request"
// @Success 200 {object} string
// @Success 200 {object} domain.CreateAppGroupResponse
// @Router /app-groups [post]
// @Security JWT
func (h *AppGroupHandler) CreateAppGroup(w http.ResponseWriter, r *http.Request) {
var input = domain.CreateAppGroupRequest{}
body, err := io.ReadAll(r.Body)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}
err = json.Unmarshal(body, &input)
input := domain.CreateAppGroupRequest{}
err := UnmarshalRequestInput(r, &input)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

if input.Type != "LMA" && input.Type != "SERVICE_MESH" && input.Type != "LMA_EFK" {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
var dto domain.AppGroup
if err = domain.Map(input, &dto); err != nil {
log.Info(err)
}

appGroupId, err := h.usecase.Create(input.ClusterId, input.Name, input.Type, "", input.Description)
appGroupId, err := h.usecase.Create(r.Context(), dto)
if err != nil {
ErrorJSON(w, err)
return
}

var out struct {
AppGroupId string `json:"appGroupId"`
}
out.AppGroupId = appGroupId
var out domain.CreateAppGroupResponse
out.ID = appGroupId.String()

ResponseJSON(w, http.StatusOK, out)
}
Expand All @@ -72,31 +64,34 @@ func (h *AppGroupHandler) CreateAppGroup(w http.ResponseWriter, r *http.Request)
// @Accept json
// @Produce json
// @Param clusterId query string false "clusterId"
// @Success 200 {object} []domain.AppGroup
// @Success 200 {object} domain.GetAppGroupsResponse
// @Router /app-groups [get]
// @Security JWT
func (h *AppGroupHandler) GetAppGroups(w http.ResponseWriter, r *http.Request) {
urlParams := r.URL.Query()

clusterId := urlParams.Get("clusterId")
if clusterId == "" {
if clusterId == "" || !helper.ValidateClusterId(clusterId) {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid clusterId")))
return
}

appGroups, err := h.usecase.Fetch(clusterId)
appGroups, err := h.usecase.Fetch(domain.ClusterId(clusterId))
if err != nil {
ErrorJSON(w, err)
return
}

var out struct {
AppGroups []domain.AppGroup `json:"appGroups"`
var out domain.GetAppGroupsResponse
out.AppGroups = make([]domain.AppGroupResponse, len(appGroups))
for i, appGroup := range appGroups {
if err := domain.Map(appGroup, &out.AppGroups[i]); err != nil {
log.Info(err)
continue
}
}
out.AppGroups = appGroups

ResponseJSON(w, http.StatusOK, out)

}

// GetAppGroup godoc
Expand All @@ -106,27 +101,31 @@ func (h *AppGroupHandler) GetAppGroups(w http.ResponseWriter, r *http.Request) {
// @Accept json
// @Produce json
// @Param appGroupId path string true "appGroupId"
// @Success 200 {object} []domain.AppGroup
// @Success 200 {object} domain.GetAppGroupResponse
// @Router /app-groups/{appGroupId} [get]
// @Security JWT
func (h *AppGroupHandler) GetAppGroup(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appGroupId, ok := vars["appGroupId"]
strId, ok := vars["appGroupId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appGroupId")))
return
}

appGroupId := domain.AppGroupId(strId)
if !appGroupId.Validate() {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appGroupId")))
return
}
appGroup, err := h.usecase.Get(appGroupId)
if err != nil {
ErrorJSON(w, err)
return
}

var out struct {
AppGroup domain.AppGroup `json:"appGroup"`
var out domain.GetAppGroupResponse
if err := domain.Map(appGroup, &out.AppGroup); err != nil {
log.Info(err)
}
out.AppGroup = appGroup

ResponseJSON(w, http.StatusOK, out)
}
Expand All @@ -138,16 +137,21 @@ func (h *AppGroupHandler) GetAppGroup(w http.ResponseWriter, r *http.Request) {
// @Accept json
// @Produce json
// @Param object body string true "body"
// @Success 200 {object} object
// @Success 200 {object} nil
// @Router /app-groups [delete]
// @Security JWT
func (h *AppGroupHandler) DeleteAppGroup(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appGroupId, ok := vars["appGroupId"]
strId, ok := vars["appGroupId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appGroupId")))
return
}
appGroupId := domain.AppGroupId(strId)
if !appGroupId.Validate() {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appGroupId")))
return
}

err := h.usecase.Delete(appGroupId)
if err != nil {
Expand All @@ -167,36 +171,46 @@ func (h *AppGroupHandler) DeleteAppGroup(w http.ResponseWriter, r *http.Request)
// @Produce json
// @Param appGroupId path string true "appGroupId"
// @Param applicationType query string true "applicationType"
// @Success 200 {object} object
// @Success 200 {object} domain.GetApplicationsResponse
// @Router /app-groups/{appGroupId}/applications [get]
// @Security JWT
func (h *AppGroupHandler) GetApplications(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appGroupId, ok := vars["appGroupId"]
strId, ok := vars["appGroupId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appGroupId")))
return
}
appGroupId := domain.AppGroupId(strId)
if !appGroupId.Validate() {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appGroupId")))
return
}

urlParams := r.URL.Query()
applicationType := urlParams.Get("applicationType")
if applicationType == "" {
applicationType = "PROMETHEUS" // by default
strApplicationType := urlParams.Get("applicationType")
applicationType := domain.ApplicationType_PROMETHEUS // by default
if strApplicationType == "" {
applicationType = domain.ApplicationType_PROMETHEUS
} else {
applicationType.FromString(strApplicationType)
}

log.Debug(applicationType)

applications, err := h.usecase.GetApplications(appGroupId, applicationType)
if err != nil {
log.Error("Failed to get applications err : ", err)
ErrorJSON(w, err)
return
}

var out struct {
Applications []domain.Application `json:"applications"`
var out domain.GetApplicationsResponse
out.Applications = make([]domain.ApplicationResponse, len(applications))
for i, application := range applications {
if err := domain.Map(application, &out.Applications[i]); err != nil {
log.Info(err)
continue
}
}
out.Applications = applications

ResponseJSON(w, http.StatusOK, out)
}
Expand All @@ -208,30 +222,35 @@ func (h *AppGroupHandler) GetApplications(w http.ResponseWriter, r *http.Request
// @Accept json
// @Produce json
// @Param object body domain.UpdateApplicationRequest true "body"
// @Success 200 {object} object
// @Success 200 {object} nil
// @Router /app-groups/{appGroupId}/applications [post]
// @Security JWT
func (h *AppGroupHandler) UpdateApplication(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appGroupId, ok := vars["appGroupId"]
strId, ok := vars["appGroupId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appGroupId")))
return
}

var input = domain.UpdateApplicationRequest{}
body, err := io.ReadAll(r.Body)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
appGroupId := domain.AppGroupId(strId)
if !appGroupId.Validate() {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appGroupId")))
return
}
err = json.Unmarshal(body, &input)

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

err = h.usecase.UpdateApplication(appGroupId, input)
var dto domain.Application
if err := domain.Map(input, &dto); err != nil {
log.Info(err)
}

err = h.usecase.UpdateApplication(dto)
if err != nil {
log.Error("Failed to update application err : ", err)
ErrorJSON(w, err)
Expand Down
27 changes: 11 additions & 16 deletions internal/delivery/http/cloud-setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewCloudSettingHandler(h usecase.ICloudSettingUsecase) *CloudSettingHandler
// @Accept json
// @Produce json
// @Param body body domain.CreateCloudSettingRequest true "create cloud setting request"
// @Success 200 {object} domain.CreateCloudSettingsResponse
// @Success 200 {object} domain.CreateCloudSettingResponse
// @Router /cloud-settings [post]
// @Security JWT
func (h *CloudSettingHandler) CreateCloudSetting(w http.ResponseWriter, r *http.Request) {
Expand All @@ -44,8 +44,7 @@ func (h *CloudSettingHandler) CreateCloudSetting(w http.ResponseWriter, r *http.

var dto domain.CloudSetting
if err = domain.Map(input, &dto); err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
log.Info(err)
}

cloudSettingId, err := h.usecase.Create(r.Context(), dto)
Expand All @@ -54,9 +53,7 @@ func (h *CloudSettingHandler) CreateCloudSetting(w http.ResponseWriter, r *http.
return
}

log.Info("Newly created cloud setting : ", cloudSettingId)

var out domain.CreateCloudSettingsResponse
var out domain.CreateCloudSettingResponse
out.ID = cloudSettingId.String()

ResponseJSON(w, http.StatusOK, out)
Expand Down Expand Up @@ -98,6 +95,7 @@ func (h *CloudSettingHandler) GetCloudSettings(w http.ResponseWriter, r *http.Re
out.CloudSettings = make([]domain.CloudSettingResponse, len(cloudSettings))
for i, cloudSetting := range cloudSettings {
if err := domain.Map(cloudSetting, &out.CloudSettings[i]); err != nil {
log.Info(err)
continue
}
}
Expand All @@ -112,33 +110,32 @@ func (h *CloudSettingHandler) GetCloudSettings(w http.ResponseWriter, r *http.Re
// @Accept json
// @Produce json
// @Param cloudSettingId path string true "cloudSettingId"
// @Success 200 {object} domain.CloudSettingResponse
// @Success 200 {object} domain.GetCloudSettingResponse
// @Router /cloud-settings/{cloudSettingId} [get]
// @Security JWT
func (h *CloudSettingHandler) GetCloudSetting(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
cloudSettingId, ok := vars["cloudSettingId"]
strId, ok := vars["cloudSettingId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid cloudSettingId")))
return
}

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

cloudSetting, err := h.usecase.Get(parsedUuid)
cloudSetting, err := h.usecase.Get(cloudSettingId)
if err != nil {
ErrorJSON(w, err)
return
}

var out domain.GetCloudSettingResponse
if err := domain.Map(cloudSetting, &out.CloudSetting); err != nil {
ErrorJSON(w, err)
return
log.Info(err)
}

ResponseJSON(w, http.StatusOK, out)
Expand Down Expand Up @@ -177,8 +174,7 @@ func (h *CloudSettingHandler) UpdateCloudSetting(w http.ResponseWriter, r *http.

var dto domain.CloudSetting
if err = domain.Map(input, &dto); err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
log.Info(err)
}
dto.ID = parsedUuid

Expand Down Expand Up @@ -225,8 +221,7 @@ func (h *CloudSettingHandler) DeleteCloudSetting(w http.ResponseWriter, r *http.

var dto domain.CloudSetting
if err = domain.Map(input, &dto); err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
log.Info(err)
}
dto.ID = parsedId

Expand Down
Loading

0 comments on commit 398c2c3

Please sign in to comment.