Browse Source

Edit

master
parent
commit
3dda734ae4
  1. 5
      go.mod
  2. 11
      go.sum
  3. 25
      web/handlers.remind.go
  4. 23
      web/main.go
  5. 35
      web/models.remind.go
  6. BIN
      web/remind.db
  7. 2
      web/routes.go
  8. 11
      web/templates/404.html
  9. 2
      web/templates/index.html
  10. 13
      web/templates/remind.view.html

5
go.mod

@ -9,9 +9,12 @@ require ( @@ -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 ( @@ -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
)

11
go.sum

@ -21,6 +21,11 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw @@ -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 @@ -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= @@ -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=

25
web/handlers.remind.go

@ -7,7 +7,7 @@ import ( @@ -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) { @@ -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) {}

23
web/main.go

@ -1,8 +1,29 @@ @@ -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() {

35
web/models.remind.go

@ -1,20 +1,35 @@ @@ -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!")
}
}

BIN
web/remind.db

Binary file not shown.

2
web/routes.go

@ -4,5 +4,7 @@ func initializeRoutes() { @@ -4,5 +4,7 @@ func initializeRoutes() {
// обработчик главного роута
router.GET("/", showIndexPage)
router.GET("/remind/view/:id", showOneRemind)
router.POST("/remind/create", createOneRemind)
}

11
web/templates/404.html

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
<!--404.html-->
<!--Embed the header.html template at this location-->
{{ template "header.html" .}}
<!--Loop over the `payload` variable, which is the list of articles-->
<h2>{{.title}}</h2>
<p>{{.error}}</p>
<!--Embed the footer.html template at this location-->
{{ template "footer.html" .}}

2
web/templates/index.html

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
<!--Display the title of the article -->
<h2>{{.Title}}</h2>
</a>
<p>{{.DateTime}}</p>
<p>{{.DateTime.Format "Jan 02, 2006 15:04:05"}}</p>
<!--Display the content of the article-->
<p>{{.Content}}</p>
{{end}}

13
web/templates/remind.view.html

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
<!--index.html-->
<!--Embed the header.html template at this location-->
{{ template "header.html" .}}
<!--Loop over the `payload` variable, which is the list of articles-->
<h2>{{.payload.Title}}</h2>
<p>Создано: {{.payload.DateTime.Format "Jan 02, 2006 15:04:05"}}</p>
<!--Display the content of the article-->
<p>Описание: {{.payload.Content}}</p>
<!--Embed the footer.html template at this location-->
{{ template "footer.html" .}}
Loading…
Cancel
Save