forked from jriou/coller
feat: Use snowflake identifiers
Fixes #29. Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
d26879a333
commit
2c3ca08dbf
9 changed files with 24 additions and 39 deletions
|
@ -1,17 +1,11 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.riou.xyz/jriou/coller/internal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const ID_MAX_RETRIES = 10
|
||||
|
||||
var idLength = 5
|
||||
|
||||
type Note struct {
|
||||
ID string `json:"id" gorm:"primaryKey"`
|
||||
Content []byte `json:"content" gorm:"not null"`
|
||||
|
@ -21,27 +15,8 @@ type Note struct {
|
|||
Language string `json:"language"`
|
||||
}
|
||||
|
||||
// Generate ID and compress content before saving to the database
|
||||
// Compress content before saving to the database
|
||||
func (n *Note) BeforeCreate(trx *gorm.DB) (err error) {
|
||||
for i := 0; i < ID_MAX_RETRIES; i++ {
|
||||
if n.ID != "" {
|
||||
continue
|
||||
}
|
||||
|
||||
id := internal.GenerateChars(idLength)
|
||||
|
||||
var note Note
|
||||
trx.Where("id = ?", id).Find(¬e)
|
||||
|
||||
if note.ID == "" {
|
||||
n.ID = id
|
||||
continue
|
||||
}
|
||||
}
|
||||
if n.ID == "" {
|
||||
return fmt.Errorf("could not find unique id before creating the note")
|
||||
}
|
||||
|
||||
n.Content = Compress(n.Content)
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue