feat(mutation): UpsertChannel (lightweight tags)
This commit is contained in:
parent
25d7f3bb9b
commit
7f9f85f061
1 changed files with 53 additions and 0 deletions
53
mutations_upsert_channel.go
Normal file
53
mutations_upsert_channel.go
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// UpsertChannel — create or update a channel. Channels are lightweight tags.
|
||||||
|
type UpsertChannel struct {
|
||||||
|
Slug string
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *UpsertChannel) Kind() string { return "upsert_channel" }
|
||||||
|
|
||||||
|
func (m *UpsertChannel) AffectedAuthors() []string {
|
||||||
|
return nil // channels don't attribute to an author
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *UpsertChannel) Validate(s *Store) error {
|
||||||
|
if strings.TrimSpace(m.Slug) == "" {
|
||||||
|
return &ValidationError{Field: "slug", Message: "required"}
|
||||||
|
}
|
||||||
|
for _, r := range m.Slug {
|
||||||
|
if r == ' ' || r == '\t' || r == '\n' || r == '/' {
|
||||||
|
return &ValidationError{Field: "slug", Message: "must not contain whitespace or slashes"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(m.Name) == "" {
|
||||||
|
return &ValidationError{Field: "name", Message: "required"}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *UpsertChannel) Apply(s *Store, lock LockRegion) error {
|
||||||
|
now := s.Now()
|
||||||
|
c := &Channel{
|
||||||
|
Slug: m.Slug,
|
||||||
|
Name: m.Name,
|
||||||
|
Description: m.Description,
|
||||||
|
CreatedAt: now,
|
||||||
|
}
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if existing, ok := s.channels[m.Slug]; ok {
|
||||||
|
c.CreatedAt = existing.CreatedAt
|
||||||
|
}
|
||||||
|
s.channels[m.Slug] = c
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = fmt.Sprintf // keep fmt for future error formatting
|
||||||
Loading…
Add table
Add a link
Reference in a new issue