投票 V0 版本
2023/10/1大约 2 分钟
1. 让服务跑起来
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
fmt.Print("香香编程喵喵喵!")
g := gin.Default()
g.GET("/login", func(context *gin.Context) {
fmt.Print("香香编程喵喵喵!")
})
if err := g.Run(":8080"); err != nil {
fmt.Print("启动失败!")
}
}
2. 返回一个简单的页面
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>香香编程-投票项目</title>
</head>
<body>
<main class="main">
<form method="post" action="/login">
<input type="text" name="name" placeholder="Your name">
<input type="password" name="password" placeholder="Password">
<button type="submit">Sign in</button>
</form>
</main>
</body>
</html>
3. 链接数据库
_ "github.com/go-sql-driver/mysql"
"gorm.io/driver/mysql"
"gorm.io/gorm"
my := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local", "root", "root", "127.0.0.1:3306", "vote")
conn, err := gorm.Open(mysql.Open(my), &gorm.Config{})
if err != nil {
fmt.Printf("err:%s\n", err)
panic(err)
}
4. 代码分层
什么是MVC?MVC有什么好处?
- app装所有的代码模块
- logic 装逻辑方法
- model 装所有数据库相关的操作
- view 装前端页面
- tools 装一些零碎工具
- router 装gin的路由还有中间件
5. 登录态 cookie
http 是无状态的。
6. 实现简单投票功能
建表语句:
CREATE TABLE `user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`created_time` datetime DEFAULT NULL,
`updated_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `vote` (
`id` bigint NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`type` int DEFAULT NULL COMMENT '0单选1多选',
`status` int DEFAULT NULL COMMENT '0正常1超时',
`time` bigint DEFAULT NULL COMMENT '有效时长',
`user_id` bigint DEFAULT NULL COMMENT '创建人',
`created_time` datetime DEFAULT NULL,
`updated_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `vote_opt` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`vote_id` bigint DEFAULT NULL,
`count` int DEFAULT NULL,
`created_time` datetime DEFAULT NULL,
`updated_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `vote_opt_user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint DEFAULT NULL,
`vote_id` bigint DEFAULT NULL,
`vote_opt_id` bigint DEFAULT NULL,
`created_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
表之间的关系:投票和选项之间是1:n 用户和投票之间是n:n,必须三张表
7. 投票首页
<!doctype html>
<html lang="en">
<head>
<title>香香编程-投票项目</title>
</head>
<body>
<main>
{{range $key,$value := .vote}}
<h2>{{$value.Title}}</h2>
{{end}}
</main>
</body>
</html>
8. 投票详情页
<!doctype html>
<html lang="en">
<head>
<title>香香编程-投票项目</title>
</head>
<body>
<main>
<h2>title:{{.vote.Vote.Title}}</h2>
<h2>id:{{.vote.Vote.Id}}</h2>
<h2>UserId:{{.vote.Vote.UserId}}</h2>
<h2>Type:{{.vote.Vote.Type}}</h2>
<h2>Status:{{.vote.Vote.Status}}</h2>
<form method="post" action="/vote">
<input type="hidden" name="vote_id" value="{{.vote.Vote.Id}}">
{{range $key,$value := .vote.Opt}}
<input type="checkbox" name="opt[]" id="customCheck{{$key}}" value="{{$value.Id}}">
<label for="customCheck{{$key}}">{{$value.Name}}</label>
{{end}}
<button type="submit">Submit</button>
</form>
</main>
</body>
</html>