updates
This commit is contained in:
57
server/internal/repository/supplement_repository.go
Normal file
57
server/internal/repository/supplement_repository.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go-server/internal/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SupplementRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewSupplementRepository(db *gorm.DB) *SupplementRepository {
|
||||
return &SupplementRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *SupplementRepository) GetAll(ctx context.Context) ([]*models.Supplement, error) {
|
||||
var supplements []*models.Supplement
|
||||
err := r.db.WithContext(ctx).
|
||||
Preload("Nutrients").
|
||||
Preload("SupplementNutrients").
|
||||
Find(&supplements).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return supplements, nil
|
||||
}
|
||||
|
||||
func (r *SupplementRepository) GetById(ctx context.Context, id string) (*models.Supplement, error) {
|
||||
var supplement *models.Supplement
|
||||
err := r.db.WithContext(ctx).
|
||||
Preload("Nutrients").
|
||||
Preload("SupplementNutrients").
|
||||
First(&supplement, "id = ?", id).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return supplement, nil
|
||||
}
|
||||
|
||||
func (r *SupplementRepository) GetDailySupplementsOverview(ctx context.Context) ([]*models.SupplementNutrientOverview, error) {
|
||||
var supplementNutrientOverview []*models.SupplementNutrientOverview
|
||||
err := r.db.WithContext(ctx).
|
||||
Table("supplements").
|
||||
Select("supplements.id as supplement_id, supplements.name as supplement_name, supplements.description as supplement_description, supplement_nutrients.serving_size as serving_size, supplement_nutrients.per_serving as per_serving, supplement_nutrients.per_serving_reference_intake as per_serving_reference_intake, nutrients.id as nutrient_id, nutrients.name as nutrient_name, nutrients.description as nutrient_description").
|
||||
Joins("INNER JOIN supplement_nutrients ON supplements.id = supplement_nutrients.supplement_id").
|
||||
Joins("JOIN nutrients ON supplement_nutrients.nutrient_id = nutrients.id").
|
||||
Find(&supplementNutrientOverview).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return supplementNutrientOverview, nil
|
||||
}
|
||||
Reference in New Issue
Block a user