Skip to content

Commit

Permalink
Merge pull request #1 from klovercloud-ci-cd/47_feature_refactor_comp…
Browse files Browse the repository at this point in the history
…any_model

Refactor update application from application -> company router
  • Loading branch information
zeromsi authored Jul 3, 2022
2 parents 710b89b + fb8c465 commit 335fbab
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 47 deletions.
40 changes: 0 additions & 40 deletions api/v1/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,46 +156,6 @@ func (a applicationApi) GetById(context echo.Context) error {
return common.GenerateErrorResponse(context, "Application Query by ID Failed", "Operation Failed")
}

// Update... Update Application
// @Summary Update Application
// @Description Update Application by company id and repository id
// @Tags Application
// @Accept json
// @Produce json
// @Param data body object true "ApplicationWithUpdateOption Data"
// @Param repositoryId query string true "repository Id"
// @Param companyUpdateOption query string true "Company Update Option"
// @Success 200 {object} common.ResponseDTO
// @Failure 404 {object} common.ResponseDTO
// @Router /api/v1/applications [POST]
func (a applicationApi) Update(context echo.Context) error {
var formData interface{}
if err := context.Bind(&formData); err != nil {
return err
}
repoId := context.QueryParam("repositoryId")
var companyId string
companyUpdateOption := context.QueryParam("companyUpdateOption")
if config.EnableAuthentication {
userResourcePermission, err := GetUserResourcePermissionFromBearerToken(context, a.jwtService)
if err != nil {
return common.GenerateUnauthorizedResponse(context, err, err.Error())
}
if err := checkAuthority(userResourcePermission, string(enums.APPLICATION), "", string(enums.UPDATE)); err != nil {
return common.GenerateUnauthorizedResponse(context, err, err.Error())
}
companyId = userResourcePermission.Metadata.CompanyId
}
code, err := a.applicationService.UpdateApplication(companyId, repoId, formData, companyUpdateOption)
if err != nil {
return common.GenerateErrorResponse(context, nil, err.Error())
}
if code == 200 {
return context.JSON(code, "Successfully updated")
}
return common.GenerateErrorResponse(context, nil, err.Error())
}

