Add link handling (for signup)

This commit is contained in:
Arnaud (Arhuman) ASSAD 2023-10-25 10:49:59 +02:00
parent 05df240021
commit ce822d0c16
4 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,69 @@
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
}

11
pkg/link/link_model.go Normal file
View File

@ -0,0 +1,11 @@
package link
import "time"
type Model struct {
ID int64 `json:"id" db:"id"`
Hash string `json:"hash" db:"hash"`
Operation string `json:"operation" db:"operation"`
TargetID string `json:"target_id" db:"target_id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
}

View File

@ -0,0 +1,11 @@
package link
// Repository represent the link repository contract
type Repository interface {
Store(a *Model) error
GetByHash(hash string) (*Model, error)
GetByID(id int64) (*Model, error)
Update(a *Model) error
GetAll() ([]*Model, error)
Delete(id int64) error
}

36
pkg/link/link_service.go Normal file
View File

@ -0,0 +1,36 @@
package link
type Service struct {
repo Repository
}
// NewLinkUsecase returns a service to manipulate Link
func NewLinkUsecase(r Repository) *Service {
return &Service{repo: r}
}
func (l *Service) Delete(id int64) error {
return l.repo.Delete(id)
}
func (l *Service) GetAll() ([]*Model, error) {
return l.repo.GetAll()
}
func (l *Service) GetByID(id int64) (*Model, error) {
link, err := l.repo.GetByID(id)
return link, err
}
func (l *Service) GetByHash(hash string) (*Model, error) {
return l.repo.GetByHash(hash)
}
func (l *Service) Store(link *Model) error {
return l.repo.Store(link)
}
func (l *Service) Update(link *Model) error {
return l.repo.Update(link)
}