From fb8c465d07860eed921cac64e345c3cc75135dbf Mon Sep 17 00:00:00 2001 From: niloydeb1 Date: Sun, 3 Jul 2022 15:20:29 +0600 Subject: [PATCH] Refactor update application from application -> company router --- api/v1/application.go | 40 ------------------------------- api/v1/base.go | 2 +- api/v1/company.go | 49 +++++++++++++++++++++++++++++++++++++- core/v1/api/application.go | 1 - core/v1/api/company.go | 1 + core/v1/logic/company.go | 6 ++--- core/v1/service/company.go | 2 +- 7 files changed, 54 insertions(+), 47 deletions(-) diff --git a/api/v1/application.go b/api/v1/application.go index 028e4d9..ed55dd1 100644 --- a/api/v1/application.go +++ b/api/v1/application.go @@ -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{ diff --git a/api/v1/base.go b/api/v1/base.go index 3ac4fbb..3657a9f 100644 --- a/api/v1/base.go +++ b/api/v1/base.go @@ -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) @@ -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) } diff --git a/api/v1/company.go b/api/v1/company.go index 1bc305f..b84a433 100644 --- a/api/v1/company.go +++ b/api/v1/company.go @@ -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") @@ -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 diff --git a/core/v1/api/application.go b/core/v1/api/application.go index f1ead3d..0df47a6 100644 --- a/core/v1/api/application.go +++ b/core/v1/api/application.go @@ -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 diff --git a/core/v1/api/company.go b/core/v1/api/company.go index 65d66b8..7758755 100644 --- a/core/v1/api/company.go +++ b/core/v1/api/company.go @@ -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 } diff --git a/core/v1/logic/company.go b/core/v1/logic/company.go index d9dfd09..52ae508 100644 --- a/core/v1/logic/company.go +++ b/core/v1/logic/company.go @@ -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 @@ -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 } @@ -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 } diff --git a/core/v1/service/company.go b/core/v1/service/company.go index 4f3594d..33513ed 100644 --- a/core/v1/service/company.go +++ b/core/v1/service/company.go @@ -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{})