dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

tableUserPrivateNotes.go (758B)


      1 package database
      2 
      3 import (
      4 	"errors"
      5 	"github.com/asaskevich/govalidator"
      6 	"time"
      7 )
      8 
      9 type UserPrivateNote struct {
     10 	ID        int64
     11 	UserID    UserID
     12 	Notes     EncryptedString
     13 	CreatedAt time.Time
     14 	UpdatedAt time.Time
     15 }
     16 
     17 func (d *DkfDB) GetUserPrivateNotes(userID UserID) (out UserPrivateNote, err error) {
     18 	err = d.db.First(&out, "user_id = ?", userID).Error
     19 	return
     20 }
     21 
     22 func (d *DkfDB) SetUserPrivateNotes(userID UserID, notes string) error {
     23 	if !govalidator.RuneLength(notes, "0", "10000") {
     24 		return errors.New("notes must have 10000 characters maximum")
     25 	}
     26 	n := UserPrivateNote{UserID: userID}
     27 	if err := d.db.FirstOrCreate(&n, "user_id = ?", userID).Error; err != nil {
     28 		return err
     29 	}
     30 	n.Notes = EncryptedString(notes)
     31 	return d.db.Save(&n).Error
     32 }