clean up unnecessary code
This commit is contained in:
		
							parent
							
								
									8b681922f6
								
							
						
					
					
						commit
						3c811c9533
					
				@ -1,70 +1,55 @@
 | 
				
			|||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"database/sql"
 | 
					 | 
				
			||||||
	"maps"
 | 
					 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SettingModel struct {
 | 
					type SettingGroup struct {
 | 
				
			||||||
	DB *sql.DB
 | 
						id          int
 | 
				
			||||||
 | 
						description string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SettingConfig struct {
 | 
					func (g *SettingGroup) Id() int {
 | 
				
			||||||
 | 
						return g.id
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SettingGroup int
 | 
					func (g *SettingGroup) Description() string {
 | 
				
			||||||
 | 
						return g.description
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	SettingGroupUser      SettingGroup = 1
 | 
						SETTING_GROUP_USER      = "user"
 | 
				
			||||||
	SettingGroupGuestbook SettingGroup = 2
 | 
						SETTING_GROUP_GUESTBOOK = "guestbook"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SettingDataType int
 | 
					type SettingDataType struct {
 | 
				
			||||||
 | 
						id          int
 | 
				
			||||||
 | 
						description string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d *SettingDataType) Id() int {
 | 
				
			||||||
 | 
						return d.id
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d *SettingDataType) Description() string {
 | 
				
			||||||
 | 
						return d.description
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	SettingTypeInt      SettingDataType = 1
 | 
						SETTING_TYPE_INTEGER = "integer"
 | 
				
			||||||
	SettingTypeDate     SettingDataType = 2
 | 
						SETTING_TYPE_STRING  = "alphanumeric"
 | 
				
			||||||
	SettingTypeAlphanum SettingDataType = 3
 | 
						SETTING_TYPE_DATE    = "datetime"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SettingValue struct {
 | 
					 | 
				
			||||||
	Id                 int
 | 
					 | 
				
			||||||
	SettingId          int
 | 
					 | 
				
			||||||
	AllowedSettingId   int
 | 
					 | 
				
			||||||
	UnconstrainedValue string
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type AllowedSettingValue struct {
 | 
					 | 
				
			||||||
	id        int
 | 
					 | 
				
			||||||
	itemValue string
 | 
					 | 
				
			||||||
	caption   string
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (s *AllowedSettingValue) Id() int {
 | 
					 | 
				
			||||||
	return s.id
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (s *AllowedSettingValue) ItemValue() string {
 | 
					 | 
				
			||||||
	return s.itemValue
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (s *AllowedSettingValue) Caption() string {
 | 
					 | 
				
			||||||
	return s.caption
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Setting struct {
 | 
					type Setting struct {
 | 
				
			||||||
	id               int
 | 
						id           int
 | 
				
			||||||
	description      string
 | 
						description  string
 | 
				
			||||||
	constrained      bool
 | 
						constrained  bool
 | 
				
			||||||
	dataType         SettingDataType
 | 
						dataType     SettingDataType
 | 
				
			||||||
	dataTypeDesc     string
 | 
						settingGroup SettingGroup
 | 
				
			||||||
	settingGroup     SettingGroup
 | 
						minValue     string
 | 
				
			||||||
	settingGroupDesc string
 | 
						maxValue     string
 | 
				
			||||||
	minValue         string // TODO: Maybe should be int?
 | 
					 | 
				
			||||||
	maxValue         string
 | 
					 | 
				
			||||||
	allowedValues    map[int]AllowedSettingValue
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *Setting) Id() int {
 | 
					func (s *Setting) Id() int {
 | 
				
			||||||
@ -95,32 +80,18 @@ func (s *Setting) MaxValue() string {
 | 
				
			|||||||
	return s.maxValue
 | 
						return s.maxValue
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *Setting) AllowedValues() map[int]AllowedSettingValue {
 | 
					func (s *Setting) Validate(value string) bool {
 | 
				
			||||||
	result := make(map[int]AllowedSettingValue, 50)
 | 
						switch s.dataType.description {
 | 
				
			||||||
	maps.Copy(result, s.allowedValues)
 | 
						case SETTING_TYPE_INTEGER:
 | 
				
			||||||
	return result
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (s *Setting) ValidateUnconstrained(value string) bool {
 | 
					 | 
				
			||||||
	switch s.dataType {
 | 
					 | 
				
			||||||
	case SettingTypeInt:
 | 
					 | 
				
			||||||
		return s.validateInt(value)
 | 
							return s.validateInt(value)
 | 
				
			||||||
	case SettingTypeAlphanum:
 | 
						case SETTING_TYPE_STRING:
 | 
				
			||||||
		return s.validateAlphanum(value)
 | 
							return s.validateAlphanum(value)
 | 
				
			||||||
	case SettingTypeDate:
 | 
						case SETTING_TYPE_DATE:
 | 
				
			||||||
		return s.validateDatetime(value)
 | 
							return s.validateDatetime(value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return false
 | 
						return false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *Setting) ValidateConstrained(value *AllowedSettingValue) bool {
 | 
					 | 
				
			||||||
	_, exists := s.allowedValues[value.id]
 | 
					 | 
				
			||||||
	if s.constrained && exists {
 | 
					 | 
				
			||||||
		return true
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return false
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (s *Setting) validateInt(value string) bool {
 | 
					func (s *Setting) validateInt(value string) bool {
 | 
				
			||||||
	v, err := strconv.ParseInt(value, 10, 0)
 | 
						v, err := strconv.ParseInt(value, 10, 0)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@ -179,45 +150,5 @@ func (s *Setting) validateDatetime(value string) bool {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *Setting) validateAlphanum(value string) bool {
 | 
					func (s *Setting) validateAlphanum(value string) bool {
 | 
				
			||||||
	return true
 | 
						return len(value) >= 0
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (s Setting) Validate(value string) bool {
 | 
					 | 
				
			||||||
	switch s.dataType {
 | 
					 | 
				
			||||||
	case SettingTypeInt:
 | 
					 | 
				
			||||||
		return s.validateInt(value)
 | 
					 | 
				
			||||||
	case SettingTypeAlphanum:
 | 
					 | 
				
			||||||
		return s.validateAlphanum(value)
 | 
					 | 
				
			||||||
	case SettingTypeDate:
 | 
					 | 
				
			||||||
		return s.validateDatetime(value)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return false
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func validateSetting(db *sql.DB, settingId int, value string) (bool, error) {
 | 
					 | 
				
			||||||
	stmt := `SELECT s.Id, Description, Constrained, DataType, SettingGroup, MinValue, MaxValue, a.Id 
 | 
					 | 
				
			||||||
	FROM settings AS s LEFT JOIN allowed_setting_values AS a ON s.Id = a.SettingId AND a.ItemValue = ?
 | 
					 | 
				
			||||||
	WHERE s.Id = ?`
 | 
					 | 
				
			||||||
	result := db.QueryRow(stmt, value, settingId)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var s Setting
 | 
					 | 
				
			||||||
	var minval sql.NullString
 | 
					 | 
				
			||||||
	var maxval sql.NullString
 | 
					 | 
				
			||||||
	var allowedId sql.NullInt64
 | 
					 | 
				
			||||||
	err := result.Scan(&s.id, &s.description, &s.constrained, &s.dataType, &s.settingGroup, &minval, &maxval, &allowedId)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return false, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if s.constrained && allowedId.Valid {
 | 
					 | 
				
			||||||
		return true, nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	switch s.dataType {
 | 
					 | 
				
			||||||
	case SettingTypeInt:
 | 
					 | 
				
			||||||
		return s.validateInt(value), nil
 | 
					 | 
				
			||||||
	case SettingTypeAlphanum:
 | 
					 | 
				
			||||||
		return s.validateAlphanum(value), nil
 | 
					 | 
				
			||||||
	case SettingTypeDate:
 | 
					 | 
				
			||||||
		return s.validateDatetime(value), nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return false, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -52,7 +52,7 @@ func (m *UserModel) InitializeSettingsMap() error {
 | 
				
			|||||||
		var s Setting
 | 
							var s Setting
 | 
				
			||||||
		var mn sql.NullString
 | 
							var mn sql.NullString
 | 
				
			||||||
		var mx sql.NullString
 | 
							var mx sql.NullString
 | 
				
			||||||
		err := result.Scan(&s.id, &s.description, &s.constrained, &s.dataType, &s.dataTypeDesc, &s.settingGroup, &s.settingGroupDesc, &mn, &mx)
 | 
							err := result.Scan(&s.id, &s.description, &s.constrained, &s.dataType.id, &s.dataType.description, &s.settingGroup.id, &s.settingGroup.description, &mn, &mx)
 | 
				
			||||||
		if mn.Valid {
 | 
							if mn.Valid {
 | 
				
			||||||
			s.minValue = mn.String
 | 
								s.minValue = mn.String
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user