kit/pkg/link/link_gorm_repository.go

70 lines
1.3 KiB
Go

package link
import (
"github.com/jinzhu/gorm"
)
type gormLinkRepository struct {
db *gorm.DB
}
// NewGormLinkRepository will create an object that represent the link.Repository interface
func NewGormLinkRepository(db *gorm.DB) Repository {
return &gormLinkRepository{db}
}
func (repo *gormLinkRepository) Delete(id int64) error {
l, err := repo.GetByID(id)
if err != nil {
return err
}
if err := repo.db.Delete(l).Error; err != nil {
return err
}
return nil
}
func (repo *gormLinkRepository) GetAll() ([]*Model, error) {
var ls []*Model
if err := repo.db.Find(&ls).Error; err != nil {
return nil, err
}
return ls, nil
}
func (repo *gormLinkRepository) GetByID(id int64) (*Model, error) {
l := new(Model)
l.ID = id
if err := repo.db.First(&l).Error; err != nil {
return nil, err
}
return l, nil
}
func (repo *gormLinkRepository) GetByHash(hash string) (*Model, error) {
var l Model
err := repo.db.Where("hash = ?", hash).First(&l).Error
if err != nil {
return nil, err
}
return &l, nil
}
func (repo *gormLinkRepository) Store(l *Model) error {
if err := repo.db.Create(&l).Error; err != nil {
return err
}
return nil
}
func (repo *gormLinkRepository) Update(l *Model) error {
if err := repo.db.Save(&l).Error; err != nil {
return err
}
return nil
}