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.
 
 
 
 
 
 

125 lines
3.4 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. "tea.fen.gg/fengg/server/types"
  22. "github.com/jmoiron/sqlx"
  23. _ "github.com/lib/pq"
  24. )
  25. const (
  26. PortalEventQueryTmpl = `SELECT * FROM portal_events %s;`
  27. PortalEventNamedUpdateTmpl = `
  28. UPDATE portal_events
  29. SET updated_at=now(), ack=:ack
  30. %s;`
  31. PortalEventNamedInsertTmpl = `
  32. INSERT INTO portal_events (
  33. pid, created_at, updated_at, ack, type, message, message_id, uuid_id, client_id
  34. ) VALUES (
  35. :pid, :created_at, :updated_at, :ack, :type, :message, :message_id, :uuid_id, :client_id
  36. ) RETURNING id;`
  37. PortalEventDeleteTmpl = `DELETE FROM portal_events %s;`
  38. )
  39. type PortalEvent struct {
  40. ID uint `db:"id" json:"id" binding:"gt=0"`
  41. PID uint `db:"pid" json:"pid"`
  42. CreatedAt time.Time `db:"created_at" json:"createdAt" binding:"-"`
  43. UpdatedAt time.Time `db:"updated_at" json:"updatedAt" binding:"-"`
  44. Ack *bool `db:"ack" json:"ack,omitempty"`
  45. Type types.PortalEventType `db:"type" json:"type" binding:"gt=0"`
  46. Message *types.JsonString `db:"message" json:"message,omitempty"`
  47. MessageID uint `db:"message_id" json:"messageId" binding:"gt=0"`
  48. UuidID uint `db:"uuid_id" json:"uuidId" binding:"gt=0"`
  49. ClientID uint `db:"client_id" json:"clientId,omitempty"`
  50. }
  51. type PortalEvents []PortalEvent
  52. func (events *PortalEvents) FindAll() error {
  53. db, err := sqlx.Connect(dbDriver, dbConnect)
  54. if err != nil {
  55. logger.Error().Err(err).Msg("cannot connect to database")
  56. return err
  57. }
  58. defer db.Close()
  59. return db.Select(events, fmt.Sprintf(PortalEventQueryTmpl, ""))
  60. }
  61. func NewPortalEvent() *PortalEvent {
  62. return &PortalEvent{
  63. CreatedAt: time.Now(),
  64. UpdatedAt: time.Now(),
  65. }
  66. }
  67. func (event *PortalEvent) Exists(pid uint) bool {
  68. db, err := sqlx.Connect(dbDriver, dbConnect)
  69. if err != nil {
  70. logger.Error().Err(err).Msg("cannot connect to database")
  71. return false
  72. }
  73. defer db.Close()
  74. var dbEvent PortalEvent
  75. err = db.Get(&dbEvent, fmt.Sprintf(PortalEventQueryTmpl, "WHERE pid = $1"), pid)
  76. if err == nil {
  77. event.ID = dbEvent.ID
  78. event.PID = dbEvent.PID
  79. }
  80. return err == nil
  81. }
  82. func (event *PortalEvent) Update() error {
  83. db, err := sqlx.Connect(dbDriver, dbConnect)
  84. if err != nil {
  85. return err
  86. }
  87. defer db.Close()
  88. _, err = db.NamedExec(fmt.Sprintf(PortalEventNamedUpdateTmpl, "WHERE pid=:pid"), event)
  89. return err
  90. }
  91. func (event *PortalEvent) Create() error {
  92. db, err := sqlx.Connect(dbDriver, dbConnect)
  93. if err != nil {
  94. return err
  95. }
  96. defer db.Close()
  97. rows, err := db.NamedQuery(PortalEventNamedInsertTmpl, event)
  98. if err != nil {
  99. return err
  100. }
  101. defer rows.Close()
  102. for rows.Next() {
  103. err = rows.Scan(&event.ID)
  104. if err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }