Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
Sample v1
Browse files Browse the repository at this point in the history
Happy DDL !
  • Loading branch information
kiramyby committed Oct 7, 2023
1 parent f9c1d0e commit e424069
Show file tree
Hide file tree
Showing 16 changed files with 1,159 additions and 0 deletions.
1 change: 1 addition & 0 deletions k1r4ca/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# HDUhelper_Todo
Empty file added k1r4ca/configs/configs.yaml
Empty file.
62 changes: 62 additions & 0 deletions k1r4ca/controller/delete/DelTx/DelTx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Package DelTx 更新表单实现id更新操作 同时利用Transaction进行并发控制
package DelTx

import (
"HDUhelper_Todo/models"
)

func DelTx(id int) error {

db := models.DbConnect()

tx := db.Begin() // 开始事务

// 设置事务的隔离级别为 SERIALIZABLE 以保证数据一致性
tx = tx.Set("gorm:query_option", "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE")

err := tx.Where("Id = ?", id).Delete(models.ListItem{}).Error //完成delete操作
if err != nil {
tx.Rollback() // 发生错误,回滚事务
return err
}

// 创建临时表
err = tx.Exec("CREATE TEMPORARY TABLE temp_table AS SELECT * FROM list_items").Error
if err != nil {
tx.Rollback()
return err
}

err = tx.Exec("DELETE FROM list_items").Error // 删除原始表中的所有数据
if err != nil {
tx.Rollback()
return err
}

// 更新临时表中的自增字段值
err = tx.Exec("UPDATE temp_table SET id = id - 1 WHERE id > ?", id).Error
if err != nil {
tx.Rollback()
return err
}

// 将临时表中的数据重新插入原始表中
err = tx.Exec("INSERT INTO list_items SELECT * FROM temp_table").Error
if err != nil {
tx.Rollback()
return err
}

err = tx.Exec("DROP TABLE temp_table").Error // 删除临时表
if err != nil {
tx.Rollback()
return err
}

err = tx.Commit().Error // 提交delete操作
if err != nil {
return err
}

return nil
}
30 changes: 30 additions & 0 deletions k1r4ca/controller/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Package delete_todo 删除指定id的ToDo内容
package delete

import (
"HDUhelper_Todo/controller/delete/DelTx"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)

type Controller struct {
}

func (c Controller) Delete(ctx *gin.Context) {

idStr := ctx.Param("id")
id, _ := strconv.Atoi(idStr) //利用int分析将idStr转换为int64

err := DelTx.DelTx(id)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "fail to delete",
})
}

ctx.JSON(http.StatusOK, gin.H{
"message": "Done!",
})

}
50 changes: 50 additions & 0 deletions k1r4ca/controller/get/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Package getall 获取所有ToDo内容
package get

import (
"HDUhelper_Todo/models"
ulits "HDUhelper_Todo/utils"

"github.com/gin-gonic/gin"
"net/http"
"time"
)

type AllController struct {
}

func (c AllController) GetAll(ctx *gin.Context) {

Item := make([]*models.ListItem, 0)
UpItem := make([]*models.ListItem, 0)

db := models.DbConnect()

//更新逾期item信息
t := time.Now()
err := db.Where("due_date <= ?", t).Find(&UpItem).Error
for i := range UpItem {
UpItem[i].Over = true
UpItem[i].UpdatedAt = time.Now()
err = db.Where("id = ?", UpItem[i].Id).Updates(&UpItem[i]).Error
}
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{
"message": "failed to update To-do item",
})
return
}

err = db.Order("due_date asc").Find(&Item).Error //查询数据并以截止日期升序排序
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{
"message": "failed to find",
})
return
}

//以json格式传输
ctx.JSON(http.StatusOK, gin.H{
"list": ulits.Response(Item), //时间转换 传输unix时间戳
})
}
79 changes: 79 additions & 0 deletions k1r4ca/controller/get/sole.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Package getsole 获取指定日期的ToDo内容 传入指定日期0秒UNIX时间戳
package get

import (
"HDUhelper_Todo/models"

"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"net/http"
"strconv"
"time"
)

type SoleController struct {
}

func (c SoleController) GetSole(ctx *gin.Context) {

dateStr := ctx.Param("date")

//将UNIX时间戳转换为go time
dateInt, _ := strconv.ParseInt(dateStr, 10, 64)
date := time.Unix(dateInt, 0)
db, err := gorm.Open(sqlite.Open("TodoList.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
return
}

Item := make([]*models.ListItem, 0)
UpItem := make([]*models.ListItem, 0)

//更新逾期item信息
t := time.Now()
err = db.Where("due_date <= ?", t).Find(&UpItem).Error
for i := range UpItem {
UpItem[i].Over = true
UpItem[i].UpdatedAt = time.Now()
err = db.Where("id = ?", UpItem[i].Id).Updates(&UpItem[i]).Error
}
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{
"message": "failed to update To-do item",
})
return
}

//查询指定日期数据并以截止日期升序排序
dd, _ := time.ParseDuration("24h")
dbWhere := db.Where("created_at >= ?", date).Where("created_at <= ?", date.Add(dd))
err = dbWhere.Order("due_date asc").Find(&Item).Error //太长了截成两句
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "failed to find",
})
return
}

//用ResponseItem结构体框架下的Items进行时间转换 以传输unix时间戳
Items := make([]*models.ResponseItem, len(Item))
for i := range Item {
Items[i] = &models.ResponseItem{
Id: Item[i].Id,
DueDate: Item[i].DueDate,
Item: Item[i].Item,
Done: Item[i].Done,
Over: Item[i].Over,
CreatedAt: Item[i].CreatedAt.Unix(),
UpdatedAt: Item[i].UpdatedAt.Unix(),
}
}

//以json格式返回
ctx.JSON(http.StatusOK, gin.H{
"list": Items,
})

}
23 changes: 23 additions & 0 deletions k1r4ca/controller/login/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package login

import (
ulits "HDUhelper_Todo/utils"
"github.com/gin-gonic/gin"
"net/http"
)

func IssueJWT(ctx *gin.Context) {

username := ctx.Param("username")
jwt, err := ulits.IssueToken(username)
if err != nil {
ctx.JSON(http.StatusUnauthorized, gin.H{
"error": "Failed to issue JWT. Please check and try again",
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"message": "Done!",
"Your JWT": jwt,
})
}
35 changes: 35 additions & 0 deletions k1r4ca/controller/post/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Package post 自动绑定json格式的新增请求 添加到数据库中
package post

import (
"HDUhelper_Todo/models"

"github.com/gin-gonic/gin"
"net/http"
)

type Controller struct {
}

func (c Controller) Post(ctx *gin.Context) {
tem := models.ListItem{}
db := models.DbConnect()
err := ctx.BindJSON(&tem)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "failed to bind",
})
return
}
err = db.Create(&tem).Error
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{
"message": "failed to creat",
})
return
}
//tem.CreatedAt = time.Now()
ctx.JSON(http.StatusOK, gin.H{
"message": "Done!",
})
}
40 changes: 40 additions & 0 deletions k1r4ca/controller/put/put.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package put 更新ToDo内容
package put

import (
"github.com/gin-gonic/gin"
"net/http"
"time"

"HDUhelper_Todo/models"
)

type Controller struct {
}

func (c Controller) Put(ctx *gin.Context) {

tem := models.ListItem{}

err := ctx.BindJSON(&tem)
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{
"message": "failed to update",
})
return
}

db := models.DbConnect()
tem.UpdatedAt = time.Now()
err = db.Where("id = ?", tem.Id).Updates(&tem).Error
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "failed to update",
})
return
}

ctx.JSON(http.StatusOK, gin.H{
"message": "Done!",
})
}
54 changes: 54 additions & 0 deletions k1r4ca/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module HDUhelper_Todo

go 1.21

require (
github.com/bytedance/sonic v1.10.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // 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/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.17.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/sqlite v1.5.3 // indirect
gorm.io/gorm v1.25.4 // indirect
)
Loading

0 comments on commit e424069

Please sign in to comment.