GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4 KiB

  1. package resolvers
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "git.aiterp.net/rpdata/api/graph2/graphcore"
  7. "git.aiterp.net/rpdata/api/models"
  8. )
  9. // Queries
  10. func (r *queryResolver) Log(ctx context.Context, id string) (*models.Log, error) {
  11. return r.s.Logs.Find(ctx, id)
  12. }
  13. func (r *queryResolver) Logs(ctx context.Context, filter *models.LogFilter) ([]*models.Log, error) {
  14. return r.s.Logs.List(ctx, filter)
  15. }
  16. // Mutations
  17. func (r *mutationResolver) AddLog(ctx context.Context, input graphcore.LogAddInput) (*models.Log, error) {
  18. open := input.Open != nil && *input.Open == true
  19. title := ""
  20. if input.Title != nil {
  21. title = *input.Title
  22. }
  23. event := ""
  24. if input.Event != nil {
  25. event = *input.Event
  26. }
  27. description := ""
  28. if input.Description != nil {
  29. description = *input.Description
  30. }
  31. return r.s.Logs.Create(ctx, title, description, input.Channel, event, open)
  32. }
  33. func (r *mutationResolver) ImportLog(ctx context.Context, input graphcore.LogImportInput) ([]*models.Log, error) {
  34. date := time.Time{}
  35. if input.Date != nil {
  36. date = *input.Date
  37. }
  38. tz := time.UTC
  39. if input.Timezone != nil {
  40. parsedTZ, err := time.LoadLocation(*input.Timezone)
  41. if err != nil {
  42. return nil, errors.New("Unknown timezone: " + *input.Timezone)
  43. }
  44. tz = parsedTZ
  45. }
  46. sessionThreshold := time.Hour * 12
  47. if input.SessionThresholdMs != nil {
  48. sessionThreshold = time.Duration(*input.SessionThresholdMs) * time.Millisecond
  49. }
  50. return r.s.Logs.Import(ctx, input.Importer, date, tz, input.ChannelName, sessionThreshold, input.Data)
  51. }
  52. func (r *mutationResolver) SplitLog(ctx context.Context, input graphcore.LogSplitInput) (*models.Log, error) {
  53. return r.s.Logs.SplitLog(ctx, input.LogID, input.StartPostID)
  54. }
  55. func (r *mutationResolver) MergeLog(ctx context.Context, input graphcore.LogMergeInput) (*models.Log, error) {
  56. removeAfter := input.RemoveAfter != nil && *input.RemoveAfter
  57. return r.s.Logs.MergeLogs(ctx, input.TargetLogID, input.SourceLogID, removeAfter)
  58. }
  59. func (r *mutationResolver) EditLog(ctx context.Context, input graphcore.LogEditInput) (*models.Log, error) {
  60. update := models.LogUpdate{
  61. Open: input.Open,
  62. Description: input.Description,
  63. EventName: input.Event,
  64. Title: input.Title,
  65. }
  66. return r.s.Logs.Update(ctx, input.ID, update)
  67. }
  68. func (r *mutationResolver) RemoveLog(ctx context.Context, input graphcore.LogRemoveInput) (*models.Log, error) {
  69. return r.s.Logs.Delete(ctx, input.ID)
  70. }