|
- 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"
- "errors"
-
- "tea.fen.gg/fengg/server/types"
- "github.com/jmoiron/sqlx"
- _ "github.com/lib/pq"
- )
-
- const (
- StatisticQueryTmpl = `SELECT * FROM statistics %s;`
- StatisticNamedUpdateTmpl = `UPDATE statistics SET count=:count WHERE %s;`
- StatisticNamedInsertTmpl = `INSERT INTO statistics (key, count) VALUES (:key, :count);`
- )
-
- type Statistic struct {
- ID uint `db:"id" json:"id"`
- Key types.StatisticKey `db:"key" json:"key"`
- Count float64 `db:"count" json:"count"`
- }
-
- type Statistics []Statistic
-
- func NewStatistic() *Statistic {
- return &Statistic{Count: 0.0}
- }
-
- func (statistic Statistic) Valid() bool {
- return len(statistic.Key) > 0 || statistic.ID > 0
- }
-
- func (statistics *Statistics) FindAll() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- return db.Select(statistics, fmt.Sprintf(StatisticQueryTmpl, ""))
- }
-
- func (statistic *Statistic) Find() error {
- if !statistic.Valid() {
- return errors.New("we need at least a Key or ID")
- }
-
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- if statistic.ID > 0 {
- return db.Get(statistic, fmt.Sprintf(StatisticQueryTmpl, "WHERE id=$1"), statistic.ID)
- }
- return db.Get(statistic, fmt.Sprintf(StatisticQueryTmpl, "WHERE key=$1"), statistic.Key)
- }
-
- func (statistics *Statistics) Find() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- return db.Select(statistics, fmt.Sprintf(StatisticQueryTmpl, ""))
- }
-
- func (statistic *Statistic) CreateOrUpdate() error {
- if !statistic.Valid() {
- return errors.New("we need at least a Key or ID")
- }
-
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- err = db.Get(&statistic.ID,
- `SELECT id FROM statistics WHERE key=$1 or id=$2;`, statistic.Key, statistic.ID)
- if err == nil {
- _, err = db.NamedExec(fmt.Sprintf(StatisticNamedUpdateTmpl, "id=:id"), statistic)
- } else {
- _, err = db.NamedExec(StatisticNamedInsertTmpl, statistic)
- }
- return err
- }
-
- func (statistics Statistics) CreateOrUpdate() (errs []error) {
- for _, statistic := range statistics {
- err := statistic.CreateOrUpdate()
- if err != nil {
- errs = append(errs, err)
- }
- }
- return
- }
|