// NewApplicationApi returns Application type api
func NewApplicationApi(applicationService service.Company, jwtService service.Jwt) api.Application {
return &applicationApi{
Expand Down
2 changes: 1 addition & 1 deletion api/v1/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func GithubEventRouter(g *echo.Group) {
// ApplicationRouter api/v1/applications/* router
func ApplicationRouter(g *echo.Group) {
applicationApi := NewApplicationApi(dependency.GetV1CompanyService(), dependency.GetV1JwtService())
g.POST("", applicationApi.Update)
g.GET("/:id", applicationApi.GetById)
g.GET("", applicationApi.GetAll)
g.POST("/:id/pipelines", applicationApi.CreatePipeline)
Expand Down Expand Up @@ -101,6 +100,7 @@ func CompanyRouter(g *echo.Group) {
g.GET("/:id", companyApi.GetById)
g.GET("/:id/repositories", companyApi.GetRepositoriesById)
g.PUT("/:id/repositories", companyApi.UpdateRepositories)
g.PUT("/:id/repositories/:repoId/applications", companyApi.UpdateApplications)
g.GET("/:id/applications", companyApi.GetApplicationsByCompanyIdAndRepositoryType)
g.PATCH("/:id/repositories/:repoId/webhooks", companyApi.UpdateWebhook)
}
Expand Down
49 changes: 48 additions & 1 deletion api/v1/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c companyApi) UpdateRepositories(context echo.Context) error {
return common.GenerateUnauthorizedResponse(context, err, err.Error())
}
if id != userResourcePermission.Metadata.CompanyId {
return context.JSON(404, "Repository not found!")
return context.JSON(404, "Company not found!")
}
}
companyUpdateOption := context.QueryParam("companyUpdateOption")
Expand All @@ -98,6 +98,53 @@ func (c companyApi) UpdateRepositories(context echo.Context) error {
return common.GenerateErrorResponse(context, nil, err.Error())
}

// Update... Update repositories
// @Summary Update repositories by company id
// @Description updates repositories
// @Tags Company
// @Produce json
// @Param data body v1.RepositoriesDto true "RepositoriesDto data"
// @Param id path string true "Company id"
// @Param repoId path string true "Repository id"
// @Param companyUpdateOption query string true "Company Update Option"
// @Success 200 {object} common.ResponseDTO
// @Router /api/v1/companies/{id}/repositories/{repoId}/applications [PUT]
func (c companyApi) UpdateApplications(context echo.Context) error {
var formData interface{}
if err := context.Bind(&formData); err != nil {
return err
}
id := context.Param("id")
if id == "" {
return common.GenerateErrorResponse(context, "[ERROR]: Company Id is not provided", "Please provide Company Id")
}
repoId := context.Param("repoId")
if repoId == "" {
return common.GenerateErrorResponse(context, "[ERROR]: Repository Id is not provided", "Please provide repository id")
}
if config.EnableAuthentication {
userResourcePermission, err := GetUserResourcePermissionFromBearerToken(context, c.jwtService)
if err != nil {
return common.GenerateUnauthorizedResponse(context, err, err.Error())
}
if err := checkAuthority(userResourcePermission, string(enums.REPOSITORY), "", string(enums.UPDATE)); err != nil {
return common.GenerateUnauthorizedResponse(context, err, err.Error())
}
if id != userResourcePermission.Metadata.CompanyId {
return context.JSON(404, "Company not found!")
}
}
companyUpdateOption := context.QueryParam("companyUpdateOption")
code, err := c.companyService.UpdateApplications(id, repoId, formData, companyUpdateOption)
if err != nil {
return common.GenerateErrorResponse(context, nil, err.Error())
}
if code == 200 {
return context.JSON(code, "Applications Updated Successfully")
}
return common.GenerateErrorResponse(context, nil, err.Error())
}

// Save... Save company
// @Summary Save company
// @Description Saves company
Expand Down
1 change: 0 additions & 1 deletion core/v1/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

// Application application api operations
type Application interface {
Update(context echo.Context) error
GetById(context echo.Context) error
GetAll(context echo.Context) error
CreatePipeline(context echo.Context) error
Expand Down
1 change: 1 addition & 0 deletions core/v1/api/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Company interface {
Get(context echo.Context) error
GetRepositoriesById(context echo.Context) error
UpdateRepositories(context echo.Context) error
UpdateApplications(context echo.Context) error
GetApplicationsByCompanyIdAndRepositoryType(context echo.Context) error
UpdateWebhook(context echo.Context) error
}
6 changes: 3 additions & 3 deletions core/v1/logic/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (c companyService) GetApplicationByApplicationId(companyId string, repoId s
return code, response
}

func (c companyService) UpdateApplication(id string, repoId string, payload interface{}, option string) (httpCode int, err error) {
func (c companyService) UpdateApplications(id string, repoId string, payload interface{}, option string) (httpCode int, err error) {
marshal, err := json.Marshal(payload)
if err != nil {
return http.StatusBadRequest, err
Expand All @@ -119,7 +119,7 @@ func (c companyService) UpdateApplication(id string, repoId string, payload inte
header["token"] = config.Token
header["Content-Type"] = "application/json"

code, err := c.httpPublisher.Post(config.KlovercloudIntegrationMangerUrl+"/applications?companyId="+id+"&repositoryId="+repoId+"&companyUpdateOption="+option, header, marshal)
code, err := c.httpPublisher.Put(config.KlovercloudIntegrationMangerUrl+"/companies/"+id+"/repositories/"+repoId+"/applications?companyUpdateOption="+option, header, marshal)
return code, err
}

Expand Down Expand Up @@ -171,7 +171,7 @@ func (c companyService) GetRepositoriesById(id string, option v1.CompanyQueryOpt
var response interface{}
header := make(map[string]string)
header["token"] = config.Token
code, b, err := c.httpPublisher.Get(config.KlovercloudIntegrationMangerUrl+"/companies/"+id+"/repositories"+"?loadRepositories="+option.LoadRepositories+"&loadApplications="+option.LoadApplications, header)
code, b, err := c.httpPublisher.Get(config.KlovercloudIntegrationMangerUrl+"/companies/"+id+"/repositories"+"?page="+option.Pagination.Page+"&limit="+option.Pagination.Limit+"&loadRepositories="+option.LoadRepositories+"&loadApplications="+option.LoadApplications, header)
if err != nil {
return code, nil
}
Expand Down
2 changes: 1 addition & 1 deletion core/v1/service/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Company interface {
GetRepositoryByRepositoryId(id string, companyId string, loadApplications string) (httpCode int, body interface{})
GetApplicationsByRepositoryId(repoId string, companyId string, option v1.RepositoryQueryOption, status string) (httpCode int, body interface{})
UpdateRepositories(companyId string, company interface{}, option string) (httpCode int, error error)
UpdateApplication(id string, repoId string, payload interface{}, option string) (httpCode int, error error)
UpdateApplications(id string, repoId string, payload interface{}, option string) (httpCode int, error error)
GetApplicationByApplicationId(companyId string, repoId string, applicationId string) (httpCode int, data interface{})
GetAllApplications(companyId string, option v1.CompanyQueryOption) (httpCode int, data interface{})
GetApplicationsByCompanyIdAndRepositoryType(id string, _type string, option v1.CompanyQueryOption, status string) (httpCode int, data interface{})
Expand Down

0 comments on commit 335fbab

Please sign in to comment.