change save file method, add file changes history
This commit is contained in:
parent
519749fd3a
commit
820557a268
12 changed files with 2388 additions and 2036 deletions
82
server/data/commit.go
Normal file
82
server/data/commit.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
var schema = `
|
||||
CREATE TABLE IF NOT EXISTS commit_log (
|
||||
id VARCHAR(255) PRIMARY KEY,
|
||||
msg VARCHAR(255),
|
||||
created_at DATETIME,
|
||||
editor VARCHAR(255),
|
||||
change_file VARCHAR(255)
|
||||
);
|
||||
`
|
||||
|
||||
type CommitLog struct {
|
||||
Id string `db:"id"`
|
||||
Msg string `db:"msg"`
|
||||
Created_at string `db:"created_at"`
|
||||
Editor string `db:"editor"`
|
||||
Change_file string `db:"change_file"`
|
||||
}
|
||||
|
||||
func HashCommit(n int) (string, error) {
|
||||
// _, err := h.Write([]byte(target))
|
||||
// if err != nil {
|
||||
// Log.Debug("Error when hashing commit", zap.Error(err))
|
||||
// return "", err
|
||||
// }
|
||||
// return string(h.Sum(nil)), nil
|
||||
byt := make([]byte, n)
|
||||
_, err := rand.Read(byt)
|
||||
if err != nil {
|
||||
Log.Debug("Error when hashing commit", zap.Error(err))
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(byt), nil
|
||||
}
|
||||
|
||||
func Insert(c *CommitLog) error {
|
||||
commit_db, err := sqlx.Connect("sqlite3", "./data/database.db")
|
||||
if err != nil {
|
||||
Log.Fatal("Error when connecting to database", zap.Error(err))
|
||||
}
|
||||
// init table in db
|
||||
commit_db.MustExec(schema)
|
||||
|
||||
_, err = commit_db.NamedExec("INSERT INTO commit_log (id, msg, created_at, editor, change_file) VALUES (:id, :msg, :created_at, :editor, :change_file)", c)
|
||||
if err != nil {
|
||||
Log.Error("Error when insert commit log", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// func GetCommitLogOfFilename(filename string) ([]CommitLog, error) {
|
||||
//}
|
||||
// cut .json, split then get pos 2, check `change_file` startwith "filename" then return all quries
|
||||
|
||||
func GetCommitLogs() ([]CommitLog, error) {
|
||||
commit_db, err := sqlx.Connect("sqlite3", "./data/database.db")
|
||||
if err != nil {
|
||||
Log.Fatal("Error when connecting to database", zap.Error(err))
|
||||
}
|
||||
|
||||
var commits []CommitLog
|
||||
err = commit_db.Get(&commits, "SELECT * FROM commit_log", nil)
|
||||
|
||||
if err != nil {
|
||||
Log.Error("Error when get commit log", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return commits, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue