Cleanup
This commit is contained in:
@ -1,89 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"repo-pattern/app/lib/helpers"
|
||||
"repo-pattern/app/models"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ApiKeysRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
type ApiKeysFilter struct {
|
||||
Key *uuid.UUID
|
||||
CompanyId *uuid.UUID
|
||||
IsActive *bool
|
||||
CompanyIsActive *bool
|
||||
}
|
||||
|
||||
func CreateApiKeysRepository(db *gorm.DB) *ApiKeysRepository {
|
||||
return &ApiKeysRepository{db}
|
||||
}
|
||||
|
||||
func applyApiKeyFilter(db *gorm.DB, query *gorm.DB, filter *ApiKeysFilter) *gorm.DB {
|
||||
if filter.Key != nil {
|
||||
query.Where("key = ?", *filter.Key)
|
||||
}
|
||||
if filter.IsActive != nil {
|
||||
now := helpers.UTCNow()
|
||||
if *filter.IsActive {
|
||||
query.Where("active_from IS NULL OR active_from <= ?", now)
|
||||
query.Where("active_to IS NULL OR active_to >= ?", now)
|
||||
} else {
|
||||
query.Where(
|
||||
"active_from IS NOT NULL AND active_from >= ?", now,
|
||||
).Or(
|
||||
"active_to IS NOT NULL AND active_to =< ?", now,
|
||||
)
|
||||
}
|
||||
}
|
||||
if filter.CompanyId != nil {
|
||||
query.Where("company_id = ?", *filter.CompanyId)
|
||||
}
|
||||
if filter.CompanyIsActive != nil {
|
||||
query.Joins("Company").Where(
|
||||
map[string]interface{}{"Company.is_active": *filter.CompanyIsActive},
|
||||
)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (r *ApiKeysRepository) New(companyId uuid.UUID) *models.ApiKey {
|
||||
now := helpers.UTCNow()
|
||||
return &models.ApiKey{
|
||||
CompanyId: companyId,
|
||||
ActiveFrom: &now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ApiKeysRepository) Get(filter *ApiKeysFilter) *models.ApiKey {
|
||||
var api_key models.ApiKey
|
||||
|
||||
query := r.db.Model(&models.ApiKey{})
|
||||
applyApiKeyFilter(r.db, query, filter)
|
||||
|
||||
result := query.First(&api_key).Joins("Company")
|
||||
if result.Error != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &api_key
|
||||
}
|
||||
|
||||
func (r *ApiKeysRepository) Save(model *models.ApiKey) {
|
||||
model.UpdatedAt = helpers.UTCNow()
|
||||
|
||||
// we don't have standard model Id, but Key as primary key.
|
||||
// this could be possible reason for save not assigning generated key uuid
|
||||
// to model.Key. but this works.
|
||||
if model.Key == uuid.Nil {
|
||||
r.db.Create(model)
|
||||
} else {
|
||||
r.db.Save(model)
|
||||
}
|
||||
}
|
||||
@ -1,101 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"repo-pattern/app/lib/helpers"
|
||||
"repo-pattern/app/models"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CertRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
type CertFilter struct {
|
||||
Alive *bool
|
||||
Id *uuid.UUID
|
||||
CompanyId *uuid.UUID
|
||||
CompanyIsActive *bool
|
||||
}
|
||||
|
||||
func CreateCertRepository(db *gorm.DB) *CertRepository {
|
||||
return &CertRepository{db}
|
||||
}
|
||||
|
||||
func applyCertFilter(db *gorm.DB, query *gorm.DB, filter *CertFilter) *gorm.DB {
|
||||
if filter.Id != nil {
|
||||
query.Where("certificates.id = ?", *filter.Id)
|
||||
}
|
||||
if filter.Alive == nil {
|
||||
query.Where("certificates.alive = TRUE")
|
||||
} else {
|
||||
query.Where("certificates.alive = ?", *filter.Alive)
|
||||
}
|
||||
if filter.CompanyId != nil {
|
||||
query.Where("certificates.company_id = ?", *filter.CompanyId)
|
||||
}
|
||||
if filter.CompanyIsActive != nil {
|
||||
query.Joins("LEFT JOIN companies on companies.is_active = TRUE")
|
||||
// query.Joins("Company").Where(
|
||||
// map[string]interface{}{"Company.is_active": *filter.CompanyIsActive},
|
||||
// )
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (r *CertRepository) New(companyId uuid.UUID) *models.Cert {
|
||||
now := helpers.UTCNow()
|
||||
return &models.Cert{
|
||||
Alive: true,
|
||||
CompanyId: companyId,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CertRepository) Get(filter *CertFilter) *models.Cert {
|
||||
var cert models.Cert
|
||||
|
||||
query := r.db.Model(&models.Cert{})
|
||||
applyCertFilter(r.db, query, filter)
|
||||
|
||||
result := query.First(&cert).Joins("Company")
|
||||
if result.Error != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &cert
|
||||
}
|
||||
|
||||
func (r *CertRepository) GetForCompany(companyId uuid.UUID) *models.Cert {
|
||||
isAlive := true
|
||||
filter := CertFilter{
|
||||
Alive: &isAlive,
|
||||
CompanyId: &companyId,
|
||||
}
|
||||
return r.Get(&filter)
|
||||
}
|
||||
|
||||
func (r *CertRepository) Save(model *models.Cert) {
|
||||
model.UpdatedAt = helpers.UTCNow()
|
||||
r.db.Save(model)
|
||||
}
|
||||
|
||||
func (r *CertRepository) Exists(filter *CertFilter) bool {
|
||||
var row models.Cert
|
||||
|
||||
query := r.db.Model(&models.Cert{})
|
||||
query = applyCertFilter(r.db, query, filter)
|
||||
result := query.Select("certificates.id").First(&row)
|
||||
|
||||
return !errors.Is(result.Error, gorm.ErrRecordNotFound) && result.Error == nil
|
||||
}
|
||||
|
||||
func (r *CertRepository) Delete(filter *CertFilter) {
|
||||
query := r.db.Model(&models.Cert{})
|
||||
applyCertFilter(r.db, query, filter)
|
||||
|
||||
query.Update("alive", false).Joins("Company")
|
||||
}
|
||||
@ -1,57 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"repo-pattern/app/lib/helpers"
|
||||
"repo-pattern/app/models"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CompanyRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
type CompanyFilter struct {
|
||||
Id *uuid.UUID
|
||||
}
|
||||
|
||||
func CreateCompanyRepository(db *gorm.DB) *CompanyRepository {
|
||||
return &CompanyRepository{db}
|
||||
}
|
||||
|
||||
func applyCompanyFilter(db *gorm.DB, query *gorm.DB, filter *CompanyFilter) *gorm.DB {
|
||||
if filter.Id != nil {
|
||||
query.Where("id = ?", *filter.Id)
|
||||
}
|
||||
query.Where("is_active = ?", true)
|
||||
return query
|
||||
}
|
||||
|
||||
func (r *CompanyRepository) New() *models.Company {
|
||||
now := helpers.UTCNow()
|
||||
return &models.Company{
|
||||
IsActive: true,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CompanyRepository) Get(filter *CompanyFilter) *models.Company {
|
||||
var company models.Company
|
||||
|
||||
query := r.db.Model(&models.Company{})
|
||||
applyCompanyFilter(r.db, query, filter)
|
||||
|
||||
result := query.First(&company)
|
||||
if result.Error != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &company
|
||||
}
|
||||
|
||||
func (r *CompanyRepository) Save(model *models.Company) {
|
||||
model.UpdatedAt = helpers.UTCNow()
|
||||
r.db.Save(model)
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package repository
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type DAO struct {
|
||||
db *gorm.DB
|
||||
ApiKeysRepository *ApiKeysRepository
|
||||
CertRepository *CertRepository
|
||||
CompanyRepository *CompanyRepository
|
||||
FiskLogRepository *FiskLogRepository
|
||||
}
|
||||
|
||||
var Dao DAO
|
||||
|
||||
func CreateDAO(db *gorm.DB) DAO {
|
||||
return DAO{
|
||||
db: db,
|
||||
ApiKeysRepository: CreateApiKeysRepository(db),
|
||||
CertRepository: CreateCertRepository(db),
|
||||
CompanyRepository: CreateCompanyRepository(db),
|
||||
FiskLogRepository: CreateFiskLogRepository(db),
|
||||
}
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"repo-pattern/app/lib/helpers"
|
||||
"repo-pattern/app/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FiskLogRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func CreateFiskLogRepository(db *gorm.DB) *FiskLogRepository {
|
||||
return &FiskLogRepository{db}
|
||||
}
|
||||
|
||||
func (r *FiskLogRepository) New(model models.FiskLogItem) *models.FiskLogItem {
|
||||
now := helpers.UTCNow()
|
||||
model.CreatedAt = now
|
||||
model.UpdatedAt = now
|
||||
return &model
|
||||
}
|
||||
|
||||
func (r *FiskLogRepository) Save(model *models.FiskLogItem) error {
|
||||
model.UpdatedAt = helpers.UTCNow()
|
||||
result := r.db.Save(model)
|
||||
return result.Error
|
||||
}
|
||||
35
app/repository/method_exists.go
Normal file
35
app/repository/method_exists.go
Normal file
@ -0,0 +1,35 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"repo-pattern/app/repository/smartfilter"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type ExistsMethod[T schema.Tabler] struct {
|
||||
DbConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *ExistsMethod[T]) Init(dbConn *gorm.DB) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
|
||||
var (
|
||||
model T
|
||||
)
|
||||
|
||||
query := m.DbConn.Model(model)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, query)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result := query.Select("*").First(&model)
|
||||
|
||||
exists := !errors.Is(result.Error, gorm.ErrRecordNotFound) && result.Error == nil
|
||||
return exists, nil
|
||||
}
|
||||
34
app/repository/method_get.go
Normal file
34
app/repository/method_get.go
Normal file
@ -0,0 +1,34 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"repo-pattern/app/repository/smartfilter"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type GetMethod[T schema.Tabler] struct {
|
||||
DbConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *GetMethod[T]) Init(dbConn *gorm.DB) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m GetMethod[T]) Get(filter interface{}) (*T, error) {
|
||||
var (
|
||||
model T
|
||||
)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := query.First(&model)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
return &model, nil
|
||||
}
|
||||
31
app/repository/method_list.go
Normal file
31
app/repository/method_list.go
Normal file
@ -0,0 +1,31 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"repo-pattern/app/repository/smartfilter"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type ListMethod[T schema.Tabler] struct {
|
||||
DbConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *ListMethod[T]) Init(dbConn *gorm.DB) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m ListMethod[T]) List(filter interface{}) (*[]T, error) {
|
||||
var (
|
||||
model T
|
||||
models []T
|
||||
)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query.Find(&models)
|
||||
return &models, nil
|
||||
}
|
||||
@ -1,84 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"repo-pattern/app/repository/smartfilter"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type ListMethod[T schema.Tabler] struct {
|
||||
DbConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *ListMethod[T]) Init(dbConn *gorm.DB) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m ListMethod[T]) List(filter interface{}) (*[]T, error) {
|
||||
var (
|
||||
model T
|
||||
models []T
|
||||
)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query.Find(&models)
|
||||
return &models, nil
|
||||
}
|
||||
|
||||
type GetMethod[T schema.Tabler] struct {
|
||||
DbConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *GetMethod[T]) Init(dbConn *gorm.DB) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m GetMethod[T]) Get(filter interface{}) (*T, error) {
|
||||
var (
|
||||
model T
|
||||
)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := query.First(&model)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
return &model, nil
|
||||
}
|
||||
|
||||
type ExistsMethod[T schema.Tabler] struct {
|
||||
DbConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *ExistsMethod[T]) Init(dbConn *gorm.DB) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
|
||||
var (
|
||||
model T
|
||||
)
|
||||
|
||||
query := m.DbConn.Model(model)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, query)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result := query.Select("*").First(&model)
|
||||
|
||||
exists := !errors.Is(result.Error, gorm.ErrRecordNotFound) && result.Error == nil
|
||||
return exists, nil
|
||||
}
|
||||
Reference in New Issue
Block a user