-
Notifications
You must be signed in to change notification settings - Fork 8
Add announcement API #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
02e1424
feat: Add announcement API (unfinished)
Feyko ac50c09
feat: Add announcement API (unfinished)
Feyko 6487eeb
Merge remote-tracking branch 'Feyko/main' into main
Feyko ffe2044
fix: finishing announcements
Feyko b04fa77
fix(announcements): minor issues
Feyko da205ef
Merge branch 'main' into main
Vilsol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package postgres | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/patrickmn/go-cache" | ||
| "github.com/satisfactorymodding/smr-api/util" | ||
| ) | ||
|
|
||
| func CreateAnnouncement(announcement *Announcement, ctx *context.Context) (*Announcement, error) { | ||
| announcement.ID = util.GenerateUniqueID() | ||
| DBCtx(ctx).Create(&announcement) | ||
| return announcement, nil | ||
| } | ||
|
|
||
| func GetAnnouncementByID(announcementID string, ctx *context.Context) *Announcement { | ||
| cacheKey := "GetAnnouncementByID_" + announcementID | ||
|
|
||
| if announcement, ok := dbCache.Get(cacheKey); ok { | ||
| return announcement.(*Announcement) | ||
| } | ||
|
|
||
| var announcement Announcement | ||
| DBCtx(ctx).Find(&announcement, "id = ?", announcementID) | ||
|
|
||
| if announcement.ID == "" { | ||
| return nil | ||
| } | ||
|
|
||
| dbCache.Set(cacheKey, announcement, cache.DefaultExpiration) | ||
|
|
||
| return &announcement | ||
| } | ||
|
|
||
| func GetAnnouncements(ctx *context.Context) []Announcement { | ||
| cacheKey := "GetAnnouncements" | ||
|
|
||
| if announcements, ok := dbCache.Get(cacheKey); ok { | ||
| return announcements.([]Announcement) | ||
| } | ||
|
|
||
| var announcements []Announcement | ||
| DBCtx(ctx).Find(&announcements) | ||
|
|
||
| dbCache.Set(cacheKey, announcements, cache.DefaultExpiration) | ||
|
|
||
| return announcements | ||
| } | ||
|
|
||
| func GetAnnouncementsByImportance(importance string, ctx *context.Context) []Announcement { | ||
| cacheKey := "GetAnnouncementsByImportance_" + importance | ||
|
|
||
| if announcements, ok := dbCache.Get(cacheKey); ok { | ||
| return announcements.([]Announcement) | ||
| } | ||
|
|
||
| var announcements []Announcement | ||
| DBCtx(ctx).Find(&announcements, "importance = ?", importance) | ||
|
|
||
| dbCache.Set(cacheKey, announcements, cache.DefaultExpiration) | ||
|
|
||
| return announcements | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package gql | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/satisfactorymodding/smr-api/util" | ||
| "gopkg.in/go-playground/validator.v9" | ||
|
|
||
| "github.com/satisfactorymodding/smr-api/db/postgres" | ||
| "github.com/satisfactorymodding/smr-api/generated" | ||
| ) | ||
|
|
||
| func (r *mutationResolver) CreateAnnouncement(ctx context.Context, announcement generated.NewAnnouncement) (*generated.Announcement, error) { | ||
| wrapper, newCtx := WrapMutationTrace(ctx, "createAnnouncement") | ||
| defer wrapper.end() | ||
|
|
||
| val := ctx.Value(util.ContextValidator{}).(*validator.Validate) | ||
| if err := val.Struct(&announcement); err != nil { | ||
| return nil, errors.Wrap(err, "validation failed") | ||
| } | ||
|
|
||
| dbAnnouncement := &postgres.Announcement{ | ||
| Message: announcement.Message, | ||
| Importance: announcement.Importance, | ||
| } | ||
|
|
||
| resultAnnouncement, err := postgres.CreateAnnouncement(dbAnnouncement, &newCtx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return DBAnnouncementToGenerated(resultAnnouncement), nil | ||
| } | ||
|
|
||
| func (r *mutationResolver) DeleteAnnouncement(ctx context.Context, announcementID string) (bool, error) { | ||
| wrapper, newCtx := WrapMutationTrace(ctx, "deleteAnnouncement") | ||
| defer wrapper.end() | ||
|
|
||
| dbAnnouncement := postgres.GetAnnouncementByID(announcementID, &newCtx) | ||
|
|
||
| if dbAnnouncement == nil { | ||
| return false, errors.New("announcement not found") | ||
| } | ||
|
|
||
| postgres.Delete(&dbAnnouncement, &newCtx) | ||
|
|
||
| return true, nil | ||
| } | ||
|
|
||
| func (r *mutationResolver) UpdateAnnouncement(ctx context.Context, announcementID string, announcement generated.UpdateAnnouncement) (*generated.Announcement, error) { | ||
| wrapper, newCtx := WrapMutationTrace(ctx, "updateAnnouncement") | ||
| defer wrapper.end() | ||
|
|
||
| val := ctx.Value(util.ContextValidator{}).(*validator.Validate) | ||
| if err := val.Struct(&announcement); err != nil { | ||
| return nil, errors.Wrap(err, "validation failed") | ||
| } | ||
|
|
||
| dbAnnouncement := postgres.GetAnnouncementByID(announcementID, &newCtx) | ||
|
|
||
| if dbAnnouncement == nil { | ||
| return nil, errors.New("guide not found") | ||
| } | ||
|
|
||
| SetStringINNOE(announcement.Message, &dbAnnouncement.Message) | ||
| SetStringINNOE(announcement.Importance, &dbAnnouncement.Importance) | ||
|
|
||
| postgres.Save(&dbAnnouncement, &newCtx) | ||
|
|
||
| return DBAnnouncementToGenerated(dbAnnouncement), nil | ||
| } | ||
|
|
||
| func (r *queryResolver) GetAnnouncement(ctx context.Context, announcementID string) (*generated.Announcement, error) { | ||
| wrapper, newCtx := WrapQueryTrace(ctx, "getAnnouncement") | ||
| defer wrapper.end() | ||
|
|
||
| announcement := postgres.GetAnnouncementByID(announcementID, &newCtx) | ||
|
|
||
| return DBAnnouncementToGenerated(announcement), nil | ||
| } | ||
|
|
||
| func (r *queryResolver) GetAnnouncements(ctx context.Context) ([]*generated.Announcement, error) { | ||
| wrapper, newCtx := WrapQueryTrace(ctx, "getAnnouncements") | ||
| defer wrapper.end() | ||
|
|
||
| announcements := postgres.GetAnnouncements(&newCtx) | ||
|
|
||
| return DBAnnouncementsToGeneratedSlice(announcements), nil | ||
| } | ||
|
|
||
| func (r *queryResolver) GetAnnouncementsByImportance(ctx context.Context, importance string) ([]*generated.Announcement, error) { | ||
| wrapper, newCtx := WrapQueryTrace(ctx, "getAnnouncementsByImportance") | ||
| defer wrapper.end() | ||
|
|
||
| announcements := postgres.GetAnnouncementsByImportance(importance, &newCtx) | ||
|
|
||
| return DBAnnouncementsToGeneratedSlice(announcements), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| drop table if exists announcements |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| create table if not exists announcements | ||
| ( | ||
| id varchar(14) not null | ||
| constraint announcements_pkey primary key, | ||
| message text not null, | ||
| importance text not null, | ||
| created_at timestamp with time zone, | ||
| updated_at timestamp with time zone, | ||
| deleted_at timestamp with time zone | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| ### Types | ||
|
|
||
| scalar AnnouncementID | ||
|
|
||
| type Announcement { | ||
| id: AnnouncementID! | ||
| message: String! | ||
| importance: String! | ||
| } | ||
|
|
||
| ### Inputs | ||
|
|
||
| input NewAnnouncement { | ||
| message: String! | ||
| importance: String! | ||
| } | ||
|
|
||
| input UpdateAnnouncement { | ||
| message: String | ||
| importance: String | ||
| } | ||
|
|
||
| ### Queries | ||
|
|
||
| extend type Query { | ||
| getAnnouncement(announcementId: AnnouncementID!): Announcement | ||
| getAnnouncements: [Announcement!]! | ||
| getAnnouncementsByImportance(importance: String!): [Announcement!]! | ||
| } | ||
|
|
||
| ### Mutations | ||
|
|
||
| extend type Mutation { | ||
| createAnnouncement(announcement: NewAnnouncement!): Announcement @canEditAnnouncements @isLoggedIn | ||
| updateAnnouncement(announcementId: AnnouncementID!, announcement: UpdateAnnouncement!): Announcement! @canEditAnnouncements @isLoggedIn | ||
| deleteAnnouncement(announcementId: AnnouncementID!): Boolean! @canEditAnnouncements @isLoggedIn | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.