add sqlc and character table.

This commit is contained in:
2021-03-21 20:53:21 +01:00
parent b6b7b8f1b6
commit c5ea598ea1
26 changed files with 1285 additions and 65 deletions
+18 -6
View File
@@ -2,12 +2,24 @@ package models
// Character is a common data model representing an RP character or NPC.
type Character struct {
ID string `json:"id" bson:"_id"`
Nicks []string `json:"nicks" bson:"nicks"`
Name string `json:"name" bson:"name"`
ShortName string `json:"shortName" bson:"shortName"`
Author string `json:"author" bson:"author"`
Description string `json:"description" bson:"description"`
ID string `json:"id" bson:"_id" db:"id"`
Nicks []string `json:"nicks" bson:"nicks" db:"nicks"`
Name string `json:"name" bson:"name" db:"name"`
ShortName string `json:"shortName" bson:"shortName" db:"short_name"`
Author string `json:"author" bson:"author" db:"author"`
Description string `json:"description" bson:"description" db:"description"`
}
func (character *Character) ApplyUpdate(update CharacterUpdate) {
if update.Name != nil {
character.Name = *update.Name
}
if update.ShortName != nil {
character.ShortName = *update.ShortName
}
if update.Description != nil {
character.Description = *update.Description
}
}
// Nick gets the character's nick.
+46 -1
View File
@@ -1,8 +1,10 @@
package models
import (
"errors"
"fmt"
"io"
"strings"
)
// A Tag associates a story with other content, like other stories, logs and more.
@@ -11,6 +13,28 @@ type Tag struct {
Name string `bson:"name"`
}
func (tag *Tag) Decode(v string) error {
split := strings.SplitN(v, ":", 2)
if len(split) == 1 {
return errors.New("bad tag name: " + v)
}
kind := TagKind("")
err := kind.UnmarshalGQL(v)
if err != nil {
return err
}
tag.Kind = kind
tag.Name = split[1]
return nil
}
func (tag *Tag) String() string {
return string(tag.Kind) + ":" + tag.Name
}
// Equal returns true if the tags match one another.
func (tag *Tag) Equal(other Tag) bool {
return tag.Kind == other.Kind && tag.Name == other.Name
@@ -22,6 +46,27 @@ func (*Tag) IsChangeObject() {
panic("this method is a dummy, and so is its caller")
}
func EncodeTagArray(arr []Tag) []string {
result := make([]string, len(arr))
for i, v := range arr {
result[i] = v.String()
}
return result
}
func DecodeTagArray(arr []string) ([]Tag, error) {
result := make([]Tag, len(arr))
for i, v := range arr {
err := result[i].Decode(v)
if err != nil {
return nil, err
}
}
return result, nil
}
// TagKind represents the kind of tags.
type TagKind string
@@ -60,7 +105,7 @@ func (e *TagKind) UnmarshalGQL(v interface{}) error {
// MarshalGQL turns it into a JSON string
func (e TagKind) MarshalGQL(w io.Writer) {
fmt.Fprint(w, "\""+string(e), "\"")
_, _ = fmt.Fprintf(w, "\"%s\"", string(e))
}
// TagFilter is a filter for tag listing.