zitadel/internal/view/failed_events.go
Fabi 34e2f1bcdd
feat: View (#27)
* feat: eventstore repository

* fix: remove gorm

* version

* feat: pkg

* feat: eventstore without eventstore-lib

* rename files

* gnueg

* fix: add object

* fix: global model

* feat: add global view functions

* feat(eventstore): sdk

* fix(eventstore): search query

* fix(eventstore): rename app to eventstore

* delete empty test

* remove unused func

* merge master

* fix(eventstore): tests

* fix(models): delete unused struct

* feat(eventstore): implemented push events

* feat(eventstore): overwrite context data

* feat(types): SQL-config

* feat(eventstore): options to overwrite editor

* fix: use global sql config

* fix: changes from mr

* fix: add some tests

* Update internal/eventstore/models/field.go

Co-Authored-By: livio-a <livio.a@gmail.com>

* fix(eventstore): code quality

* fix: try tests

* fix: query tests

* fix: use prepare funcs

* fix: go mod

* fix: go tests

* fix: better error func testing

* fix: merge master

* fix: changes for mr

* fix: change value to interface

* fix: searchmethods

* fix: check if value is string on equal ignore case

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: livio-a <livio.a@gmail.com>
2020-04-14 18:20:20 +02:00

92 lines
2.3 KiB
Go

package view
import (
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/model"
"github.com/jinzhu/gorm"
)
const (
errViewNameKey = "view_name"
errFailedSeqKey = "failed_sequence"
)
type FailedEvent struct {
ViewName string `gorm:"column:view_name;primary_key"`
FailedSequnce uint64 `gorm:"column:failed_sequence;primary_key`
FailureCount uint64 `gorm:"column:failure_count`
ErrMsg uint64 `gorm:"column:err_msg`
}
type FailedEventSearchQuery struct {
Key FailedEventSearchKey
Method model.SearchMethod
Value interface{}
}
func (req FailedEventSearchQuery) GetKey() ColumnKey {
return failedEventSearchKey(req.Key)
}
func (req FailedEventSearchQuery) GetMethod() model.SearchMethod {
return req.Method
}
func (req FailedEventSearchQuery) GetValue() interface{} {
return req.Value
}
type FailedEventSearchKey int32
const (
FAILEDEVENTKEY_UNDEFINED FailedEventSearchKey = iota
FAILEDEVENTKEY_VIEW_NAME
FAILEDEVENTKEY_FAILED_SEQUENCE
)
type failedEventSearchKey FailedEventSearchKey
func (key failedEventSearchKey) ToColumnName() string {
switch FailedEventSearchKey(key) {
case FAILEDEVENTKEY_VIEW_NAME:
return "view_name"
case FAILEDEVENTKEY_FAILED_SEQUENCE:
return "failed_sequence"
default:
return ""
}
}
func SaveFailedEvent(db *gorm.DB, table string, failedEvent *FailedEvent) error {
save := PrepareSave(table)
err := save(db, failedEvent)
if err != nil {
return errors.ThrowInternal(err, "VIEW-5kOhP", "unable to updated failed events")
}
return nil
}
func LatestFailedEvent(db *gorm.DB, table, viewName string, sequence uint64) (*FailedEvent, error) {
failedEvent := new(FailedEvent)
queries := []SearchQuery{
FailedEventSearchQuery{Key: FAILEDEVENTKEY_VIEW_NAME, Method: model.SEARCHMETHOD_EQUALS_IGNORE_CASE, Value: viewName},
FailedEventSearchQuery{Key: FAILEDEVENTKEY_FAILED_SEQUENCE, Method: model.SEARCHMETHOD_EQUALS_IGNORE_CASE, Value: sequence},
}
query := PrepareGetByQuery(table, queries...)
err := query(db, sequence)
if err == nil {
return failedEvent, nil
}
if gorm.IsRecordNotFoundError(err) {
failedEvent.ViewName = viewName
failedEvent.FailedSequnce = sequence
failedEvent.FailureCount = 0
return failedEvent, nil
}
return nil, errors.ThrowInternalf(err, "VIEW-9LyCB", "unable to get failed events of %s", viewName)
}