diff --git a/go.mod b/go.mod index e26e6a9..9f8a385 100644 --- a/go.mod +++ b/go.mod @@ -9,9 +9,12 @@ require ( github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-playground/validator/v10 v10.10.1 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-sqlite3 v1.14.12 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/ugorji/go/codec v1.2.7 // indirect @@ -20,4 +23,6 @@ require ( golang.org/x/text v0.3.7 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gorm.io/driver/sqlite v1.3.2 // indirect + gorm.io/gorm v1.23.4 // indirect ) diff --git a/go.sum b/go.sum index 263b489..514f1fb 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,11 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= @@ -36,6 +41,8 @@ github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ic github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= +github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -96,3 +103,7 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.3.2 h1:nWTy4cE52K6nnMhv23wLmur9Y3qWbZvOBz+V4PrGAxg= +gorm.io/driver/sqlite v1.3.2/go.mod h1:B+8GyC9K7VgzJAcrcXMRPdnMcck+8FgJynEehEPM16U= +gorm.io/gorm v1.23.4 h1:1BKWM67O6CflSLcwGQR7ccfmC4ebOxQrTfOQGRE9wjg= +gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= diff --git a/web/handlers.remind.go b/web/handlers.remind.go index 6149868..49da2df 100644 --- a/web/handlers.remind.go +++ b/web/handlers.remind.go @@ -7,7 +7,7 @@ import ( ) func showIndexPage(c *gin.Context) { - reminders := getAllReminds() + reminders, _ := getAllReminds() // Вызовем метод HTML из Контекста Gin для обработки шаблона c.HTML( @@ -23,3 +23,26 @@ func showIndexPage(c *gin.Context) { ) } + +func showOneRemind(c *gin.Context) { + var remind Remind + if err := DB.Where("id = ?", c.Param("id")).First(&remind).Error; err != nil { + c.HTML(http.StatusBadRequest, "404.html", gin.H{"title": "Error", "error": "Record not found!"}) + return + } + + c.HTML( + // Зададим HTTP статус 200 (OK) + http.StatusOK, + // Используем шаблон index.html + "remind.view.html", + // Передадим данные в шаблон + gin.H{ + "title": "View remind", + "payload": remind, + }, + ) + +} + +func createOneRemind(c *gin.Context) {} diff --git a/web/main.go b/web/main.go index 1f7b936..8271838 100644 --- a/web/main.go +++ b/web/main.go @@ -1,8 +1,29 @@ package main -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) var router *gin.Engine +var DB *gorm.DB + +func init() { + // Подключаем БД + db, err := gorm.Open(sqlite.Open("remind.db"), &gorm.Config{}) + if err != nil { + panic(err.Error) + } + + db.AutoMigrate(&Remind{}) + result := db.Find(&Remind{}) + if result.RowsAffected == 0 { + FirstRemindInit(db) + } + DB = db + +} func main() { diff --git a/web/models.remind.go b/web/models.remind.go index ee958a1..2880f22 100644 --- a/web/models.remind.go +++ b/web/models.remind.go @@ -1,20 +1,35 @@ package main -import "time" +import ( + "time" -type remind struct { - ID int `json:"id"` + "gorm.io/gorm" +) + +type Remind struct { + ID int `json:"id" gorm:"primary_key"` Title string `json:"title"` - DateTime time.Time `json:"datetime"` + DateTime time.Time `json:"datetime" gorm:"index"` Content string `json:"content"` } -var remindList = []remind{ - remind{ID: 1, Title: "Remind 1", DateTime: time.Now(), Content: "Remind 1 body"}, - remind{ID: 2, Title: "Remind 2", DateTime: time.Now(), Content: "Remind 2 body"}, +// Return a list of all the articles +func getAllReminds() ([]Remind, error) { + var reminds []Remind + result := DB.Find(&reminds) + if err := result.Error; err != nil { + return nil, err + } + return reminds, nil } -// Return a list of all the articles -func getAllReminds() []remind { - return remindList +func FirstRemindInit(db *gorm.DB) { + r := []Remind{ + {ID: 1, Title: "Remind 1", DateTime: time.Now(), Content: "Remind 1 body"}, + {ID: 2, Title: "Remind 2", DateTime: time.Now(), Content: "Remind 2 body"}, + } + result := db.Create(&r) + if result.RowsAffected == 0 { + panic("Not inserted test remind!") + } } diff --git a/web/remind.db b/web/remind.db new file mode 100644 index 0000000..0f0d4ca Binary files /dev/null and b/web/remind.db differ diff --git a/web/routes.go b/web/routes.go index a3b97c5..75294df 100644 --- a/web/routes.go +++ b/web/routes.go @@ -4,5 +4,7 @@ func initializeRoutes() { // обработчик главного роута router.GET("/", showIndexPage) + router.GET("/remind/view/:id", showOneRemind) + router.POST("/remind/create", createOneRemind) } diff --git a/web/templates/404.html b/web/templates/404.html new file mode 100644 index 0000000..a74bcd3 --- /dev/null +++ b/web/templates/404.html @@ -0,0 +1,11 @@ + + + +{{ template "header.html" .}} + + +

{{.title}}

+

{{.error}}

+ + +{{ template "footer.html" .}} \ No newline at end of file diff --git a/web/templates/index.html b/web/templates/index.html index bfba87e..ad0736a 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -10,7 +10,7 @@

{{.Title}}

-

{{.DateTime}}

+

{{.DateTime.Format "Jan 02, 2006 15:04:05"}}

{{.Content}}

{{end}} diff --git a/web/templates/remind.view.html b/web/templates/remind.view.html new file mode 100644 index 0000000..ebbfcd4 --- /dev/null +++ b/web/templates/remind.view.html @@ -0,0 +1,13 @@ + + + +{{ template "header.html" .}} + + +

{{.payload.Title}}

+

Создано: {{.payload.DateTime.Format "Jan 02, 2006 15:04:05"}}

+ +

Описание: {{.payload.Content}}

+ + +{{ template "footer.html" .}} \ No newline at end of file