A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

139 lines
3.5 KiB

  1. package model
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "fmt"
  20. "time"
  21. "encoding/json"
  22. "tea.fen.gg/fengg/server/types"
  23. "github.com/jmoiron/sqlx"
  24. _ "github.com/lib/pq"
  25. )
  26. const (
  27. ConfigurationNamedInsertTmpl = `
  28. INSERT INTO configuration (
  29. created_at, updated_at, key, value
  30. ) VALUES (
  31. now(), now(), :key, :value
  32. ) RETURNING id;`
  33. ConfigurationNamedUpdateTmpl = `
  34. UPDATE configuration
  35. SET updated_at=now(), value=:value
  36. WHERE key=:key OR id=:id;`
  37. ConfigurationQueryTmpl = `SELECT * FROM configuration %s;`
  38. )
  39. type Configuration struct {
  40. ID uint `db:"id" json:"id"`
  41. CreatedAt time.Time `db:"created_at" json:"createdAt"`
  42. UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
  43. Key types.ConfigurationKey `db:"key" json:"key"`
  44. Value types.JsonString `db:"value" json:"value"`
  45. }
  46. func NewConfiguration(key types.ConfigurationKey) *Configuration {
  47. return &Configuration{
  48. CreatedAt: time.Now(),
  49. UpdatedAt: time.Now(),
  50. Key: key,
  51. }
  52. }
  53. func (configuration *Configuration) Map() (jsonMap map[string]interface{}) {
  54. jsonMap = make(map[string]interface{})
  55. _ = json.Unmarshal([]byte(configuration.Value), &jsonMap)
  56. return
  57. }
  58. func (configuration *Configuration) Slice() (jsonSlice []string) {
  59. _ = json.Unmarshal([]byte(configuration.Value), &jsonSlice)
  60. return
  61. }
  62. // Exists directly updates the struct with the findings
  63. func (configuration *Configuration) Exists() bool {
  64. db, err := sqlx.Connect(dbDriver, dbConnect)
  65. if err != nil {
  66. logger.Error().Err(err).Msg("cannot open database")
  67. return false
  68. }
  69. defer db.Close()
  70. err = db.Get(configuration,
  71. fmt.Sprintf(ConfigurationQueryTmpl, "WHERE key=$1 OR id=$2",),
  72. configuration.Key, configuration.ID)
  73. if err != nil {
  74. logger.Debug().Err(err).Msg("cannot find configuration key or id")
  75. }
  76. return err == nil
  77. }
  78. func (configuration *Configuration) Update() error {
  79. db, err := sqlx.Connect(dbDriver, dbConnect)
  80. if err != nil {
  81. return err
  82. }
  83. defer db.Close()
  84. _, err = db.NamedExec(ConfigurationNamedUpdateTmpl, configuration)
  85. return err
  86. }
  87. func (configuration *Configuration) Marshal(data interface{}) error {
  88. result, err := json.Marshal(data)
  89. if err != nil {
  90. return err
  91. }
  92. configuration.Value = types.JsonString(string(result))
  93. return nil
  94. }
  95. func (configuration *Configuration) MarshalAndCreate(data interface{}) error {
  96. err := configuration.Marshal(data)
  97. if err != nil {
  98. return err
  99. }
  100. return configuration.Create()
  101. }
  102. func (configuration *Configuration) Create() error {
  103. db, err := sqlx.Connect(dbDriver, dbConnect)
  104. if err != nil {
  105. return err
  106. }
  107. defer db.Close()
  108. rows, err := db.NamedQuery(ConfigurationNamedInsertTmpl, configuration)
  109. if err != nil {
  110. return err
  111. }
  112. defer rows.Close()
  113. for rows.Next() {
  114. err = rows.Scan(&configuration.ID)
  115. if err != nil {
  116. return err
  117. }
  118. }
  119. return nil
  120. }