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.

74 lines
1.8 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) EditLog(ctx context.Context, input graphcore.LogEditInput) (*models.Log, error) {
  49. update := models.LogUpdate{
  50. Open: input.Open,
  51. Description: input.Description,
  52. EventName: input.Event,
  53. Title: input.Title,
  54. }
  55. return r.s.Logs.Update(ctx, input.ID, update)
  56. }
  57. func (r *mutationResolver) RemoveLog(ctx context.Context, input graphcore.LogRemoveInput) (*models.Log, error) {
  58. return r.s.Logs.Delete(ctx, input.ID)
  59. }