Gisle Aune
6 years ago
6 changed files with 168 additions and 0 deletions
-
57graph2/queries/log.go
-
10graph2/schema/root.gql
-
6graph2/schema/types/Log.gql
-
43models/logs/add.go
-
39models/logs/edit.go
-
13models/logs/remove.go
@ -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 |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue