Skip to content

Commit

Permalink
feat: added staged changes for category
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourav Bhowmik committed Nov 7, 2024
1 parent 2974e9f commit 848453f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 22 deletions.
26 changes: 19 additions & 7 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2223,6 +2223,7 @@ const docTemplate = `{
"models.ObligationDTO": {
"type": "object",
"required": [
"category",
"classification",
"shortnames",
"text",
Expand All @@ -2233,6 +2234,10 @@ const docTemplate = `{
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down Expand Up @@ -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"
Expand Down
26 changes: 19 additions & 7 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2216,6 +2216,7 @@
"models.ObligationDTO": {
"type": "object",
"required": [
"category",
"classification",
"shortnames",
"text",
Expand All @@ -2226,6 +2227,10 @@
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down Expand Up @@ -2386,10 +2391,17 @@
},
"models.ObligationUpdateDTO": {
"type": "object",
"required": [
"category"
],
"properties": {
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down
23 changes: 16 additions & 7 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -382,6 +382,9 @@ definitions:
properties:
active:
type: boolean
category:
example: DISTRIBUTION
type: string
classification:
example: GREEN
type: string
Expand Down Expand Up @@ -410,6 +413,7 @@ definitions:
example: RISK
type: string
required:
- category
- classification
- shortnames
- text
Expand Down Expand Up @@ -502,6 +506,9 @@ definitions:
properties:
active:
type: boolean
category:
example: DISTRIBUTION
type: string
classification:
example: GREEN
type: string
Expand All @@ -519,6 +526,8 @@ definitions:
type:
example: RISK
type: string
required:
- category
type: object
models.PaginationMeta:
properties:
Expand Down
35 changes: 34 additions & 1 deletion pkg/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -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 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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 848453f

Please sign in to comment.