feat: remove relation of application (#1894)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2022-12-05 19:55:03 +08:00
parent f352089f2c
commit 8023aba2cf
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
11 changed files with 1462 additions and 2220 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -169,111 +169,3 @@ func (h *Handlers) GetApplications(ctx *gin.Context) {
h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
ctx.JSON(http.StatusOK, applications)
}
// @Summary Add Scheduler to Application
// @Description Add Scheduler to Application
// @Tags Application
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Param scheduler_cluster_id path string true "scheduler cluster id"
// @Success 200
// @Failure 400
// @Failure 404
// @Failure 500
// @Router /applications/{id}/scheduler-clusters/{scheduler_cluster_id} [put]
func (h *Handlers) AddSchedulerClusterToApplication(ctx *gin.Context) {
var params types.AddSchedulerClusterToApplicationParams
if err := ctx.ShouldBindUri(&params); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
if err := h.service.AddSchedulerClusterToApplication(ctx.Request.Context(), params.ID, params.SchedulerClusterID); err != nil {
ctx.Error(err) // nolint: errcheck
return
}
ctx.Status(http.StatusOK)
}
// @Summary Delete Scheduler to Application
// @Description Delete Scheduler to Application
// @Tags Application
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Param scheduler_cluster_id path string true "scheduler cluster id"
// @Success 200
// @Failure 400
// @Failure 404
// @Failure 500
// @Router /applications/{id}/scheduler-clusters/{scheduler_cluster_id} [delete]
func (h *Handlers) DeleteSchedulerClusterToApplication(ctx *gin.Context) {
var params types.DeleteSchedulerClusterToApplicationParams
if err := ctx.ShouldBindUri(&params); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
if err := h.service.DeleteSchedulerClusterToApplication(ctx.Request.Context(), params.ID, params.SchedulerClusterID); err != nil {
ctx.Error(err) // nolint: errcheck
return
}
ctx.Status(http.StatusOK)
}
// @Summary Add SeedPeer to Application
// @Description Add SeedPeer to Application
// @Tags Application
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Param seed_peer_cluster_id path string true "seed peer cluster id"
// @Success 200
// @Failure 400
// @Failure 404
// @Failure 500
// @Router /applications/{id}/seed-peer-clusters/{seed_peer_cluster_id} [put]
func (h *Handlers) AddSeedPeerClusterToApplication(ctx *gin.Context) {
var params types.AddSeedPeerClusterToApplicationParams
if err := ctx.ShouldBindUri(&params); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
if err := h.service.AddSeedPeerClusterToApplication(ctx.Request.Context(), params.ID, params.SeedPeerClusterID); err != nil {
ctx.Error(err) // nolint: errcheck
return
}
ctx.Status(http.StatusOK)
}
// @Summary Delete SeedPeer to Application
// @Description Delete SeedPeer to Application
// @Tags Application
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Param seed_peer_cluster_id path string true "seed peer cluster id"
// @Success 200
// @Failure 400
// @Failure 404
// @Failure 500
// @Router /applications/{id}/seed-peer-clusters/{seed_peer_cluster_id} [delete]
func (h *Handlers) DeleteSeedPeerClusterToApplication(ctx *gin.Context) {
var params types.DeleteSeedPeerClusterToApplicationParams
if err := ctx.ShouldBindUri(&params); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
if err := h.service.DeleteSeedPeerClusterToApplication(ctx.Request.Context(), params.ID, params.SeedPeerClusterID); err != nil {
ctx.Error(err) // nolint: errcheck
return
}
ctx.Status(http.StatusOK)
}

View File

@ -18,13 +18,11 @@ package model
type Application struct {
Model
Name string `gorm:"column:name;type:varchar(256);index:uk_application_name,unique;not null;comment:name" json:"name"`
DownloadRateLimit uint `gorm:"column:download_rate_limit;comment:download rate limit" json:"download_rate_limit"`
URL string `gorm:"column:url;not null;comment:url" json:"url"`
State string `gorm:"column:state;type:varchar(256);default:'enable';comment:state" json:"state"`
BIO string `gorm:"column:bio;type:varchar(1024);comment:biography" json:"bio"`
UserID uint `gorm:"comment:user id" json:"user_id"`
User User `json:"user"`
SeedPeerClusters []SeedPeerCluster `json:"seed_peer_clusters"`
SchedulerClusters []SchedulerCluster `json:"scheduler_clusters"`
Name string `gorm:"column:name;type:varchar(256);index:uk_application_name,unique;not null;comment:name" json:"name"`
DownloadRateLimit uint `gorm:"column:download_rate_limit;comment:download rate limit" json:"download_rate_limit"`
URL string `gorm:"column:url;not null;comment:url" json:"url"`
State string `gorm:"column:state;type:varchar(256);default:'enable';comment:state" json:"state"`
BIO string `gorm:"column:bio;type:varchar(1024);comment:biography" json:"bio"`
UserID uint `gorm:"comment:user id" json:"user_id"`
User User `json:"user"`
}

View File

@ -26,8 +26,6 @@ type SchedulerCluster struct {
IsDefault bool `gorm:"column:is_default;not null;default:false;comment:default scheduler cluster" json:"is_default"`
SeedPeerClusters []SeedPeerCluster `gorm:"many2many:seed_peer_cluster_scheduler_cluster;" json:"seed_peer_clusters"`
Schedulers []Scheduler `json:"-"`
ApplicationID uint `gorm:"comment:application id" json:"application_id"`
Application Application `json:"-"`
SecurityGroupID uint `gorm:"comment:security group id" json:"security_group_id"`
SecurityGroup SecurityGroup `json:"-"`
Jobs []Job `gorm:"many2many:job_scheduler_cluster;" json:"jobs"`

View File

@ -25,8 +25,6 @@ type SeedPeerCluster struct {
IsDefault bool `gorm:"column:is_default;not null;default:false;comment:default seed peer cluster" json:"is_default"`
SchedulerClusters []SchedulerCluster `gorm:"many2many:seed_peer_cluster_scheduler_cluster;" json:"scheduler_clusters"`
SeedPeers []SeedPeer `json:"-"`
ApplicationID uint `gorm:"comment:application id" json:"application_id"`
Application Application `json:"-"`
SecurityGroupID uint `gorm:"comment:security group id" json:"security_group_id"`
SecurityGroup SecurityGroup `json:"-"`
Jobs []Job `gorm:"many2many:job_seed_peer_cluster;" json:"jobs"`

View File

@ -164,10 +164,6 @@ func Init(cfg *config.Config, logDir string, service service.Service, enforcer *
cs.PATCH(":id", h.UpdateApplication)
cs.GET(":id", h.GetApplication)
cs.GET("", h.GetApplications)
cs.PUT(":id/scheduler-clusters/:scheduler_cluster_id", h.AddSchedulerClusterToApplication)
cs.DELETE(":id/scheduler-clusters/:scheduler_cluster_id", h.DeleteSchedulerClusterToApplication)
cs.PUT(":id/seed-peer-clusters/:seed_peer_cluster_id", h.AddSeedPeerClusterToApplication)
cs.DELETE(":id/seed-peer-clusters/:seed_peer_cluster_id", h.DeleteSeedPeerClusterToApplication)
// Seed Peer Cluster
spc := apiv1.Group("/seed-peer-clusters", jwt.MiddlewareFunc(), rbac)

View File

@ -33,7 +33,7 @@ func (s *service) CreateApplication(ctx context.Context, json types.CreateApplic
State: json.State,
}
if err := s.db.WithContext(ctx).Preload("SeedPeerClusters").Preload("SchedulerClusters").Preload("User").Create(&application).Error; err != nil {
if err := s.db.WithContext(ctx).Preload("User").Create(&application).Error; err != nil {
return nil, err
}
@ -55,7 +55,7 @@ func (s *service) DestroyApplication(ctx context.Context, id uint) error {
func (s *service) UpdateApplication(ctx context.Context, id uint, json types.UpdateApplicationRequest) (*model.Application, error) {
application := model.Application{}
if err := s.db.WithContext(ctx).Preload("SeedPeerClusters").Preload("SchedulerClusters").Preload("User").First(&application, id).Updates(model.Application{
if err := s.db.WithContext(ctx).Preload("User").First(&application, id).Updates(model.Application{
Name: json.Name,
DownloadRateLimit: json.DownloadRateLimit,
URL: json.URL,
@ -71,7 +71,7 @@ func (s *service) UpdateApplication(ctx context.Context, id uint, json types.Upd
func (s *service) GetApplication(ctx context.Context, id uint) (*model.Application, error) {
application := model.Application{}
if err := s.db.WithContext(ctx).Preload("SeedPeerClusters").Preload("SchedulerClusters").Preload("User").First(&application, id).Error; err != nil {
if err := s.db.WithContext(ctx).Preload("User").First(&application, id).Error; err != nil {
return nil, err
}
@ -81,81 +81,9 @@ func (s *service) GetApplication(ctx context.Context, id uint) (*model.Applicati
func (s *service) GetApplications(ctx context.Context, q types.GetApplicationsQuery) ([]model.Application, int64, error) {
var count int64
applications := []model.Application{}
if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Preload("SeedPeerClusters").Preload("SchedulerClusters").Preload("User").Find(&applications).Limit(-1).Offset(-1).Count(&count).Error; err != nil {
if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Preload("User").Find(&applications).Limit(-1).Offset(-1).Count(&count).Error; err != nil {
return nil, 0, err
}
return applications, count, nil
}
func (s *service) AddSchedulerClusterToApplication(ctx context.Context, id, schedulerClusterID uint) error {
application := model.Application{}
if err := s.db.WithContext(ctx).First(&application, id).Error; err != nil {
return err
}
schedulerCluster := model.SchedulerCluster{}
if err := s.db.WithContext(ctx).First(&schedulerCluster, schedulerClusterID).Error; err != nil {
return err
}
if err := s.db.WithContext(ctx).Model(&application).Association("SchedulerClusters").Append(&schedulerCluster); err != nil {
return err
}
return nil
}
func (s *service) DeleteSchedulerClusterToApplication(ctx context.Context, id, schedulerClusterID uint) error {
application := model.Application{}
if err := s.db.WithContext(ctx).First(&application, id).Error; err != nil {
return err
}
schedulerCluster := model.SchedulerCluster{}
if err := s.db.WithContext(ctx).First(&schedulerCluster, schedulerClusterID).Error; err != nil {
return err
}
if err := s.db.Model(&application).Association("SchedulerClusters").Delete(&schedulerCluster); err != nil {
return err
}
return nil
}
func (s *service) AddSeedPeerClusterToApplication(ctx context.Context, id, seedPeerClusterID uint) error {
application := model.Application{}
if err := s.db.WithContext(ctx).First(&application, id).Error; err != nil {
return err
}
seedPeerCluster := model.SeedPeerCluster{}
if err := s.db.WithContext(ctx).First(&seedPeerCluster, seedPeerClusterID).Error; err != nil {
return err
}
if err := s.db.WithContext(ctx).Model(&application).Association("SeedPeerClusters").Append(&seedPeerCluster); err != nil {
return err
}
return nil
}
func (s *service) DeleteSeedPeerClusterToApplication(ctx context.Context, id, seedPeerClusterID uint) error {
application := model.Application{}
if err := s.db.WithContext(ctx).First(&application, id).Error; err != nil {
return err
}
seedPeerCluster := model.SeedPeerCluster{}
if err := s.db.WithContext(ctx).First(&seedPeerCluster, seedPeerClusterID).Error; err != nil {
return err
}
if err := s.db.Model(&application).Association("SeedPeerClusters").Delete(&seedPeerCluster); err != nil {
return err
}
return nil
}

View File

@ -133,10 +133,6 @@ type Service interface {
UpdateApplication(context.Context, uint, types.UpdateApplicationRequest) (*model.Application, error)
GetApplication(context.Context, uint) (*model.Application, error)
GetApplications(context.Context, types.GetApplicationsQuery) ([]model.Application, int64, error)
AddSchedulerClusterToApplication(context.Context, uint, uint) error
DeleteSchedulerClusterToApplication(context.Context, uint, uint) error
AddSeedPeerClusterToApplication(context.Context, uint, uint) error
DeleteSeedPeerClusterToApplication(context.Context, uint, uint) error
CreateModel(context.Context, types.CreateModelParams, types.CreateModelRequest) (*types.Model, error)
DestroyModel(context.Context, types.ModelParams) error

View File

@ -20,26 +20,6 @@ type ApplicationParams struct {
ID uint `uri:"id" binding:"required"`
}
type AddSchedulerClusterToApplicationParams struct {
ID uint `uri:"id" binding:"required"`
SchedulerClusterID uint `uri:"scheduler_cluster_id" binding:"required"`
}
type DeleteSchedulerClusterToApplicationParams struct {
ID uint `uri:"id" binding:"required"`
SchedulerClusterID uint `uri:"scheduler_cluster_id" binding:"required"`
}
type AddSeedPeerClusterToApplicationParams struct {
ID uint `uri:"id" binding:"required"`
SeedPeerClusterID uint `uri:"seed_peer_cluster_id" binding:"required"`
}
type DeleteSeedPeerClusterToApplicationParams struct {
ID uint `uri:"id" binding:"required"`
SeedPeerClusterID uint `uri:"seed_peer_cluster_id" binding:"required"`
}
type CreateApplicationRequest struct {
Name string `json:"name" binding:"required"`
BIO string `json:"bio" binding:"omitempty"`