Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type NFContext struct {
TaskMutex sync.RWMutex
NextTaskID uint64

Messages []Message
Messages []Message
DragonBallData map[string]int32
}

type Message struct {
Expand Down Expand Up @@ -86,6 +87,16 @@ func InitNfContext() {
nfContext.NextTaskID = 0

nfContext.Messages = make([]Message, 0)

nfContext.DragonBallData = map[string]int32{
"Goku": 7,
"Vegeta": 6,
"Gohan": 5,
"Trunks": 4,
"Piccolo": 3,
"Krillin": 2,
"Yamcha": 1,
}
}

func GetSelf() *NFContext {
Expand Down
141 changes: 141 additions & 0 deletions internal/sbi/api_dragonball.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package sbi

import (
"net/http"

"github.com/Alonza0314/nf-example/internal/logger"
"github.com/gin-gonic/gin"
)

func (s *Server) getDragonBallRoute() []Route {
return []Route{
{
Name: "Hello Dragon Ball!",
Method: http.MethodGet,
Pattern: "/",
APIFunc: func(c *gin.Context) {
c.JSON(http.StatusOK, "Hello Dragon Ball!")
},
// Use
// curl -X GET http://127.0.0.163:8000/dragonball/
},
{
Name: "Dragon Ball Search Character",
Method: http.MethodGet,
Pattern: "/character/:name",
APIFunc: s.HTTPSearchDragonBallCharacter,
// Use
// curl -X GET http://127.0.0.163:8000/dragonball/character/Goku
},
{
Name: "Dragon Ball Fight",
Method: http.MethodPost,
Pattern: "/battle",
APIFunc: s.HTTPDragonBallFight,
// Use
// curl -X POST "http://127.0.0.163:8000/dragonball/battle" -d '{"name1": "Goku", "name2": "Vegeta"}'
},
{
Name: "Add Dragon Ball Character",
Method: http.MethodPost,
Pattern: "/character",
APIFunc: s.HTTPAddDragonBallCharacter,
// Use
// curl -X POST "http://127.0.0.163:8000/dragonball/character" -d '{"Name": "Saitama", "Powerlevel": 10000}'
},
{
Name: "Update Dragon Ball Character's Powerlevel",
Method: http.MethodPut,
Pattern: "/character/:name",
APIFunc: s.HTTPUpdateDragonBallCharacter,
// Use
// curl -X PUT "http://127.0.0.163:8000/dragonball/character/Goku" -d '{"Powerlevel": 500}'
},
}
}

func (s *Server) HTTPSearchDragonBallCharacter(c *gin.Context) {
logger.SBILog.Infof("In HTTPSearchDragonBallCharacter")
targetName := c.Param("name")

if targetName == "" {
c.String(http.StatusBadRequest, "No name provided")
return
}

s.Processor().SearchDragonBallCharacter(c, targetName)
}

func (s *Server) HTTPDragonBallFight(c *gin.Context) {
logger.SBILog.Infof("In HTTPDragonBallFight")

type RequestBody struct {
TargetName1 string `json:"name1"`
TargetName2 string `json:"name2"`
}
var requestbody RequestBody
if err := c.ShouldBindBodyWithJSON(&requestbody); err != nil {
c.String(http.StatusBadRequest, "error")
return
}
if requestbody.TargetName1 == "" {
c.String(http.StatusBadRequest, "No name1 provided")
return
}
if requestbody.TargetName2 == "" {
c.String(http.StatusBadRequest, "No name2 provided")
return
}

s.Processor().FightDragonBall(c, requestbody.TargetName1, requestbody.TargetName2)
}

func (s *Server) HTTPAddDragonBallCharacter(c *gin.Context) {
logger.SBILog.Infof("In HTTPAddDragonBallCharacter")

type RequestBody struct {
Name string `json:"name"`
PowerLevel *int32 `json:"powerLevel"`
}
var requestbody RequestBody
if err := c.ShouldBindBodyWithJSON(&requestbody); err != nil {
c.String(http.StatusBadRequest, "error")
return
}
if requestbody.Name == "" {
c.String(http.StatusBadRequest, "No name provided")
return
}
if requestbody.PowerLevel == nil {
c.String(http.StatusBadRequest, "No Powerlevel provided")
return
}

s.Processor().AddDragonBallCharacter(c, requestbody.Name, *requestbody.PowerLevel)
}

func (s *Server) HTTPUpdateDragonBallCharacter(c *gin.Context) {
logger.SBILog.Infof("In HTTPUpdateDragonBallCharacter")
targetName := c.Param("name")

if targetName == "" {
c.String(http.StatusBadRequest, "No name provided")
return
}

type RequestBody struct {
PowerLevel *int32 `json:"powerLevel"`
}
var requestbody RequestBody
if err := c.ShouldBindBodyWithJSON(&requestbody); err != nil {
c.String(http.StatusBadRequest, "error")
return
}

if requestbody.PowerLevel == nil {
c.String(http.StatusBadRequest, "No Powerlevel provided")
return
}

s.Processor().UpdateDragonBallCharacter(c, targetName, *requestbody.PowerLevel)
}
Loading
Loading