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