You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
3.6 KiB

package main
import (
_ "database/sql"
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
_ "gorm.io/driver/mysql"
"math/rand"
_ "os"
)
type Author struct {
gorm.Model
Name string`gorm:"varchar(100);not null"`
Dynasty string`gorm:"varchar(100);not null"`
}
type Format struct {
gorm.Model
Title string`gorm:"varchar(100);not null"`
Author string `gorm:"varchar(100);not null"`
Format string `gorm:"varchar(100);not null"`
Dynasty string `gorm:"varchar(100);not null"`
}
type Type struct {
gorm.Model
Title string`gorm:"varchar(100);not null"`
Author string `gorm:"varchar(100);not null"`
Type string `gorm:"varchar(100);not null"`
Dynasty string `gorm:"varchar(100);not null"`
}
func main() {
//获取初始化的数据库
db := InitDB()
//延迟关闭数据库
defer db.Close()
//创建一个默认的路由引擎
r := gin.Default()
config := cors.DefaultConfig()
config.AllowOrigins = []string{"*"}
r.Use(cors.New(config))
r.POST("/poetry", func(c *gin.Context) {
var format[] Format
name := c.PostForm("name")
db.Where("author =?", name).Find(&format)
c.JSON(200, gin.H{
"message":format,
})
})
//sql统计一个诗人不同风格诗的数量
r.POST("/authortypenum", func(c *gin.Context) {
type TypeCount struct {
Type string
Count int
}
var types []TypeCount
name := c.PostForm("name")
//db.Where("author =?", name).Find(&types)
db.Raw(
"select type,count(*) as count from cauc.types where author = ? group by type order by count desc limit 6",
name).Scan(&types)
result := make(map[string]int)
for _, tc := range types {
result[tc.Type] = tc.Count
}
c.JSON(200, gin.H{
"message": result,
})
})
//sql统计某个朝代的诗人数量
r.POST("/dynastyauthornum", func(c *gin.Context) {
type DynastyCount struct {
Dynasty string
Count int
}
var dynastys []DynastyCount
db.Raw(
"select dynasty,count(*) as count from cauc.authors group by dynasty").Scan(&dynastys)
result := make(map[string]int)
for _, dc := range dynastys {
result[dc.Dynasty] = dc.Count
}
c.JSON(200, gin.H{
"message": result,
})
})
//统计诗词每个类型的数量
r.POST("/typepeotrynum", func(c *gin.Context) {
type TypeCount struct {
Type string
Count int
}
var types []TypeCount
db.Raw(
"select type,count(*) as count from cauc.types group by type order by count desc").Scan(&types)
result := make(map[string]int)
var type1 []TypeCount
for i := 0; i < 6; i++ {
type1 = append(type1, types[rand.Intn(len(types))])
}
for _, tc := range type1 {
result[tc.Type] = tc.Count
}
c.JSON(200, gin.H{
"message": result,
})
})
//统计某个朝代诗词的形式数量
r.POST("/dynastyformatnum", func(c *gin.Context) {
type FormatCount struct {
Format string
Count int
}
var formats []FormatCount
dynasty := c.PostForm("dynasty")
db.Raw(
"select format,count(*) as count from cauc.formats where dynasty = ? group by format order by count desc",
dynasty).Scan(&formats)
result := make(map[string]int)
for _, fc := range formats {
result[fc.Format] = fc.Count
}
c.JSON(200, gin.H{
"message": result,
})
})
//在88800端口启动服务
panic(r.Run(":8880"))
}
func InitDB() *gorm.DB {
driverName := "mysql"
host := "127.0.0.1"
port := "3306"
database := "cauc"
username := "root"
password := "root"
charset := "utf8"
args := fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=%s&parseTime=true",
username,
password,
host,
port,
database,
charset)
db, _ := gorm.Open(driverName, args)
return db
}