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

60 lines
1.5 KiB
Go

package repository
import (
"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(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(note)
}
// GetNote retrieves a note by its ID.
func (r *NoteRepository) GetNote(id int) (models.Note, error) {
return r.store.GetNoteByID(id)
}
// ListNotes retrieves all notes.
func (r *NoteRepository) ListNotes() ([]models.Note, error) {
return r.store.GetAllNotes()
}
// UpdateNote updates the content of an existing note.
func (r *NoteRepository) UpdateNote(id int, content string) (models.Note, error) {
if content == "" {
return models.Note{}, fmt.Errorf("content cannot be empty")
}
note, err := r.store.GetNoteByID(id)
if err != nil {
return models.Note{}, err
}
note.Content = content
note.LastUpdate = time.Now()
return r.store.SaveNote(note)
}
// DeleteNote deletes a note by its ID.
func (r *NoteRepository) DeleteNote(id int) error {
return r.store.DeleteNoteByID(id)
}