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.

219 lines
4.6 KiB

package main
import (
_ "database/sql"
"fmt"
"math/rand"
_ "os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
_ "gorm.io/driver/mysql"
)
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"`
}
type Mingju struct {
gorm.Model
Lines string `gorm:"varchar(256);not null"`
Author string `gorm:"varchar(100);not null"`
Sources 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)
c.JSON(200, gin.H{
"message": types,
})
})
//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)
c.JSON(200, gin.H{
"message": dynastys,
})
})
//统计诗词每个类型的数量
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)
var type1 []TypeCount
for i := 0; i < 6; i++ {
type1 = append(type1, types[rand.Intn(len(types))])
}
c.JSON(200, gin.H{
"message": type1,
})
})
//统计某个朝代诗词的形式数量
r.POST("/dynastyformatnum", func(c *gin.Context) {
type FormatCount struct {
Format string
Count int
}
var formats []FormatCount
var dynastys = []string{"先秦", "两汉", "魏晋", "南北朝", "唐代", "五代", "宋代", "金朝", "元代", "明代", "清代"}
dynasty := dynastys[rand.Intn(len(dynastys))]
db.Raw(
"select format,count(*) as count from cauc.formats where dynasty = ? group by format order by count desc",
dynasty).Scan(&formats)
c.JSON(200, gin.H{
"message": formats,
})
})
//随机从数据库中返回4个名句
r.POST("/randommingju", func(c *gin.Context) {
type Result struct {
Mingju string
Author string
}
var mingjus []Mingju
name := c.PostForm("name")
db.Raw(
"select * from cauc.mingjus where author=? order by rand() limit 4", name).Scan(&mingjus)
var results []Result
for _, fc := range mingjus {
results = append(results, Result{Mingju: fc.Lines, Author: fc.Author + "——" + fc.Sources})
}
c.JSON(200, gin.H{
"message": results,
})
})
r.POST("/formatnum", func(c *gin.Context) {
type FormatCount struct {
Format string
Count int
}
var formats []FormatCount
db.Raw(
"select format,count(*) as count from cauc.formats group by format order by count desc",
).Scan(&formats)
c.JSON(200, gin.H{
"message": formats,
})
})
//统计诗词总数量
r.POST("/poetrynum", func(c *gin.Context) {
type Count struct {
Count int
}
var count Count
db.Raw(
"select count(*) as count from cauc.types").Scan(&count)
c.JSON(200, gin.H{
"message": count,
})
})
//在88800端口启动服务
panic(r.Run(":8880"))
}
func InitDB() *gorm.DB {
driverName := "mysql"
host := "127.0.0.1"
port := "3306"
database := "cauc"
username := "root"
password := "240011@rbb"
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
}