GraphQL API and utilities for the rpdata project
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.
|
|
package posts
import ( "crypto/rand" "encoding/binary" "errors" "strconv" "time"
"git.aiterp.net/rpdata/api/internal/counter" "git.aiterp.net/rpdata/api/models" )
// Add creates a new post.
func Add(log models.Log, time time.Time, kind, nick, text string) (models.Post, error) { if kind == "" || nick == "" || text == "" { return models.Post{}, errors.New("Missing/empty parameters") }
mutex.RLock() defer mutex.RUnlock()
position, err := counter.Next("next_post_id", log.ShortID) if err != nil { return models.Post{}, err }
post := models.Post{ ID: generateID(time), Position: position, LogID: log.ShortID, Time: time, Kind: kind, Nick: nick, Text: text, }
err = collection.Insert(post) if err != nil { return models.Post{}, err }
return post, nil }
func generateID(time time.Time) string { data := make([]byte, 4) rand.Read(data)
return "P" + strconv.FormatInt(time.UnixNano(), 36) + strconv.FormatInt(int64(binary.LittleEndian.Uint32(data)), 36) }
|