From a08d712d33092075901e1d87a1185b737f81e385 Mon Sep 17 00:00:00 2001 From: clupus <154858@sch.bme.hu> Date: Fri, 22 Oct 2021 19:27:10 +0200 Subject: [PATCH] felig kesz. Adom ezt a go-t --- dbHelper/dbController.go | 5 ++--- main.go | 6 ++---- routes/delete.go | 9 ++++++++- routes/get.go | 12 ++++++------ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/dbHelper/dbController.go b/dbHelper/dbController.go index c412811..11bd3b2 100644 --- a/dbHelper/dbController.go +++ b/dbHelper/dbController.go @@ -7,9 +7,9 @@ import ( "mobwebhf/models" ) +var Db *gorm.DB - -func Dbinit() *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{}) if err != nil { @@ -19,5 +19,4 @@ func Dbinit() *gorm.DB { if asd != nil { fmt.Println("Failed to create table") } - return db } diff --git a/main.go b/main.go index fc2338d..db6396e 100644 --- a/main.go +++ b/main.go @@ -15,10 +15,8 @@ func something(c *gin.Context){ func main() { fmt.Println("alma") - db := dbHelper.Dbinit() - if db != nil { - print(db) - } + dbHelper.Dbinit() + router := gin.Default() ur := router.Group("/question") diff --git a/routes/delete.go b/routes/delete.go index 84bef88..aced349 100644 --- a/routes/delete.go +++ b/routes/delete.go @@ -2,8 +2,15 @@ package routes import ( "github.com/gin-gonic/gin" + "mobwebhf/dbHelper" + "mobwebhf/models" + "net/http" ) func Delete(c *gin.Context){ - + id, err := ID(c) + if err != nil { + c.AbortWithStatus(http.StatusBadRequest) + } + dbHelper.Db.Delete(&models.Question{}, id) } \ No newline at end of file diff --git a/routes/get.go b/routes/get.go index 3351242..6d6b219 100644 --- a/routes/get.go +++ b/routes/get.go @@ -2,13 +2,14 @@ package routes import ( "github.com/gin-gonic/gin" + "mobwebhf/dbHelper" + "mobwebhf/models" "net/http" ) func List(c *gin.Context){ - c.JSON(200, gin.H{ - //TODO db list all question - }) + result := dbHelper.Db.Find(&models.Question{}) + c.JSON(http.StatusOK, result) } func View(c *gin.Context){ @@ -16,8 +17,7 @@ func View(c *gin.Context){ if err != nil { c.AbortWithStatus(http.StatusBadRequest) }else { - c.JSON(200, gin.H{ - //TODO get specific question - }) + result := dbHelper.Db.Where("id = ?", id).First(&models.Question{}) + c.JSON(http.StatusOK, result) } } \ No newline at end of file -- GitLab