123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package v1
- import (
- "auth-server/common"
- pb "auth-server/rpc_idl/dr_study_pb"
- "auth-server/service"
- "fmt"
- "github.com/gin-gonic/gin"
- "google.golang.org/protobuf/encoding/protojson"
- "net/http"
- )
- func CreateStudy(c *gin.Context) {
- request := pb.StudyRequest{}
- reqBytes, err := c.GetRawData()
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read request body"})
- return
- }
- unmarshaler := protojson.UnmarshalOptions{
- AllowPartial: true, // 允许部分解析,即使缺少必需字段
- DiscardUnknown: true, // 丢弃 JSON 中 Protobuf 消息未定义的字段
- }
- err = unmarshaler.Unmarshal(reqBytes, &request)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Failed to unmarshal JSON to Protobuf: %v", err)})
- return
- }
- res, err := service.StudyClient.CreateStudy(common.GC2GM(c), &request)
- if err != nil {
- common.HttpErr(c, err)
- return
- }
- common.HttpSuccess(c, res)
- }
- func GetStudy(c *gin.Context) {
- id := c.Param("id")
- res, err := service.StudyClient.GetStudy(common.GC2GM(c), &id)
- if err != nil {
- common.HttpErr(c, err)
- return
- }
- common.HttpSuccess(c, res)
- }
|