34 lines
761 B
Go
34 lines
761 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"maot-shortner/internal/domain"
|
|
)
|
|
|
|
func (db *Database) GetLinks(ctx context.Context) ([]*domain.Link, error) {
|
|
links := make([]*domain.Link, 0)
|
|
|
|
if err := db.WithContext(ctx).Find(&links).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return links, nil
|
|
}
|
|
|
|
func (db *Database) CreateLink(ctx context.Context, link *domain.Link) (*domain.Link, error) {
|
|
if err := db.WithContext(ctx).Create(link).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return link, nil
|
|
}
|
|
|
|
func (db *Database) GetLinkByShortCode(ctx context.Context, shortCode string) (*domain.Link, error) {
|
|
link := &domain.Link{}
|
|
|
|
if err := db.WithContext(ctx).Where("short_url = ?", shortCode).First(link).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return link, nil
|
|
}
|