Skip to content

Commit

Permalink
Use json serializer for assessments
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Lucidi <[email protected]>
  • Loading branch information
mansam committed Jun 28, 2024
1 parent 4331ace commit f022bb3
Show file tree
Hide file tree
Showing 17 changed files with 377 additions and 353 deletions.
42 changes: 24 additions & 18 deletions api/assessment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"encoding/json"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -148,21 +147,25 @@ func (h AssessmentHandler) Update(ctx *gin.Context) {
// Assessment REST resource.
type Assessment struct {
Resource `yaml:",inline"`
Application *Ref `json:"application,omitempty" yaml:",omitempty" binding:"excluded_with=Archetype"`
Archetype *Ref `json:"archetype,omitempty" yaml:",omitempty" binding:"excluded_with=Application"`
Questionnaire Ref `json:"questionnaire" binding:"required"`
Sections []assessment.Section `json:"sections" binding:"dive"`
Stakeholders []Ref `json:"stakeholders"`
StakeholderGroups []Ref `json:"stakeholderGroups" yaml:"stakeholderGroups"`
Application *Ref `json:"application,omitempty" yaml:",omitempty" binding:"excluded_with=Archetype"`
Archetype *Ref `json:"archetype,omitempty" yaml:",omitempty" binding:"excluded_with=Application"`
Questionnaire Ref `json:"questionnaire" binding:"required"`
Sections []Section `json:"sections" binding:"dive"`
Stakeholders []Ref `json:"stakeholders"`
StakeholderGroups []Ref `json:"stakeholderGroups" yaml:"stakeholderGroups"`
// read only
Risk string `json:"risk"`
Confidence int `json:"confidence"`
Status string `json:"status"`
Thresholds assessment.Thresholds `json:"thresholds"`
RiskMessages assessment.RiskMessages `json:"riskMessages" yaml:"riskMessages"`
Required bool `json:"required"`
Risk string `json:"risk"`
Confidence int `json:"confidence"`
Status string `json:"status"`
Thresholds Thresholds `json:"thresholds"`
RiskMessages RiskMessages `json:"riskMessages" yaml:"riskMessages"`
Required bool `json:"required"`
}

type Section model.Section
type Thresholds model.Thresholds
type RiskMessages model.RiskMessages

// With updates the resource with the model.
func (r *Assessment) With(m *model.Assessment) {
r.Resource.With(&m.Model)
Expand All @@ -186,18 +189,21 @@ func (r *Assessment) With(m *model.Assessment) {
r.Required = a.Questionnaire.Required
r.Risk = a.Risk()
r.Confidence = a.Confidence()
r.RiskMessages = a.RiskMessages
r.Thresholds = a.Thresholds
r.Sections = a.Sections
r.RiskMessages = RiskMessages(a.RiskMessages)
r.Thresholds = Thresholds(a.Thresholds)
r.Sections = []Section{}
for _, s := range a.Sections {
r.Sections = append(r.Sections, Section(s))
}
r.Status = a.Status()
}

// Model builds a model.
func (r *Assessment) Model() (m *model.Assessment) {
m = &model.Assessment{}
m.ID = r.ID
if r.Sections != nil {
m.Sections, _ = json.Marshal(r.Sections)
for _, s := range r.Sections {
m.Sections = append(m.Sections, model.Section(s))
}
m.QuestionnaireID = r.Questionnaire.ID
if r.Archetype != nil {
Expand Down
33 changes: 18 additions & 15 deletions api/questionnaire.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package api

import (
"encoding/json"
"net/http"

"github.com/gin-gonic/gin"
"github.com/konveyor/tackle2-hub/assessment"
"github.com/konveyor/tackle2-hub/model"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -190,13 +188,13 @@ func (h QuestionnaireHandler) Update(ctx *gin.Context) {

type Questionnaire struct {
Resource `yaml:",inline"`
Name string `json:"name" yaml:"name" binding:"required"`
Description string `json:"description" yaml:"description"`
Required bool `json:"required" yaml:"required"`
Sections []assessment.Section `json:"sections" yaml:"sections" binding:"required,min=1,dive"`
Thresholds assessment.Thresholds `json:"thresholds" yaml:"thresholds" binding:"required"`
RiskMessages assessment.RiskMessages `json:"riskMessages" yaml:"riskMessages" binding:"required"`
Builtin bool `json:"builtin,omitempty" yaml:"builtin,omitempty"`
Name string `json:"name" yaml:"name" binding:"required"`
Description string `json:"description" yaml:"description"`
Required bool `json:"required" yaml:"required"`
Sections []Section `json:"sections" yaml:"sections" binding:"required,min=1,dive"`
Thresholds Thresholds `json:"thresholds" yaml:"thresholds" binding:"required"`
RiskMessages RiskMessages `json:"riskMessages" yaml:"riskMessages" binding:"required"`
Builtin bool `json:"builtin,omitempty" yaml:"builtin,omitempty"`
}

// With updates the resource with the model.
Expand All @@ -206,9 +204,12 @@ func (r *Questionnaire) With(m *model.Questionnaire) {
r.Description = m.Description
r.Required = m.Required
r.Builtin = m.Builtin()
_ = json.Unmarshal(m.Sections, &r.Sections)
_ = json.Unmarshal(m.Thresholds, &r.Thresholds)
_ = json.Unmarshal(m.RiskMessages, &r.RiskMessages)
r.Sections = []Section{}
for _, s := range m.Sections {
r.Sections = append(r.Sections, Section(s))
}
r.Thresholds = Thresholds(m.Thresholds)
r.RiskMessages = RiskMessages(m.RiskMessages)
}

// Model builds a model.
Expand All @@ -219,9 +220,11 @@ func (r *Questionnaire) Model() (m *model.Questionnaire) {
Required: r.Required,
}
m.ID = r.ID
m.Sections, _ = json.Marshal(r.Sections)
m.Thresholds, _ = json.Marshal(r.Thresholds)
m.RiskMessages, _ = json.Marshal(r.RiskMessages)
for _, s := range r.Sections {
m.Sections = append(m.Sections, model.Section(s))
}
m.Thresholds = model.Thresholds(r.Thresholds)
m.RiskMessages = model.RiskMessages(r.RiskMessages)

return
}
Loading

0 comments on commit f022bb3

Please sign in to comment.