Skip to content
Snippets Groups Projects
Unverified Commit 7f8403c0 authored by Cseh Viktor's avatar Cseh Viktor
Browse files

structure of the code initialized

parent a88cb6fd
No related branches found
No related tags found
No related merge requests found
package dbHelper package dbHelper
import ( import (
"fmt"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"mobwebhf/models" "mobwebhf/models"
...@@ -16,7 +17,7 @@ func Dbinit() *gorm.DB { ...@@ -16,7 +17,7 @@ func Dbinit() *gorm.DB {
} }
asd := db.AutoMigrate(&models.Question{}) asd := db.AutoMigrate(&models.Question{})
if asd != nil { if asd != nil {
fmt.Println("Failed to create table")
} }
return db return db
} }
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"mobwebhf/dbHelper" "mobwebhf/dbHelper"
"mobwebhf/routes"
) )
func something(c *gin.Context){ func something(c *gin.Context){
...@@ -19,7 +20,13 @@ func main() { ...@@ -19,7 +20,13 @@ func main() {
print(db) print(db)
} }
router := gin.Default() router := gin.Default()
ur := router.Group("/question")
router.GET("/Almafa", something) ur.GET("", routes.List)
ur.GET("/:id", routes.ListOne)
//router.GET("/Almafa", something)
router.Run() router.Run()
} }
...@@ -4,10 +4,11 @@ import "gorm.io/gorm" ...@@ -4,10 +4,11 @@ import "gorm.io/gorm"
type Question struct{ type Question struct{
gorm.Model gorm.Model
QuestionId int `gorm:"not null" json:"questionId"` //QuestionId int `gorm:"not null" json:"questionId"`
QestionText string `gorm:"type:text" json:"questionText"` QestionText string `gorm:"type:text" json:"questionText"`
Answer1 string `gorm:"type:text" json:"answer1"` Answer1 string `gorm:"type:text" json:"answer1"`
Answer2 string `gorm:"type:text" json:"answer2"` Answer2 string `gorm:"type:text" json:"answer2"`
Answer3 string `gorm:"type:text" json:"answer3"` Answer3 string `gorm:"type:text" json:"answer3"`
Answer4 string `gorm:"type:text" json:"answer4"` Answer4 string `gorm:"type:text" json:"answer4"`
Correct int `gorm:"type:text" json:"correct"`
} }
# Mobweb kikerdezo házifeladat backed
## Model
### Question
| Field | Type |
|---|---|
| id | Integer (auto) |
| questionText | String |
| answer1 | String |
| answer2 | String |
| answer3 | String |
| answer4 | String |
| correct | int |
## API
### GET /question
List of Questions
Authentication: _None_
Returned data: `200 OK`
```json
{
"data": [
{
"id": 1,
"questionText": "1.kerdes szövege",
"answer1": "Elso valaszlegetoseg",
"answer2": "Masodik valaszlegetoseg",
"answer3": "Harmadik valaszlegetoseg",
"answer4": "Negyedik valaszlegetoseg",
"correct": 1
},
...
]
}
```
### GET /question/:id
Get One question
Authentication: _None_
Returned data: `200 OK`
```json
{
"data": {
"id": 1,
"questionText": "1.kerdes szövege",
"answer1": "Elso valaszlegetoseg",
"answer2": "Masodik valaszlegetoseg",
"answer3": "Harmadik valaszlegetoseg",
"answer4": "Negyedik valaszlegetoseg",
"correct": 1
}
}
```
### PUT /question/:id
Update the question with Id
Sent data in body:
```json
{
"data": {
"questionText": "1.kerdes szövege",
"answer1": "Elso valaszlegetoseg",
"answer2": "Masodik valaszlegetoseg",
"answer3": "Harmadik valaszlegetoseg",
"answer4": "Negyedik valaszlegetoseg",
"correct": 1
}
}
```
Response: `200 OK`
### DELETE /question/:id
Delete a question by Id
Response: `200 OK`
### DELETE /history/:id
Delete service by the given Id
Response: `200 OK`
### POST /question
Create a question
Sent data in body:
```json
{
"questionText": "x.kerdes szövege",
"answer1": "Elso valaszlegetoseg",
"answer2": "Masodik valaszlegetoseg",
"answer3": "Harmadik valaszlegetoseg",
"answer4": "Negyedik valaszlegetoseg",
"correct": 1
}
```
Response: `201 CREATED`
```json
{
"id": 3
}
```
## Credits
For DB Connection https://gorm.io/ used as reference.
package routes package routes
package routes package routes
import (
"fmt"
"github.com/gin-gonic/gin"
_ "net/http"
_ "github.com/gin-gonic/gin"
)
func List(c *gin.Context){
fmt.Print("alma")
c.JSON(200, gin.H{
//TODO db list all question
})
}
func ListOne(c *gin.Context){
c.JSON(200, gin.H{
//TODO get specific question
})
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment