diff --git a/README.md b/README.md index 9147816c37fad7e34bcc55afe48b49aff9bc15f3..9954129a3e53d2399c4bf37d5a2477d71d64aea4 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 11bd3b2ad9ca7de53b6e41817fcfc95e0b7b47c8..21b47fd1a22bbede0fb37f5aef7d80822e771d0b 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 fa7fce9181be08f9542d7553500ed06b49882962..4c04104c780866a0af354ea93a3a61d0698dc2bc 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 6d6b2193979d291f8df6529d4fb315c23f44b73e..d486fcb7dd3ddc45c5726e92ff5aac30051d7b53 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 e202c617a00ffad87464c1b1e142262fc715f60f..329ef60f1c013b2f9bdf8d09a7046443f95c0747 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 c633a3eb463fe2e85ee208dcd19df8cfcfdc171b..85c8c3e589fdb04b0ac1572b250c77594c898347 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 5d023a7e94a80c4471ec8f55ff13ce3462e91a89..99d79b43ea0fd7c3cb7e1094e8660ddb1c2aaad5 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 +} +