@@ -5,12 +5,16 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.aiterp.net/rpdata/api/graph2/input"
|
||||||
|
"git.aiterp.net/rpdata/api/internal/auth"
|
||||||
"git.aiterp.net/rpdata/api/internal/loader"
|
"git.aiterp.net/rpdata/api/internal/loader"
|
||||||
"git.aiterp.net/rpdata/api/models"
|
"git.aiterp.net/rpdata/api/models"
|
||||||
"git.aiterp.net/rpdata/api/models/logs"
|
"git.aiterp.net/rpdata/api/models/logs"
|
||||||
"github.com/99designs/gqlgen/graphql"
|
"github.com/99designs/gqlgen/graphql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Queries
|
||||||
|
|
||||||
func (r *resolver) Log(ctx context.Context, id string) (models.Log, error) {
|
func (r *resolver) Log(ctx context.Context, id string) (models.Log, error) {
|
||||||
return logs.FindID(id)
|
return logs.FindID(id)
|
||||||
}
|
}
|
||||||
@@ -44,3 +48,56 @@ func (r *resolver) Logs(ctx context.Context, filter *logs.Filter) ([]models.Log,
|
|||||||
|
|
||||||
return logs, nil
|
return logs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mutations
|
||||||
|
|
||||||
|
func (r *mutationResolver) AddLog(ctx context.Context, input input.LogAddInput) (models.Log, error) {
|
||||||
|
token := auth.TokenFromContext(ctx)
|
||||||
|
if !token.Authenticated() || !token.Permitted("log.add") {
|
||||||
|
return models.Log{}, errors.New("You are not permitted to add logs")
|
||||||
|
}
|
||||||
|
|
||||||
|
open := input.Open != nil && *input.Open == true
|
||||||
|
title := ""
|
||||||
|
if input.Title != nil {
|
||||||
|
title = *input.Title
|
||||||
|
}
|
||||||
|
event := ""
|
||||||
|
if input.Event != nil {
|
||||||
|
event = *input.Event
|
||||||
|
}
|
||||||
|
description := ""
|
||||||
|
if input.Description != nil {
|
||||||
|
description = *input.Description
|
||||||
|
}
|
||||||
|
|
||||||
|
return logs.Add(input.Date, input.Channel, title, event, description, open)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *mutationResolver) EditLog(ctx context.Context, input input.LogEditInput) (models.Log, error) {
|
||||||
|
token := auth.TokenFromContext(ctx)
|
||||||
|
if !token.Authenticated() || !token.Permitted("log.edit") {
|
||||||
|
return models.Log{}, errors.New("You are not permitted to edit logs")
|
||||||
|
}
|
||||||
|
|
||||||
|
log, err := logs.FindID(input.ID)
|
||||||
|
if err != nil {
|
||||||
|
return models.Log{}, errors.New("Log not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return logs.Edit(log, input.Title, input.Event, input.Description, input.Open)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *mutationResolver) RemoveLog(ctx context.Context, input input.LogRemoveInput) (models.Log, error) {
|
||||||
|
token := auth.TokenFromContext(ctx)
|
||||||
|
if !token.Authenticated() || !token.Permitted("log.remove") {
|
||||||
|
return models.Log{}, errors.New("You are not permitted to remove logs")
|
||||||
|
}
|
||||||
|
|
||||||
|
log, err := logs.FindID(input.ID)
|
||||||
|
if err != nil {
|
||||||
|
return models.Log{}, errors.New("Log not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return logs.Remove(log)
|
||||||
|
}
|
||||||
|
|||||||
@@ -85,6 +85,16 @@ type Mutation {
|
|||||||
removeChapter(input: ChapterRemoveInput!): Chapter!
|
removeChapter(input: ChapterRemoveInput!): Chapter!
|
||||||
|
|
||||||
|
|
||||||
|
# Add a new log
|
||||||
|
addLog(input: LogAddInput!): Log!
|
||||||
|
|
||||||
|
# Edit a log
|
||||||
|
editLog(input: LogEditInput!): Log!
|
||||||
|
|
||||||
|
# Remove a log
|
||||||
|
removeLog(input: LogRemoveInput!): Log!
|
||||||
|
|
||||||
|
|
||||||
# Add a post
|
# Add a post
|
||||||
addPost(input: PostAddInput!): Post!
|
addPost(input: PostAddInput!): Post!
|
||||||
|
|
||||||
|
|||||||
@@ -94,4 +94,10 @@ input LogEditInput {
|
|||||||
|
|
||||||
# Open/close the log
|
# Open/close the log
|
||||||
open: Boolean
|
open: Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
# Input for removeLog mutation
|
||||||
|
input LogRemoveInput {
|
||||||
|
# The id of the log to remove
|
||||||
|
id: String!
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package logs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.aiterp.net/rpdata/api/internal/counter"
|
||||||
|
"git.aiterp.net/rpdata/api/model/channel"
|
||||||
|
"git.aiterp.net/rpdata/api/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Add creates a new Log
|
||||||
|
func Add(date time.Time, channelName, title, eventName, description string, open bool) (models.Log, error) {
|
||||||
|
nextID, err := counter.Next("auto_increment", "Log")
|
||||||
|
if err != nil {
|
||||||
|
return models.Log{}, errors.New("Failed to allocate short ID: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = channel.Ensure(channelName, open)
|
||||||
|
if err != nil {
|
||||||
|
return models.Log{}, errors.New("Failed to ensure channel: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
log := models.Log{
|
||||||
|
ID: makeLogID(date, channelName),
|
||||||
|
ShortID: "L" + strconv.Itoa(nextID),
|
||||||
|
Date: date,
|
||||||
|
ChannelName: channelName,
|
||||||
|
Title: title,
|
||||||
|
EventName: eventName,
|
||||||
|
Description: description,
|
||||||
|
Open: open,
|
||||||
|
CharacterIDs: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = collection.Insert(log)
|
||||||
|
if err != nil {
|
||||||
|
return models.Log{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return log, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package logs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.aiterp.net/rpdata/api/models"
|
||||||
|
"github.com/globalsign/mgo/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Edit sets a log's meta-data.
|
||||||
|
func Edit(log models.Log, title *string, event *string, description *string, open *bool) (models.Log, error) {
|
||||||
|
changes := bson.M{}
|
||||||
|
|
||||||
|
if title != nil && *title != log.Title {
|
||||||
|
changes["title"] = *title
|
||||||
|
log.Title = *title
|
||||||
|
}
|
||||||
|
if event != nil && *event != log.EventName {
|
||||||
|
changes["event"] = *event
|
||||||
|
log.EventName = *event
|
||||||
|
}
|
||||||
|
if description != nil && *description != log.Description {
|
||||||
|
changes["description"] = *description
|
||||||
|
log.Description = *description
|
||||||
|
}
|
||||||
|
if open != nil && *open != log.Open {
|
||||||
|
changes["open"] = *open
|
||||||
|
log.Open = *open
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(changes) == 0 {
|
||||||
|
return log, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := collection.UpdateId(log.ID, bson.M{"$set": changes})
|
||||||
|
if err != nil {
|
||||||
|
return models.Log{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return log, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package logs
|
||||||
|
|
||||||
|
import "git.aiterp.net/rpdata/api/models"
|
||||||
|
|
||||||
|
// Remove removes the log.
|
||||||
|
func Remove(log models.Log) (models.Log, error) {
|
||||||
|
err := collection.RemoveId(log.ID)
|
||||||
|
if err != nil {
|
||||||
|
return models.Log{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return log, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user