Initial
This commit is contained in:
101
app/repository/certs.go
Normal file
101
app/repository/certs.go
Normal file
@ -0,0 +1,101 @@
|
||||
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")
|
||||
}
|
||||
Reference in New Issue
Block a user