Files

277 lines
9.8 KiB
Go

package pages
import (
"api-service/internal/models"
"database/sql"
"encoding/json"
"time"
"github.com/lib/pq"
)
// Rol_pages represents the data structure for the role_access.rol_pages table
// with proper null handling and optimized JSON marshaling
type Rol_pages struct {
ID int64 `json:"id" db:"id"`
IDs int64 `json:"id" db:"id"`
Name string `json:"name," db:"name"`
Icon sql.NullString `json:"icon,omitempty" db:"icon"`
Url sql.NullString `json:"url,omitempty" db:"url"`
Level int64 `json:"level" db:"level"`
Sort int64 `json:"sort" db:"sort"`
Parent models.NullableInt32 `json:"parent,omitempty" db:"parent"`
Active bool `json:"active" db:"active"`
Components []Rol_component `json:"list_component,omitempty" db:"list_component"`
Permissions []Rol_permission `json:"list_permission,omitempty" db:"permission"`
}
type Rol_component struct {
ID int64 `json:"id,omitempty" db:"id"`
Name string `json:"name,omitempty" db:"name"`
Description sql.NullString `json:"description,omitempty" db:"description"`
Directory string `json:"directory,omitempty" db:"directory"`
Active bool `json:"active,omitempty" db:"active"`
FkRolPagesID int64 `json:"fk_rol_pages_id,omitempty" db:"fk_rol_pages_id"`
Sort int64 `json:"sort,omitempty" db:"sort"`
}
type Rol_permission struct {
ID int64 `json:"id,omitempty" db:"id"`
Create sql.NullBool `json:"create,omitempty" db:"create"`
Read sql.NullBool `json:"read,omitempty" db:"read"`
Update sql.NullBool `json:"update,omitempty" db:"update"`
Disable sql.NullBool `json:"disable,omitempty" db:"disable"` // Note: "disable" is a Go keyword, so "Disable" is used for the field name.
Delete sql.NullBool `json:"delete,omitempty" db:"delete"`
Active sql.NullBool `json:"active,omitempty" db:"active"`
FkRolPagesID models.NullableInt32 `json:"fk_rol_pages_id,omitempty" db:"fk_rol_pages_id"`
RoleKeycloak pq.StringArray `json:"role_keycloak,omitempty" db:"role_keycloak"` // Use NullString for optional text fields
GroupKeycloak pq.StringArray `json:"group_keycloak,omitempty" db:"group_keycloak"`
}
// Custom JSON marshaling for Rol_pages so NULL values don't appear in response
func (r Rol_pages) MarshalJSON() ([]byte, error) {
type Alias Rol_pages
aux := &struct {
*Alias
Icon *string `json:"icon,omitempty"`
Url *string `json:"url,omitempty"`
Parent *int `json:"parent,omitempty"`
}{
Alias: (*Alias)(&r),
}
if r.Icon.Valid {
aux.Icon = &r.Icon.String
}
if r.Url.Valid {
aux.Url = &r.Url.String
}
if r.Parent.Valid {
parent := int(r.Parent.Int32)
aux.Parent = &parent
}
return json.Marshal(aux)
}
func (r Rol_component) MarshalJSON() ([]byte, error) {
type Alias Rol_component
aux := &struct {
*Alias
Description *string `json:"description,omitempty"`
}{
Alias: (*Alias)(&r),
}
if r.Description.Valid {
aux.Description = &r.Description.String
}
return json.Marshal(aux)
}
func (r Rol_permission) MarshalJSON() ([]byte, error) {
type Alias Rol_permission
aux := &struct {
*Alias
Create *bool `json:"create,omitempty"`
Read *bool `json:"read,omitempty"`
Update *bool `json:"update,omitempty"`
Disable *bool `json:"disable,omitempty"`
Delete *bool `json:"delete,omitempty"`
Active *bool `json:"active,omitempty"`
FkRolPagesID *int `json:"fk_rol_pages_id,omitempty"`
}{
Alias: (*Alias)(&r),
}
if r.Create.Valid {
aux.Create = &r.Create.Bool
}
if r.Read.Valid {
aux.Read = &r.Read.Bool
}
if r.Update.Valid {
aux.Update = &r.Update.Bool
}
if r.Disable.Valid {
aux.Disable = &r.Disable.Bool
}
if r.Delete.Valid {
aux.Delete = &r.Delete.Bool
}
if r.Active.Valid {
aux.Active = &r.Active.Bool
}
if r.FkRolPagesID.Valid {
fkRolPagesID := int(r.FkRolPagesID.Int32)
aux.FkRolPagesID = &fkRolPagesID
}
return json.Marshal(aux)
}
// Helper method to safely get Icon
func (r *Rol_pages) GetIcon() string {
if r.Icon.Valid {
return r.Icon.String
}
return ""
}
// Helper method to safely get Url
func (r *Rol_pages) GetUrl() string {
if r.Url.Valid {
return r.Url.String
}
return ""
}
// Helper method to safely get Parent
func (r *Rol_pages) GetParent() int {
if r.Parent.Valid {
return int(r.Parent.Int32)
}
return 0
}
// Helper method to safely get Description
func (r *Rol_component) GetDescription() string {
if r.Description.Valid {
return r.Description.String
}
return ""
}
// Helper method to safely get Sort
func (r *Rol_pages) GetSort() int64 {
return r.Sort
}
// Response struct for GET list
type PagesGetResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data []Rol_pages `json:"data"`
Meta models.MetaResponse `json:"meta"`
Summary *models.AggregateData `json:"summary,omitempty"`
}
// Response struct for create
type PagesCreateResponse struct {
Message string `json:"message"`
Data *Rol_pages `json:"data"`
}
// Request struct for create
type PagesCreateRequest struct {
//Status string `json:"status" validate:"required,oneof=draft active inactive"`
ID *int `json:"id"`
Name string `json:"name" validate:"required,min=1,max=20"`
Icon *string `json:"icon" validate:"omitempty,min=1,max=100"`
Url *string `json:"url" validate:"omitempty,min=1,max=100"`
Level int `json:"level"`
Sort int `json:"sort"`
Parent *int `json:"parent" validate:"omitempty,min=1"`
Active bool `json:"active"`
Components []ComponentCreateRequest `json:"components,omitempty" validate:"omitempty,dive"`
Permissions []PermissionCreateRequest `json:"permissions,omitempty" validate:"omitempty,dive"`
}
type ComponentCreateRequest struct {
Name string `json:"name" validate:"required,min=1,max=100"`
Description *string `json:"description" validate:"omitempty,max=255"`
Directory string `json:"directory" validate:"required,min=1,max=255"`
Active bool `json:"active"`
Sort int64 `json:"sort"`
// FkRolPagesID int64 `json:"fk_rol_pages_id" validate:"required,min=1"`
}
type PermissionCreateRequest struct {
Create *bool `json:"create" validate:"omitempty"`
Read *bool `json:"read" validate:"omitempty"`
Update *bool `json:"update" validate:"omitempty"`
Disable *bool `json:"disable" validate:"omitempty"`
Delete *bool `json:"delete" validate:"omitempty"`
Active *bool `json:"active" validate:"omitempty"`
// FkRolPagesID *int64 `json:"fk_rol_pages_id" validate:"omitempty,"`
RoleKeycloak *[]string `json:"role_keycloak" validate:"omitempty"`
GroupKeycloak *[]string `json:"group_keycloak" validate:"omitempty"`
}
type ComponentUpdateRequest struct {
ID *int `json:"id" validate:"required"`
Name *string `json:"name" validate:"omitempty,min=1,max=100"`
Description *string `json:"description" validate:"omitempty,max=255"`
Directory *string `json:"directory" validate:"omitempty,min=1,max=255"`
Active *bool `json:"active" validate:"omitempty"`
Sort *int `json:"sort" validate:"omitempty,min=1"`
FkRolPagesID *int `json:"fk_rol_pages_id" validate:"omitempty,min=1"`
}
type PermissionUpdateRequest struct {
ID *int `json:"id" validate:"required"`
Create *bool `json:"create" validate:"omitempty"`
Read *bool `json:"read" validate:"omitempty"`
Update *bool `json:"update" validate:"omitempty"`
Disable *bool `json:"disable" validate:"omitempty"`
Delete *bool `json:"delete" validate:"omitempty"`
Active *bool `json:"active" validate:"omitempty"`
FkRolPagesID *int `json:"fk_rol_pages_id" validate:"omitempty"`
RoleKeycloak *[]string `json:"role_keycloak" validate:"omitempty"`
GroupKeycloak *[]string `json:"group_keycloak" validate:"omitempty"`
}
// Response struct for update
type PagesUpdateResponse struct {
Message string `json:"message"`
Data *Rol_pages `json:"data"`
}
// Update request
type PagesUpdateRequest struct {
IDs *int `json:"id" validate:"omitempty"` // used when updating via JSON array (bulk)
ID *int `json:"-" validate:"omitempty,min=1"` // required for single update via URL param
// Status string `json:"status" validate:"required,oneof=draft active inactive"`
Name *string `json:"name" validate:"omitempty,min=1,max=20"`
Icon *string `json:"icon" validate:"omitempty,min=1,max=100"`
Url *string `json:"url" validate:"omitempty,min=1,max=100"`
Level *int `json:"level" validate:"omitempty,min=1"`
Sort *int `json:"sort" validate:"omitempty,min=1"`
Parent *int `json:"parent" validate:"omitempty,min=1"`
Active *bool `json:"active" validate:"omitempty"`
Components []ComponentUpdateRequest `json:"components,omitempty" validate:"omitempty,dive"`
Permissions []PermissionUpdateRequest `json:"permissions,omitempty" validate:"omitempty,dive"`
}
// Response struct for delete
type PagesDeleteResponse struct {
Message string `json:"message"`
ID string `json:"id"`
}
// Filter struct for query parameters
type Rol_pagesFilter struct {
Search *string `json:"search,omitempty" form:"search"`
DateFrom *time.Time `json:"date_from,omitempty" form:"date_from"`
DateTo *time.Time `json:"date_to,omitempty" form:"date_to"`
Status *string `json:"status,omitempty" form:"status"`
}