Files
gotes/internal/repository/notes.go
2026-03-17 21:59:40 +00:00

61 lines
1.6 KiB
Go

package repository
import (
"context"
"fmt"
"time"
"git.hrafn.xyz/aether/notes/internal/models"
)
// NoteRepository manages operations on notes using a data store.
type NoteRepository struct {
store NoteStore
}
// NewNoteRepository creates and returns a new NoteRepository instance.
func NewNoteRepository(store NoteStore) *NoteRepository {
return &NoteRepository{store: store}
}
// CreateNote creates a new note with the given content.
func (r *NoteRepository) CreateNote(ctx context.Context, content string) (models.Note, error) {
if content == "" {
return models.Note{}, fmt.Errorf("content cannot be empty")
}
note := models.Note{
Content: content,
LastUpdate: time.Now(),
}
return r.store.SaveNote(ctx, note)
}
// GetNote retrieves a note by its ID.
func (r *NoteRepository) GetNote(ctx context.Context, id int) (models.Note, error) {
return r.store.GetNoteByID(ctx, id)
}
// ListNotes retrieves all notes.
func (r *NoteRepository) ListNotes(ctx context.Context) ([]models.Note, error) {
return r.store.GetAllNotes(ctx)
}
// UpdateNote updates the content of an existing note.
func (r *NoteRepository) UpdateNote(ctx context.Context, id int, content string) (models.Note, error) {
if content == "" {
return models.Note{}, fmt.Errorf("content cannot be empty")
}
note, err := r.store.GetNoteByID(ctx, id)
if err != nil {
return models.Note{}, err
}
note.Content = content
note.LastUpdate = time.Now()
return r.store.SaveNote(ctx, note)
}
// DeleteNote deletes a note by its ID.
func (r *NoteRepository) DeleteNote(ctx context.Context, id int) error {
return r.store.DeleteNoteByID(ctx, id)
}