From 1a397f856afa1e3b1ddcd0ab8bddf6fb882f2fa4 Mon Sep 17 00:00:00 2001 From: clupus <154858@sch.bme.hu> Date: Sun, 24 Oct 2021 18:47:04 +0200 Subject: [PATCH] Kesz az api --- README.md | 1 + dbHelper/dbController.go | 10 ++++++++-- models/models.go | 10 ++++------ routes/get.go | 18 ++++++++++++++---- routes/post.go | 17 ++++++++++++++++- routes/put.go | 21 +++++++++++++++++++++ routes/request.go | 39 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 103 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9147816..9954129 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ Sent data in body: ```json { "data": { + "id": 1, "questionText": "1.kerdes szövege", "answer1": "Elso valaszlegetoseg", "answer2": "Masodik valaszlegetoseg", diff --git a/dbHelper/dbController.go b/dbHelper/dbController.go index 11bd3b2..21b47fd 100644 --- a/dbHelper/dbController.go +++ b/dbHelper/dbController.go @@ -11,11 +11,17 @@ var Db *gorm.DB func Dbinit() { dsn := "host=localhost user=postgres password=almafa port=5432 sslmode=disable TimeZone=Europe/Budapest" - db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + var err error + Db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { panic("failed to connect database") } - asd := db.AutoMigrate(&models.Question{}) + fmt.Print("Ez a db ---------------") + fmt.Println(&Db) + if Db == nil { + fmt.Print("Ez bizony nullpointer...") + } + asd := Db.AutoMigrate(&models.Question{}) if asd != nil { fmt.Println("Failed to create table") } diff --git a/models/models.go b/models/models.go index fa7fce9..4c04104 100644 --- a/models/models.go +++ b/models/models.go @@ -1,14 +1,12 @@ package models -import "gorm.io/gorm" - type Question struct{ - gorm.Model - //QuestionId int `gorm:"not null" json:"questionId"` - QestionText string `gorm:"type:text" json:"questionText"` + //gorm.Model + Id uint `gorm:"primaryKey;autoIncrement" json:"questionId"` + QuestionText string `gorm:"type:text" json:"questionText"` Answer1 string `gorm:"type:text" json:"answer1"` Answer2 string `gorm:"type:text" json:"answer2"` Answer3 string `gorm:"type:text" json:"answer3"` Answer4 string `gorm:"type:text" json:"answer4"` - Correct int `gorm:"type:text" json:"correct"` + Correct int `gorm:"type:uint" json:"correct"` } diff --git a/routes/get.go b/routes/get.go index 6d6b219..d486fcb 100644 --- a/routes/get.go +++ b/routes/get.go @@ -1,6 +1,7 @@ package routes import ( + "fmt" "github.com/gin-gonic/gin" "mobwebhf/dbHelper" "mobwebhf/models" @@ -8,16 +9,25 @@ import ( ) func List(c *gin.Context){ - result := dbHelper.Db.Find(&models.Question{}) - c.JSON(http.StatusOK, result) + var a []models.Question + + result := dbHelper.Db.Find(&a) + if result.Error != nil { + fmt.Println("hiba a lekeresben") + } + c.JSON(http.StatusOK, a) } func View(c *gin.Context){ + var a models.Question id, err := ID(c) if err != nil { c.AbortWithStatus(http.StatusBadRequest) }else { - result := dbHelper.Db.Where("id = ?", id).First(&models.Question{}) - c.JSON(http.StatusOK, result) + result := dbHelper.Db.Where("id = ?", id).First(&a) + if result.Error != nil{ + fmt.Println("Baj van a lekeressel") + } + c.JSON(http.StatusOK, a) } } \ No newline at end of file diff --git a/routes/post.go b/routes/post.go index e202c61..329ef60 100644 --- a/routes/post.go +++ b/routes/post.go @@ -2,8 +2,23 @@ package routes import ( "github.com/gin-gonic/gin" + "mobwebhf/dbHelper" + "mobwebhf/models" ) -func Create(c *gin.Context) { +func Create(c *gin.Context) { + r, err := QuestionCreate(c) + if err != nil { + return + } + question := &models.Question{ + QuestionText: r.QuestionText, + Answer1: r.Answer1, + Answer2: r.Answer2, + Answer3: r.Answer3, + Answer4: r.Answer4, + Correct: r.Correct, + } + dbHelper.Db.Create(&question) } diff --git a/routes/put.go b/routes/put.go index c633a3e..85c8c3e 100644 --- a/routes/put.go +++ b/routes/put.go @@ -3,8 +3,29 @@ package routes import ( "github.com/gin-gonic/gin" + "mobwebhf/dbHelper" + "mobwebhf/models" + "net/http" ) func Update(c *gin.Context){ + id, err := ID(c) + if err != nil { + c.AbortWithStatus(http.StatusBadRequest) + } + r, err := QuestionUpdate(c);if err != nil{ + return + } + + question := &models.Question{ + Id: uint(id), + QuestionText: r.QuestionText, + Answer1: r.Answer1, + Answer2: r.Answer2, + Answer3: r.Answer3, + Answer4: r.Answer4, + Correct: r.Correct, + } + dbHelper.Db.Save(&question) } diff --git a/routes/request.go b/routes/request.go index 5d023a7..99d79b4 100644 --- a/routes/request.go +++ b/routes/request.go @@ -1,11 +1,30 @@ package routes import ( + "fmt" "github.com/gin-gonic/gin" "net/http" "strconv" ) +type QuestionCreateStruct struct { + QuestionText string `gorm:"type:text" json:"questionText" binding:"required"` + Answer1 string `gorm:"type:text" json:"answer1" binding:"required"` + Answer2 string `gorm:"type:text" json:"answer2" binding:"required"` + Answer3 string `gorm:"type:text" json:"answer3" binding:"required"` + Answer4 string `gorm:"type:text" json:"answer4" binding:"required"` + Correct int `gorm:"type:text" json:"correct" binding:"required"` +} + +type QuestionUpdateStruct struct { + QuestionText string `gorm:"type:text" json:"questionText"` + Answer1 string `gorm:"type:text" json:"answer1"` + Answer2 string `gorm:"type:text" json:"answer2"` + Answer3 string `gorm:"type:text" json:"answer3"` + Answer4 string `gorm:"type:text" json:"answer4"` + Correct int `gorm:"type:uint" json:"correct"` +} + func ID(c *gin.Context) (int, error) { id, err := strconv.Atoi(c.Param("id")) if err != nil { @@ -14,3 +33,23 @@ func ID(c *gin.Context) (int, error) { } return id, nil } + +func QuestionCreate(c *gin.Context) (*QuestionCreateStruct, error) { + var r QuestionCreateStruct + fmt.Print(r) + if err := c.ShouldBindJSON(&r); err != nil{ + return nil, err + } + + return &r, nil +} + + +func QuestionUpdate(c *gin.Context) (*QuestionUpdateStruct, error) { + var r QuestionUpdateStruct + if err := c.ShouldBindJSON(&r); err != nil { + return nil, err + } + return &r, nil +} + -- GitLab