Enhance CMS Entity and Forms with Type Field
- Added a new Type field to the CMS entity to differentiate between Company and Organization types, improving data categorization. - Updated the CMSForm to include validation for the Type field, ensuring proper input handling. - Modified the ToResponse method to include the Type field in the CMSResponse, enhancing the API response structure. - Adjusted the copyFormToEntity method in the CMS service to handle the new Type field, maintaining data integrity based on the selected type. - Enhanced Swagger documentation to reflect the changes in the CMS API, providing clear examples for the new Type field.
This commit is contained in:
@@ -13,6 +13,7 @@ type CMS struct {
|
|||||||
Language string `bson:"language"`
|
Language string `bson:"language"`
|
||||||
CompanyName string `bson:"company_name"`
|
CompanyName string `bson:"company_name"`
|
||||||
Country string `bson:"country"`
|
Country string `bson:"country"`
|
||||||
|
Type CMSType `bson:"type"`
|
||||||
Hero heroSection `bson:"hero"`
|
Hero heroSection `bson:"hero"`
|
||||||
Chart chartSection `bson:"chart"`
|
Chart chartSection `bson:"chart"`
|
||||||
Features cardSection `bson:"features"`
|
Features cardSection `bson:"features"`
|
||||||
@@ -22,6 +23,13 @@ type CMS struct {
|
|||||||
Footer footerSection `bson:"footer"`
|
Footer footerSection `bson:"footer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CMSType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
CMSTypeCompany CMSType = "company"
|
||||||
|
CMSTypeOrganization CMSType = "organization"
|
||||||
|
)
|
||||||
|
|
||||||
// SetID sets the CMS ID (implements IDSetter interface)
|
// SetID sets the CMS ID (implements IDSetter interface)
|
||||||
func (c *CMS) SetID(id string) {
|
func (c *CMS) SetID(id string) {
|
||||||
c.ID, _ = bson.ObjectIDFromHex(id)
|
c.ID, _ = bson.ObjectIDFromHex(id)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ type CMSForm struct {
|
|||||||
Language string `json:"language" valid:"optional,length(2|100)" example:"en"`
|
Language string `json:"language" valid:"optional,length(2|100)" example:"en"`
|
||||||
CompanyName string `json:"company_name" valid:"optional,length(2|100)" example:"Opplens"`
|
CompanyName string `json:"company_name" valid:"optional,length(2|100)" example:"Opplens"`
|
||||||
Country string `json:"country" valid:"optional,length(3)" example:"SWE"`
|
Country string `json:"country" valid:"optional,length(3)" example:"SWE"`
|
||||||
|
Type CMSType `json:"type" valid:"optional,in(company|organization)" example:"company"`
|
||||||
Hero heroSectionForm `json:"hero" valid:"optional"`
|
Hero heroSectionForm `json:"hero" valid:"optional"`
|
||||||
Chart chartSectionForm `json:"chart" valid:"optional"`
|
Chart chartSectionForm `json:"chart" valid:"optional"`
|
||||||
Features cardSectionForm `json:"features" valid:"optional"`
|
Features cardSectionForm `json:"features" valid:"optional"`
|
||||||
@@ -88,6 +89,10 @@ type SearchCMSForm struct {
|
|||||||
type CMSResponse struct {
|
type CMSResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
|
Language string `json:"language"`
|
||||||
|
CompanyName string `json:"company_name"`
|
||||||
|
Country string `json:"country"`
|
||||||
|
Type CMSType `json:"type"`
|
||||||
Hero heroSection `json:"hero"`
|
Hero heroSection `json:"hero"`
|
||||||
Chart chartSection `json:"chart"`
|
Chart chartSection `json:"chart"`
|
||||||
Features cardSection `json:"features"`
|
Features cardSection `json:"features"`
|
||||||
@@ -110,6 +115,10 @@ func (c *CMS) ToResponse() *CMSResponse {
|
|||||||
return &CMSResponse{
|
return &CMSResponse{
|
||||||
ID: c.GetID(),
|
ID: c.GetID(),
|
||||||
Key: c.Key,
|
Key: c.Key,
|
||||||
|
Language: c.Language,
|
||||||
|
CompanyName: c.CompanyName,
|
||||||
|
Country: c.Country,
|
||||||
|
Type: c.Type,
|
||||||
Hero: c.Hero,
|
Hero: c.Hero,
|
||||||
Chart: c.Chart,
|
Chart: c.Chart,
|
||||||
Features: c.Features,
|
Features: c.Features,
|
||||||
|
|||||||
@@ -236,6 +236,12 @@ func (s *cmsService) Search(ctx context.Context, form *SearchCMSForm, pagination
|
|||||||
|
|
||||||
// copyFormToEntity copies data from form to entity
|
// copyFormToEntity copies data from form to entity
|
||||||
func (s *cmsService) copyFormToEntity(form *CMSForm, cms *CMS) {
|
func (s *cmsService) copyFormToEntity(form *CMSForm, cms *CMS) {
|
||||||
|
if form.Type != "" {
|
||||||
|
cms.Type = CMSType(form.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch cms.Type {
|
||||||
|
case CMSTypeCompany:
|
||||||
if form.Language != "" {
|
if form.Language != "" {
|
||||||
cms.Language = form.Language
|
cms.Language = form.Language
|
||||||
}
|
}
|
||||||
@@ -245,6 +251,12 @@ func (s *cmsService) copyFormToEntity(form *CMSForm, cms *CMS) {
|
|||||||
if form.Country != "" {
|
if form.Country != "" {
|
||||||
cms.Country = form.Country
|
cms.Country = form.Country
|
||||||
}
|
}
|
||||||
|
case CMSTypeOrganization:
|
||||||
|
cms.Language = ""
|
||||||
|
cms.CompanyName = ""
|
||||||
|
cms.Country = ""
|
||||||
|
}
|
||||||
|
|
||||||
// Hero section
|
// Hero section
|
||||||
if form.Hero.Title != "" {
|
if form.Hero.Title != "" {
|
||||||
cms.Hero.Title = form.Hero.Title
|
cms.Hero.Title = form.Hero.Title
|
||||||
|
|||||||
Reference in New Issue
Block a user