-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add announcement API (unfinished) * feat: Add announcement API (unfinished) * fix: finishing announcements + linting * fix(announcements): minor issues Co-authored-by: Vilsol <[email protected]>
- Loading branch information
Showing
11 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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