add delete
This commit is contained in:
@@ -11,13 +11,13 @@ import (
|
|||||||
|
|
||||||
// GetDadJoke godoc
|
// GetDadJoke godoc
|
||||||
//
|
//
|
||||||
// @Summary Generate dadjoke
|
// @Summary Generate dadjokelocaloca
|
||||||
// @Description get a dad joke
|
// @Description get a dad joke
|
||||||
// @Tags DadJoke
|
// @Tags DadJoke
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
// @Success 200 {object} model.Message "response"
|
// @Success 200 {object} model.Message "response"
|
||||||
// @failure 400 {object} model.Message "error"
|
// @failure 500 {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)
|
joke, err := service.GetRandomDadJoke(c.Db.DB)
|
||||||
@@ -36,23 +36,46 @@ func (c *Controller) GetDadJoke(ctx *gin.Context) {
|
|||||||
// @Tags DadJoke
|
// @Tags DadJoke
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
|
// @Param joke query string true "Dad Joke you wish to enter into database"
|
||||||
// @Success 200 {object} model.Message "response"
|
// @Success 200 {object} model.Message "response"
|
||||||
// @failure 400 {object} model.Message "error"
|
// @failure 500 {object} model.Message "error"
|
||||||
// @Router /jokes/dadjoke [post]
|
// @Router /jokes/dadjoke [post]
|
||||||
func (c *Controller) PostDadJoke(ctx *gin.Context) {
|
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})
|
req.JOKE = ctx.Query("joke")
|
||||||
return
|
|
||||||
}
|
|
||||||
entry := model.DadJoke{
|
|
||||||
JOKE: req.JOKE,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.Db.DB.Create(&entry).Error
|
err := service.PostDadJoke(c.Db.DB, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteDadJoke godoc
|
||||||
|
//
|
||||||
|
// @Summary Generate dadjoke
|
||||||
|
// @Description create a dad joke
|
||||||
|
// @Tags DadJoke
|
||||||
|
// @Accept json
|
||||||
|
// @Produce application/json
|
||||||
|
// @Param joke query string true "Dad joke you wish to delete from the database"
|
||||||
|
// @Success 200 {object} model.Message "response"
|
||||||
|
// @failure 500 {object} model.Message "error"
|
||||||
|
// @Router /jokes/dadjoke [delete]
|
||||||
|
func (c *Controller) DeleteDadJoke(ctx *gin.Context) {
|
||||||
|
var req model.DadJoke
|
||||||
|
|
||||||
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||||
|
req.JOKE = ctx.Query("joke")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := service.DeleteDadJoke(c.Db.DB, req)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
|
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
|
||||||
|
|||||||
51
docs/docs.go
51
docs/docs.go
@@ -66,7 +66,7 @@ const docTemplate = `{
|
|||||||
"tags": [
|
"tags": [
|
||||||
"DadJoke"
|
"DadJoke"
|
||||||
],
|
],
|
||||||
"summary": "Generate dadjoke",
|
"summary": "Generate dadjokelocaloca",
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "response",
|
"description": "response",
|
||||||
@@ -74,7 +74,7 @@ const docTemplate = `{
|
|||||||
"$ref": "#/definitions/model.Message"
|
"$ref": "#/definitions/model.Message"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"500": {
|
||||||
"description": "error",
|
"description": "error",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/model.Message"
|
"$ref": "#/definitions/model.Message"
|
||||||
@@ -94,6 +94,15 @@ const docTemplate = `{
|
|||||||
"DadJoke"
|
"DadJoke"
|
||||||
],
|
],
|
||||||
"summary": "Generate dadjoke",
|
"summary": "Generate dadjoke",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "Dad Joke you wish to enter into database",
|
||||||
|
"name": "joke",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "response",
|
"description": "response",
|
||||||
@@ -101,7 +110,43 @@ const docTemplate = `{
|
|||||||
"$ref": "#/definitions/model.Message"
|
"$ref": "#/definitions/model.Message"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"500": {
|
||||||
|
"description": "error",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/model.Message"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"description": "create a dad joke",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"DadJoke"
|
||||||
|
],
|
||||||
|
"summary": "Generate dadjoke",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "Dad joke you wish to delete from the database",
|
||||||
|
"name": "joke",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "response",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/model.Message"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
"description": "error",
|
"description": "error",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/model.Message"
|
"$ref": "#/definitions/model.Message"
|
||||||
|
|||||||
@@ -57,7 +57,7 @@
|
|||||||
"tags": [
|
"tags": [
|
||||||
"DadJoke"
|
"DadJoke"
|
||||||
],
|
],
|
||||||
"summary": "Generate dadjoke",
|
"summary": "Generate dadjokelocaloca",
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "response",
|
"description": "response",
|
||||||
@@ -65,7 +65,7 @@
|
|||||||
"$ref": "#/definitions/model.Message"
|
"$ref": "#/definitions/model.Message"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"500": {
|
||||||
"description": "error",
|
"description": "error",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/model.Message"
|
"$ref": "#/definitions/model.Message"
|
||||||
@@ -85,6 +85,15 @@
|
|||||||
"DadJoke"
|
"DadJoke"
|
||||||
],
|
],
|
||||||
"summary": "Generate dadjoke",
|
"summary": "Generate dadjoke",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "Dad Joke you wish to enter into database",
|
||||||
|
"name": "joke",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "response",
|
"description": "response",
|
||||||
@@ -92,7 +101,43 @@
|
|||||||
"$ref": "#/definitions/model.Message"
|
"$ref": "#/definitions/model.Message"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"500": {
|
||||||
|
"description": "error",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/model.Message"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"description": "create a dad joke",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"DadJoke"
|
||||||
|
],
|
||||||
|
"summary": "Generate dadjoke",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "Dad joke you wish to delete from the database",
|
||||||
|
"name": "joke",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "response",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/model.Message"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
"description": "error",
|
"description": "error",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/model.Message"
|
"$ref": "#/definitions/model.Message"
|
||||||
|
|||||||
@@ -79,6 +79,30 @@ paths:
|
|||||||
tags:
|
tags:
|
||||||
- health
|
- health
|
||||||
/jokes/dadjoke:
|
/jokes/dadjoke:
|
||||||
|
delete:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
description: create a dad joke
|
||||||
|
parameters:
|
||||||
|
- description: Dad joke you wish to delete from the database
|
||||||
|
in: query
|
||||||
|
name: joke
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: response
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/model.Message'
|
||||||
|
"500":
|
||||||
|
description: error
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/model.Message'
|
||||||
|
summary: Generate dadjoke
|
||||||
|
tags:
|
||||||
|
- DadJoke
|
||||||
get:
|
get:
|
||||||
consumes:
|
consumes:
|
||||||
- application/json
|
- application/json
|
||||||
@@ -90,17 +114,23 @@ paths:
|
|||||||
description: response
|
description: response
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/model.Message'
|
$ref: '#/definitions/model.Message'
|
||||||
"400":
|
"500":
|
||||||
description: error
|
description: error
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/model.Message'
|
$ref: '#/definitions/model.Message'
|
||||||
summary: Generate dadjoke
|
summary: Generate dadjokelocaloca
|
||||||
tags:
|
tags:
|
||||||
- DadJoke
|
- DadJoke
|
||||||
post:
|
post:
|
||||||
consumes:
|
consumes:
|
||||||
- application/json
|
- application/json
|
||||||
description: create a dad joke
|
description: create a dad joke
|
||||||
|
parameters:
|
||||||
|
- description: Dad Joke you wish to enter into database
|
||||||
|
in: query
|
||||||
|
name: joke
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
produces:
|
produces:
|
||||||
- application/json
|
- application/json
|
||||||
responses:
|
responses:
|
||||||
@@ -108,7 +138,7 @@ paths:
|
|||||||
description: response
|
description: response
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/model.Message'
|
$ref: '#/definitions/model.Message'
|
||||||
"400":
|
"500":
|
||||||
description: error
|
description: error
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/model.Message'
|
$ref: '#/definitions/model.Message'
|
||||||
|
|||||||
1
main.go
1
main.go
@@ -43,6 +43,7 @@ func main() {
|
|||||||
|
|
||||||
jokes.Use(c.AuthMiddleware([]string{"rw-jokes"}, c.Cfg.Groupsenv))
|
jokes.Use(c.AuthMiddleware([]string{"rw-jokes"}, c.Cfg.Groupsenv))
|
||||||
jokes.POST("dadjoke", c.PostDadJoke)
|
jokes.POST("dadjoke", c.PostDadJoke)
|
||||||
|
jokes.DELETE("dadjoke", c.DeleteDadJoke)
|
||||||
}
|
}
|
||||||
openai := v1.Group("/openai")
|
openai := v1.Group("/openai")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -21,6 +22,45 @@ func GetRandomDadJoke(db *gorm.DB) (string, error) {
|
|||||||
return randomElement.JOKE, err
|
return randomElement.JOKE, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PostDadJoke(db *gorm.DB, joke model.DadJoke) error {
|
||||||
|
jokes, err := getDadJokes(db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for _, i := range jokes {
|
||||||
|
if i.JOKE == joke.JOKE {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if found {
|
||||||
|
return errors.New("Joke is already in database")
|
||||||
|
} else {
|
||||||
|
err = db.Create(&joke).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteDadJoke(db *gorm.DB, joke model.DadJoke) error {
|
||||||
|
check := &model.DadJoke{}
|
||||||
|
db.Where("joke = ?", joke.JOKE).First(check)
|
||||||
|
if check.JOKE == "" {
|
||||||
|
return errors.New("Joke does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := db.Where("joke = ?", joke.JOKE).Delete(joke).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func getDadJokes(db *gorm.DB) ([]model.DadJoke, error) {
|
func getDadJokes(db *gorm.DB) ([]model.DadJoke, error) {
|
||||||
req := []model.DadJoke{}
|
req := []model.DadJoke{}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user