add dad jokes

This commit is contained in:
2023-06-24 10:34:49 -04:00
parent b01b26b12d
commit 31a71fac7f
11 changed files with 193 additions and 46 deletions

30
service/dadjoke.go Normal file
View File

@@ -0,0 +1,30 @@
package service
import (
"math/rand"
"gorm.io/gorm"
"gitlab.com/DeveloperDurp/DurpAPI/model"
)
func GetRandomDadJoke(db *gorm.DB) (string, error) {
jokes, err := getDadJokes(db)
if err != nil {
return "", err
}
randomIndex := rand.Intn(len(jokes))
randomElement := jokes[randomIndex]
return randomElement.JOKE, err
}
func getDadJokes(db *gorm.DB) ([]model.DadJoke, error) {
req := []model.DadJoke{}
err := db.Find(&req).Error
return req, err
}