Skip to content
Open
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
61 changes: 61 additions & 0 deletions internal/sbi/api_student.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package sbi

import (
"net/http"

"github.com/gin-gonic/gin"
)

// Rutas para el nuevo servicio /student
func (s *Server) getStudentRoute() []Route {
return []Route{
{
Name: "GetStudent",
Method: http.MethodGet,
Pattern: "/:id",
APIFunc: s.HTTPGetStudent,
},
{
Name: "CreateStudent",
Method: http.MethodPost,
Pattern: "/",
APIFunc: s.HTTPCreateStudent,
},
}
}

// Estructura para el cuerpo del POST
type Student struct {
ID string `json:"id" binding:"required"`
Name string `json:"name" binding:"required"`
}

// GET /student/:id
func (s *Server) HTTPGetStudent(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "id is required"})
return
}

c.JSON(http.StatusOK, gin.H{
"id": id,
"name": "Student " + id,
"msg": "Student info from nf-example",
})
}

// POST /student/
func (s *Server) HTTPCreateStudent(c *gin.Context) {
var student Student

if err := c.ShouldBindJSON(&student); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusCreated, gin.H{
"msg": "Student created successfully",
"student": student,
})
}
4 changes: 4 additions & 0 deletions internal/sbi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func newRouter(s *Server) *gin.Engine {

spyFamilyGroup := router.Group("/spyfamily")
applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute())

studentGroup := router.Group("/student")
applyRoutes(studentGroup, s.getStudentRoute())


return router
}
Expand Down