|
- package model
- //
- // Fengg Security Gateway Server Application
- // Copyright (C) 2020 Lukas Matt <support@fen.gg>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- //
- import (
- "fmt"
- "time"
-
- "tea.fen.gg/fengg/server/types"
- "github.com/jmoiron/sqlx"
- _ "github.com/lib/pq"
- )
-
- const (
- PortalEventQueryTmpl = `SELECT * FROM portal_events %s;`
- PortalEventNamedUpdateTmpl = `
- UPDATE portal_events
- SET updated_at=now(), ack=:ack
- %s;`
- PortalEventNamedInsertTmpl = `
- INSERT INTO portal_events (
- pid, created_at, updated_at, ack, type, message, message_id, uuid_id, client_id
- ) VALUES (
- :pid, :created_at, :updated_at, :ack, :type, :message, :message_id, :uuid_id, :client_id
- ) RETURNING id;`
- PortalEventDeleteTmpl = `DELETE FROM portal_events %s;`
- )
-
- type PortalEvent struct {
- ID uint `db:"id" json:"id" binding:"gt=0"`
- PID uint `db:"pid" json:"pid"`
- CreatedAt time.Time `db:"created_at" json:"createdAt" binding:"-"`
- UpdatedAt time.Time `db:"updated_at" json:"updatedAt" binding:"-"`
-
- Ack *bool `db:"ack" json:"ack,omitempty"`
- Type types.PortalEventType `db:"type" json:"type" binding:"gt=0"`
- Message *types.JsonString `db:"message" json:"message,omitempty"`
- MessageID uint `db:"message_id" json:"messageId" binding:"gt=0"`
- UuidID uint `db:"uuid_id" json:"uuidId" binding:"gt=0"`
- ClientID uint `db:"client_id" json:"clientId,omitempty"`
- }
-
- type PortalEvents []PortalEvent
-
- func (events *PortalEvents) FindAll() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- logger.Error().Err(err).Msg("cannot connect to database")
- return err
- }
- defer db.Close()
-
- return db.Select(events, fmt.Sprintf(PortalEventQueryTmpl, ""))
- }
-
- func NewPortalEvent() *PortalEvent {
- return &PortalEvent{
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- }
- }
-
- func (event *PortalEvent) Exists(pid uint) bool {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- logger.Error().Err(err).Msg("cannot connect to database")
- return false
- }
- defer db.Close()
-
- var dbEvent PortalEvent
- err = db.Get(&dbEvent, fmt.Sprintf(PortalEventQueryTmpl, "WHERE pid = $1"), pid)
- if err == nil {
- event.ID = dbEvent.ID
- event.PID = dbEvent.PID
- }
- return err == nil
- }
-
- func (event *PortalEvent) Update() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- _, err = db.NamedExec(fmt.Sprintf(PortalEventNamedUpdateTmpl, "WHERE pid=:pid"), event)
- return err
- }
-
- func (event *PortalEvent) Create() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- rows, err := db.NamedQuery(PortalEventNamedInsertTmpl, event)
- if err != nil {
- return err
- }
- defer rows.Close()
-
- for rows.Next() {
- err = rows.Scan(&event.ID)
- if err != nil {
- return err
- }
- }
- return nil
- }
|