50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package goal
|
|
|
|
import (
|
|
"rideaware/pkg/database"
|
|
)
|
|
|
|
type Repository struct{}
|
|
|
|
func NewRepository() *Repository {
|
|
return &Repository{}
|
|
}
|
|
|
|
func (r *Repository) Create(goal *Goal) error {
|
|
return database.DB.Create(goal).Error
|
|
}
|
|
|
|
func (r *Repository) GetByID(id, userID uint) (*Goal, error) {
|
|
var g Goal
|
|
err := database.DB.Where("id = ? AND user_id = ?", id, userID).First(&g).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &g, nil
|
|
}
|
|
|
|
func (r *Repository) List(userID uint) ([]Goal, error) {
|
|
var goals []Goal
|
|
err := database.DB.Where("user_id = ?", userID).
|
|
Order("created_at desc").
|
|
Find(&goals).Error
|
|
return goals, err
|
|
}
|
|
|
|
func (r *Repository) ListByStatus(userID uint, status GoalStatus) ([]Goal, error) {
|
|
var goals []Goal
|
|
err := database.DB.Where("user_id = ? AND status = ?", userID, status).
|
|
Order("created_at desc").
|
|
Find(&goals).Error
|
|
return goals, err
|
|
}
|
|
|
|
func (r *Repository) Update(goal *Goal) error {
|
|
return database.DB.Save(goal).Error
|
|
}
|
|
|
|
func (r *Repository) Delete(id, userID uint) error {
|
|
return database.DB.Where("id = ? AND user_id = ?", id, userID).Delete(&Goal{}).Error
|
|
}
|
|
|