|
| 1 | +package gql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/pkg/errors" |
| 7 | + "github.com/satisfactorymodding/smr-api/util" |
| 8 | + "gopkg.in/go-playground/validator.v9" |
| 9 | + |
| 10 | + "github.com/satisfactorymodding/smr-api/db/postgres" |
| 11 | + "github.com/satisfactorymodding/smr-api/generated" |
| 12 | +) |
| 13 | + |
| 14 | +func (r *mutationResolver) CreateAnnouncement(ctx context.Context, announcement generated.NewAnnouncement) (*generated.Announcement, error) { |
| 15 | + wrapper, newCtx := WrapMutationTrace(ctx, "createAnnouncement") |
| 16 | + defer wrapper.end() |
| 17 | + |
| 18 | + val := ctx.Value(util.ContextValidator{}).(*validator.Validate) |
| 19 | + if err := val.Struct(&announcement); err != nil { |
| 20 | + return nil, errors.Wrap(err, "validation failed") |
| 21 | + } |
| 22 | + |
| 23 | + dbAnnouncement := &postgres.Announcement{ |
| 24 | + Message: announcement.Message, |
| 25 | + Importance: announcement.Importance, |
| 26 | + } |
| 27 | + |
| 28 | + resultAnnouncement, err := postgres.CreateAnnouncement(dbAnnouncement, &newCtx) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + return DBAnnouncementToGenerated(resultAnnouncement), nil |
| 33 | +} |
| 34 | + |
| 35 | +func (r *mutationResolver) DeleteAnnouncement(ctx context.Context, announcementID string) (bool, error) { |
| 36 | + wrapper, newCtx := WrapMutationTrace(ctx, "deleteAnnouncement") |
| 37 | + defer wrapper.end() |
| 38 | + |
| 39 | + dbAnnouncement := postgres.GetAnnouncementByID(announcementID, &newCtx) |
| 40 | + |
| 41 | + if dbAnnouncement == nil { |
| 42 | + return false, errors.New("announcement not found") |
| 43 | + } |
| 44 | + |
| 45 | + postgres.Delete(&dbAnnouncement, &newCtx) |
| 46 | + |
| 47 | + return true, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (r *mutationResolver) UpdateAnnouncement(ctx context.Context, announcementID string, announcement generated.UpdateAnnouncement) (*generated.Announcement, error) { |
| 51 | + wrapper, newCtx := WrapMutationTrace(ctx, "updateAnnouncement") |
| 52 | + defer wrapper.end() |
| 53 | + |
| 54 | + val := ctx.Value(util.ContextValidator{}).(*validator.Validate) |
| 55 | + if err := val.Struct(&announcement); err != nil { |
| 56 | + return nil, errors.Wrap(err, "validation failed") |
| 57 | + } |
| 58 | + |
| 59 | + dbAnnouncement := postgres.GetAnnouncementByID(announcementID, &newCtx) |
| 60 | + |
| 61 | + if dbAnnouncement == nil { |
| 62 | + return nil, errors.New("guide not found") |
| 63 | + } |
| 64 | + |
| 65 | + SetStringINNOE(announcement.Message, &dbAnnouncement.Message) |
| 66 | + SetStringINNOE(announcement.Importance, &dbAnnouncement.Importance) |
| 67 | + |
| 68 | + postgres.Save(&dbAnnouncement, &newCtx) |
| 69 | + |
| 70 | + return DBAnnouncementToGenerated(dbAnnouncement), nil |
| 71 | +} |
| 72 | + |
| 73 | +func (r *queryResolver) GetAnnouncement(ctx context.Context, announcementID string) (*generated.Announcement, error) { |
| 74 | + wrapper, newCtx := WrapQueryTrace(ctx, "getAnnouncement") |
| 75 | + defer wrapper.end() |
| 76 | + |
| 77 | + announcement := postgres.GetAnnouncementByID(announcementID, &newCtx) |
| 78 | + |
| 79 | + return DBAnnouncementToGenerated(announcement), nil |
| 80 | +} |
| 81 | + |
| 82 | +func (r *queryResolver) GetAnnouncements(ctx context.Context) ([]*generated.Announcement, error) { |
| 83 | + wrapper, newCtx := WrapQueryTrace(ctx, "getAnnouncements") |
| 84 | + defer wrapper.end() |
| 85 | + |
| 86 | + announcements := postgres.GetAnnouncements(&newCtx) |
| 87 | + |
| 88 | + return DBAnnouncementsToGeneratedSlice(announcements), nil |
| 89 | +} |
| 90 | + |
| 91 | +func (r *queryResolver) GetAnnouncementsByImportance(ctx context.Context, importance string) ([]*generated.Announcement, error) { |
| 92 | + wrapper, newCtx := WrapQueryTrace(ctx, "getAnnouncementsByImportance") |
| 93 | + defer wrapper.end() |
| 94 | + |
| 95 | + announcements := postgres.GetAnnouncementsByImportance(importance, &newCtx) |
| 96 | + |
| 97 | + return DBAnnouncementsToGeneratedSlice(announcements), nil |
| 98 | +} |
0 commit comments