From 0f2da9e4ad03d0d7ede0bdc5f35b9ed571c2de9f Mon Sep 17 00:00:00 2001 From: Sourav Bhowmik Date: Thu, 7 Nov 2024 11:41:38 +0100 Subject: [PATCH] feat: added staged changes for category --- cmd/laas/docs/docs.go | 26 +++++++++++++++++++------- cmd/laas/docs/swagger.json | 26 +++++++++++++++++++------- cmd/laas/docs/swagger.yaml | 23 ++++++++++++++++------- pkg/models/types.go | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 88 insertions(+), 22 deletions(-) diff --git a/cmd/laas/docs/docs.go b/cmd/laas/docs/docs.go index ec98aa2..eb461c9 100644 --- a/cmd/laas/docs/docs.go +++ b/cmd/laas/docs/docs.go @@ -2156,14 +2156,14 @@ const docTemplate = `{ "category": { "type": "string", "enum": [ - "Distribution", - "Patent", - "Internal", - "Contractual", - "Export Control", - "General" + "DISTRIBUTION", + "PATENT", + "INTERNAL", + "CONTRACTUAL", + "EXPORT CONTROL", + "GENERAL" ], - "example": "Distribution" + "example": "DISTRIBUTION" }, "classification": { "$ref": "#/definitions/models.ObligationClassification" @@ -2223,6 +2223,7 @@ const docTemplate = `{ "models.ObligationDTO": { "type": "object", "required": [ + "category", "classification", "shortnames", "text", @@ -2233,6 +2234,10 @@ const docTemplate = `{ "active": { "type": "boolean" }, + "category": { + "type": "string", + "example": "DISTRIBUTION" + }, "classification": { "type": "string", "example": "GREEN" @@ -2393,10 +2398,17 @@ const docTemplate = `{ }, "models.ObligationUpdateDTO": { "type": "object", + "required": [ + "category" + ], "properties": { "active": { "type": "boolean" }, + "category": { + "type": "string", + "example": "DISTRIBUTION" + }, "classification": { "type": "string", "example": "GREEN" diff --git a/cmd/laas/docs/swagger.json b/cmd/laas/docs/swagger.json index be9c615..902d3cc 100644 --- a/cmd/laas/docs/swagger.json +++ b/cmd/laas/docs/swagger.json @@ -2149,14 +2149,14 @@ "category": { "type": "string", "enum": [ - "Distribution", - "Patent", - "Internal", - "Contractual", - "Export Control", - "General" + "DISTRIBUTION", + "PATENT", + "INTERNAL", + "CONTRACTUAL", + "EXPORT CONTROL", + "GENERAL" ], - "example": "Distribution" + "example": "DISTRIBUTION" }, "classification": { "$ref": "#/definitions/models.ObligationClassification" @@ -2216,6 +2216,7 @@ "models.ObligationDTO": { "type": "object", "required": [ + "category", "classification", "shortnames", "text", @@ -2226,6 +2227,10 @@ "active": { "type": "boolean" }, + "category": { + "type": "string", + "example": "DISTRIBUTION" + }, "classification": { "type": "string", "example": "GREEN" @@ -2386,10 +2391,17 @@ }, "models.ObligationUpdateDTO": { "type": "object", + "required": [ + "category" + ], "properties": { "active": { "type": "boolean" }, + "category": { + "type": "string", + "example": "DISTRIBUTION" + }, "classification": { "type": "string", "example": "GREEN" diff --git a/cmd/laas/docs/swagger.yaml b/cmd/laas/docs/swagger.yaml index 4a8ecd3..85fa958 100644 --- a/cmd/laas/docs/swagger.yaml +++ b/cmd/laas/docs/swagger.yaml @@ -334,13 +334,13 @@ definitions: type: boolean category: enum: - - Distribution - - Patent - - Internal - - Contractual - - Export Control - - General - example: Distribution + - DISTRIBUTION + - PATENT + - INTERNAL + - CONTRACTUAL + - EXPORT CONTROL + - GENERAL + example: DISTRIBUTION type: string classification: $ref: '#/definitions/models.ObligationClassification' @@ -382,6 +382,9 @@ definitions: properties: active: type: boolean + category: + example: DISTRIBUTION + type: string classification: example: GREEN type: string @@ -410,6 +413,7 @@ definitions: example: RISK type: string required: + - category - classification - shortnames - text @@ -502,6 +506,9 @@ definitions: properties: active: type: boolean + category: + example: DISTRIBUTION + type: string classification: example: GREEN type: string @@ -519,6 +526,8 @@ definitions: type: example: RISK type: string + required: + - category type: object models.PaginationMeta: properties: diff --git a/pkg/models/types.go b/pkg/models/types.go index 80aaa92..e4ef659 100644 --- a/pkg/models/types.go +++ b/pkg/models/types.go @@ -13,6 +13,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" "time" "github.com/go-playground/validator/v10" @@ -305,7 +306,27 @@ type Obligation struct { Licenses []*LicenseDB `gorm:"many2many:obligation_licenses;"` Type *ObligationType `gorm:"foreignKey:ObligationTypeId"` Classification *ObligationClassification `gorm:"foreignKey:ObligationClassificationId"` - Category string `json:"category" enums:"Distribution,Patent,Internal,Contractual,Export Control,General" example:"Distribution"` + Category *string `json:"category" enums:"DISTRIBUTION,PATENT,INTERNAL,CONTRACTUAL,EXPORT CONTROL,GENERAL" example:"DISTRIBUTION"` +} + +var validCategories = []string{"DISTRIBUTION", "PATENT", "INTERNAL", "CONTRACTUAL", "EXPORT CONTROL", "GENERAL"} + +func validateCategory(o *Obligation) error { + allCategories := strings.Join(validCategories, ", ") + + // Check if the provided category is in the list of valid categories + categoryValid := false + for _, cat := range validCategories { + if strings.ToUpper(*o.Category) == cat { + categoryValid = true + break + } + } + + if !categoryValid { + return fmt.Errorf("category must be one of the following values: %s", allCategories) + } + return nil } func (o *Obligation) BeforeCreate(tx *gorm.DB) (err error) { @@ -359,6 +380,10 @@ func (o *Obligation) BeforeCreate(tx *gorm.DB) (err error) { } } + if err := validateCategory(o); err != nil { + return err + } + for i := 0; i < len(o.Licenses); i++ { var license LicenseDB if err := tx.Where(LicenseDB{Shortname: o.Licenses[i].Shortname}).First(&license).Error; err != nil { @@ -412,6 +437,9 @@ func (o *Obligation) BeforeUpdate(tx *gorm.DB) (err error) { } } } + if err := validateCategory(o); err != nil { + return err + } if o.Classification != nil { var obClassifications []ObligationClassification if err := tx.Find(&obClassifications).Error; err != nil { @@ -441,6 +469,7 @@ func (o *Obligation) MarshalJSON() ([]byte, error) { Active: o.Active, TextUpdatable: o.TextUpdatable, Shortnames: []string{}, + Category: o.Category, } if o.Type != nil { @@ -476,6 +505,7 @@ func (o *Obligation) UnmarshalJSON(data []byte) error { o.Comment = dto.Comment o.Active = dto.Active o.TextUpdatable = dto.TextUpdatable + o.Category = dto.Category if dto.Type != nil { o.Type = &ObligationType{ @@ -510,6 +540,7 @@ type ObligationDTO struct { Active *bool `json:"active"` TextUpdatable *bool `json:"text_updatable" example:"true"` Shortnames []string `json:"shortnames" validate:"required" example:"GPL-2.0-only,GPL-2.0-or-later"` + Category *string `json:"category" example:"DISTRIBUTION" validate:"required"` } // ObligationUpdateDTO represents an obligation json object. @@ -522,6 +553,7 @@ type ObligationUpdateDTO struct { Comment *string `json:"comment"` Active *bool `json:"active"` TextUpdatable *bool `json:"text_updatable" example:"true"` + Category *string `json:"category" example:"DISTRIBUTION" validate:"required"` } func (obDto *ObligationUpdateDTO) Converter() *Obligation { @@ -539,6 +571,7 @@ func (obDto *ObligationUpdateDTO) Converter() *Obligation { o.Comment = obDto.Comment o.Active = obDto.Active o.TextUpdatable = obDto.TextUpdatable + o.Category = obDto.Category return &o }