53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package persistence
|
|
|
|
import (
|
|
domainInvite "RenewCMS/internal/domain/invite"
|
|
"RenewCMS/internal/infrastructure/persistence/mappers"
|
|
entity "RenewCMS/internal/infrastructure/persistence/models"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type InviteRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewInviteRepository(db *gorm.DB) *InviteRepository {
|
|
return &InviteRepository{db}
|
|
}
|
|
|
|
func (i *InviteRepository) Create(invite domainInvite.Invite) (domainInvite.Invite, error) {
|
|
model := mappers.InviteToModel(invite)
|
|
model.ID = 0
|
|
if err := i.db.Create(&model).Error; err != nil {
|
|
return domainInvite.Invite{}, err
|
|
}
|
|
|
|
return mappers.InviteToDomain(model), nil
|
|
}
|
|
|
|
func (i *InviteRepository) GetAll() []domainInvite.Invite {
|
|
var invites []entity.Invite
|
|
i.db.Model(&entity.Invite{}).Find(&invites)
|
|
|
|
var domainInvites []domainInvite.Invite
|
|
for _, localInvite := range invites {
|
|
domainInvites = append(domainInvites, mappers.InviteToDomain(localInvite))
|
|
}
|
|
|
|
return domainInvites
|
|
}
|
|
|
|
func (i *InviteRepository) Delete(id uint32) error {
|
|
result := i.db.Delete(&entity.Invite{}, id)
|
|
return deleteOrNotFound(result, domainInvite.ErrInviteNotFound)
|
|
}
|
|
|
|
func (i *InviteRepository) MarkAccepted(id uint32, acceptedAt time.Time) error {
|
|
result := i.db.Model(&entity.Invite{}).Where("id = ?", id).Update("accepted_at", acceptedAt)
|
|
return deleteOrNotFound(result, domainInvite.ErrInviteNotFound)
|
|
}
|
|
|
|
var _ domainInvite.Repository = &InviteRepository{}
|