add dad jokes
This commit is contained in:
@@ -11,11 +11,13 @@ import (
|
|||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
|
|
||||||
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
||||||
|
"gitlab.com/DeveloperDurp/DurpAPI/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
Cfg model.Config
|
Cfg model.Config
|
||||||
dbcfg model.DBConfig
|
Dbcfg model.DBConfig
|
||||||
|
Db model.Repository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewController() *Controller {
|
func NewController() *Controller {
|
||||||
@@ -23,19 +25,30 @@ func NewController() *Controller {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to load file: %e", err)
|
log.Fatalf("unable to load file: %e", err)
|
||||||
}
|
}
|
||||||
controller := &Controller{}
|
|
||||||
controller.Cfg = model.Config{}
|
controller := &Controller{
|
||||||
controller.dbcfg = model.DBConfig{}
|
Cfg: model.Config{},
|
||||||
|
Dbcfg: model.DBConfig{},
|
||||||
|
}
|
||||||
|
|
||||||
err = env.Parse(&controller.Cfg)
|
err = env.Parse(&controller.Cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to parse environment variables: %e", err)
|
log.Fatalf("unable to parse environment variables: %e", err)
|
||||||
}
|
}
|
||||||
err = env.Parse(&controller.dbcfg)
|
err = env.Parse(&controller.Dbcfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to parse database variables: %e", err)
|
log.Fatalf("unable to parse database variables: %e", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
controller.Cfg.OpenaiClient = *openai.NewClient(controller.Cfg.OpenaiApiKey)
|
controller.Cfg.OpenaiClient = *openai.NewClient(controller.Cfg.OpenaiApiKey)
|
||||||
|
|
||||||
|
Db, err := storage.Connect(controller.Dbcfg)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("Failed to connect to database")
|
||||||
|
}
|
||||||
|
controller.Db = *Db
|
||||||
|
|
||||||
return controller
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
||||||
|
"gitlab.com/DeveloperDurp/DurpAPI/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetDadJoke godoc
|
// GetDadJoke godoc
|
||||||
@@ -19,15 +20,40 @@ import (
|
|||||||
// @failure 400 {object} model.Message "error"
|
// @failure 400 {object} model.Message "error"
|
||||||
// @Router /jokes/dadjoke [get]
|
// @Router /jokes/dadjoke [get]
|
||||||
func (c *Controller) GetDadJoke(ctx *gin.Context) {
|
func (c *Controller) GetDadJoke(ctx *gin.Context) {
|
||||||
|
joke, err := service.GetRandomDadJoke(c.Db.DB)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{"message": joke})
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostDadJoke godoc
|
||||||
|
//
|
||||||
|
// @Summary Generate dadjoke
|
||||||
|
// @Description create a dad joke
|
||||||
|
// @Tags DadJoke
|
||||||
|
// @Accept json
|
||||||
|
// @Produce application/json
|
||||||
|
// @Success 200 {object} model.Message "response"
|
||||||
|
// @failure 400 {object} model.Message "error"
|
||||||
|
// @Router /jokes/dadjoke [post]
|
||||||
|
func (c *Controller) PostDadJoke(ctx *gin.Context) {
|
||||||
var req model.DadJoke
|
var req model.DadJoke
|
||||||
|
|
||||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
message := "test"
|
entry := model.DadJoke{
|
||||||
// message, err := service.GetDadJoke(req)
|
JOKE: req.JOKE,
|
||||||
// if err != nil {
|
}
|
||||||
// ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
|
|
||||||
// }
|
err := c.Db.DB.Create(&entry).Error
|
||||||
ctx.JSON(http.StatusOK, gin.H{"message": message})
|
if err != nil {
|
||||||
|
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
|
||||||
}
|
}
|
||||||
|
|||||||
27
docs/docs.go
27
docs/docs.go
@@ -81,6 +81,33 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"post": {
|
||||||
|
"description": "create a dad joke",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"DadJoke"
|
||||||
|
],
|
||||||
|
"summary": "Generate dadjoke",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "response",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/model.Message"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"400": {
|
||||||
|
"description": "error",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/model.Message"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/openai/general": {
|
"/openai/general": {
|
||||||
|
|||||||
@@ -72,6 +72,33 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"post": {
|
||||||
|
"description": "create a dad joke",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"DadJoke"
|
||||||
|
],
|
||||||
|
"summary": "Generate dadjoke",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "response",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/model.Message"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"400": {
|
||||||
|
"description": "error",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/model.Message"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/openai/general": {
|
"/openai/general": {
|
||||||
|
|||||||
@@ -97,6 +97,24 @@ paths:
|
|||||||
summary: Generate dadjoke
|
summary: Generate dadjoke
|
||||||
tags:
|
tags:
|
||||||
- DadJoke
|
- DadJoke
|
||||||
|
post:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
description: create a dad joke
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: response
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/model.Message'
|
||||||
|
"400":
|
||||||
|
description: error
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/model.Message'
|
||||||
|
summary: Generate dadjoke
|
||||||
|
tags:
|
||||||
|
- DadJoke
|
||||||
/openai/general:
|
/openai/general:
|
||||||
get:
|
get:
|
||||||
consumes:
|
consumes:
|
||||||
|
|||||||
3
main.go
3
main.go
@@ -40,6 +40,9 @@ func main() {
|
|||||||
jokes := v1.Group("/jokes")
|
jokes := v1.Group("/jokes")
|
||||||
{
|
{
|
||||||
jokes.GET("dadjoke", c.GetDadJoke)
|
jokes.GET("dadjoke", c.GetDadJoke)
|
||||||
|
|
||||||
|
jokes.Use(c.AuthMiddleware([]string{"rw-jokes"}, c.Cfg.Groupsenv))
|
||||||
|
jokes.POST("dadjoke", c.PostDadJoke)
|
||||||
}
|
}
|
||||||
openai := v1.Group("/openai")
|
openai := v1.Group("/openai")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
type DadJokes struct {
|
|
||||||
ID uint `gorm: "primary key;autoIncrement" json:"id"`
|
|
||||||
Author *string `json:"author"`
|
|
||||||
Title *string `json:"title"`
|
|
||||||
Publisher *string `json:"publisher"`
|
|
||||||
}
|
|
||||||
type DadJoke struct {
|
|
||||||
Author string `json:"author"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Publisher string `json:"publisher"`
|
|
||||||
}
|
|
||||||
5
model/jokes.go
Normal file
5
model/jokes.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type DadJoke struct {
|
||||||
|
JOKE string `json:"joke"`
|
||||||
|
}
|
||||||
30
service/dadjoke.go
Normal file
30
service/dadjoke.go
Normal 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
|
||||||
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func RetryOperation(maxRetries int, delay time.Duration, operation func() error) error {
|
|
||||||
var err error
|
|
||||||
for i := 0; i <= maxRetries; i++ {
|
|
||||||
err = operation()
|
|
||||||
if err == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
fmt.Printf("Error encountered: %v\n", err)
|
|
||||||
if i < maxRetries {
|
|
||||||
fmt.Printf("Retrying after %v...\n", delay)
|
|
||||||
time.Sleep(delay)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
33
storage/postgres.go
Normal file
33
storage/postgres.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gorm.io/driver/postgres"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Connect(config model.DBConfig) (*model.Repository, error) {
|
||||||
|
dsn := fmt.Sprintf(
|
||||||
|
"host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
|
||||||
|
config.Host, config.Port, config.User, config.Password, config.DBName, config.SSLMode,
|
||||||
|
)
|
||||||
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
runMigrations(db)
|
||||||
|
return &model.Repository{
|
||||||
|
DB: db,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runMigrations(db *gorm.DB) error {
|
||||||
|
err := db.AutoMigrate(&model.DadJoke{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user