Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"fmt"
"log"
"math/rand"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -19,6 +21,9 @@ func (Tweet) TableName() string {
}

func main() {
r := rand.Int()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reported by reviewdog 🐶
Do not use math/rand. Use crypto/rand instead.

fmt.Printf("Hello: %d\n", r)

db, err := gorm.Open(sqlite.Open("sample.db"), &gorm.Config{})
if err != nil {
log.Fatalf("failed to open a database: %s", err.Error())
Expand All @@ -33,6 +38,15 @@ func main() {
"posts": posts,
})
})
r.GET("/search", func(c *gin.Context) {
title, _ := c.GetQuery("title")
query := fmt.Sprintf("select * from posts where title LIKE '%%%s%%'", title)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reported by reviewdog 🐶
A raw query is used: "select * from posts where title LIKE '%%%s%%'".

posts := []Tweet{}
db.Raw(query).Scan(&posts)
c.JSON(http.StatusOK, map[string]interface{}{
"posts": posts,
})
})
r.POST("/post", func(c *gin.Context) {
title := c.PostForm("title")
db.Create(&Tweet{
Expand Down