From 28f5200946caac8fdb8a632ec794c41bd5faf028 Mon Sep 17 00:00:00 2001 From: solar224 Date: Fri, 19 Sep 2025 19:24:29 +0800 Subject: [PATCH] feat: add task api service with GET and POST methods --- internal/context/context.go | 7 ++++++ internal/sbi/api_task.go | 32 ++++++++++++++++++++++++++ internal/sbi/processor/task_handler.go | 26 +++++++++++++++++++++ internal/sbi/router.go | 3 +++ 4 files changed, 68 insertions(+) create mode 100644 internal/sbi/api_task.go create mode 100644 internal/sbi/processor/task_handler.go diff --git a/internal/context/context.go b/internal/context/context.go index 3b98168..662fec4 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -4,12 +4,17 @@ import ( "os" "github.com/andy89923/nf-example/internal/logger" + "github.com/andy89923/nf-example/pkg/factory" "github.com/google/uuid" "github.com/free5gc/openapi/models" ) +type Task struct { + ID int `json:"id"` + Name string `json:"name"` +} type NFContext struct { NfId string Name string @@ -18,6 +23,7 @@ type NFContext struct { SBIPort int SpyFamilyData map[string]string + Tasks []Task } var nfContext = NFContext{} @@ -57,6 +63,7 @@ func InitNfContext() { "Henry": "Henderson", "Martha": "Marriott", } + nfContext.Tasks = make([]Task, 0) } func GetSelf() *NFContext { diff --git a/internal/sbi/api_task.go b/internal/sbi/api_task.go new file mode 100644 index 0000000..8733e31 --- /dev/null +++ b/internal/sbi/api_task.go @@ -0,0 +1,32 @@ +package sbi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func (s *Server) HTTPCreateNewTask(c *gin.Context) { + s.Processor().CreateNewTask(c) +} + +func (s *Server) HTTPGetAllTasks(c *gin.Context) { + s.Processor().GetAllTasks(c) +} + +func (s *Server) getTaskRoute() []Route { + return []Route{ + { + Name: "Get All Tasks", + Method: http.MethodGet, + Pattern: "/tasks", + APIFunc: s.HTTPGetAllTasks, + }, + { + Name: "Create New Task", + Method: http.MethodPost, + Pattern: "/tasks", + APIFunc: s.HTTPCreateNewTask, + }, + } +} diff --git a/internal/sbi/processor/task_handler.go b/internal/sbi/processor/task_handler.go new file mode 100644 index 0000000..ea60a32 --- /dev/null +++ b/internal/sbi/processor/task_handler.go @@ -0,0 +1,26 @@ +package processor + +import ( + "net/http" + + "github.com/andy89923/nf-example/internal/context" + "github.com/gin-gonic/gin" +) + +func (p *Processor) CreateNewTask(c *gin.Context) { + var newTask context.Task + if err := c.ShouldBindJSON(&newTask); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"}) + return + } + tasks := p.Context().Tasks + newTask.ID = len(tasks) + 1 + p.Context().Tasks = append(tasks, newTask) + + c.JSON(http.StatusCreated, newTask) +} + +func (p *Processor) GetAllTasks(c *gin.Context) { + tasks := p.Context().Tasks + c.JSON(http.StatusOK, tasks) +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index 6fa35e1..5bdbdf9 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -45,6 +45,9 @@ func newRouter(s *Server) *gin.Engine { spyFamilyGroup := router.Group("/spyfamily") applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) + taskGroup := router.Group("/api") + applyRoutes(taskGroup, s.getTaskRoute()) + return router }