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.

84 lines
2.2 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. return r.s.Logs.Import(ctx, input.Importer, date, tz, input.ChannelName, input.Data)
  47. }
  48. func (r *mutationResolver) SplitLog(ctx context.Context, input graphcore.LogSplitInput) (*models.Log, error) {
  49. return r.s.Logs.SplitLog(ctx, input.LogID, input.StartPostID)
  50. }
  51. func (r *mutationResolver) MergeLog(ctx context.Context, input graphcore.LogMergeInput) (*models.Log, error) {
  52. removeAfter := input.RemoveAfter != nil && *input.RemoveAfter
  53. return r.s.Logs.MergeLogs(ctx, input.TargetLogID, input.SourceLogID, removeAfter)
  54. }
  55. func (r *mutationResolver) EditLog(ctx context.Context, input graphcore.LogEditInput) (*models.Log, error) {
  56. update := models.LogUpdate{
  57. Open: input.Open,
  58. Description: input.Description,
  59. EventName: input.Event,
  60. Title: input.Title,
  61. }
  62. return r.s.Logs.Update(ctx, input.ID, update)
  63. }
  64. func (r *mutationResolver) RemoveLog(ctx context.Context, input graphcore.LogRemoveInput) (*models.Log, error) {
  65. return r.s.Logs.Delete(ctx, input.ID)
  66. }