Skip to content

Commit

Permalink
modify checking of obj to only include endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
will-lol committed Mar 5, 2024
1 parent 99d55d2 commit b019c0a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
15 changes: 4 additions & 11 deletions dependencies/db/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type db[T any] struct {

type DB[T any] interface {
DeleteObject(T) error
DoesObjExist(T) (*bool, error)
DoesKeyExist(map[string]types.AttributeValue) (*bool, error)
GetObjects() (*[]T, error)
SaveObject(T) (error)
}
Expand Down Expand Up @@ -49,19 +49,12 @@ func getTableName() (name string, err error) {
return name, err
}

// DoesObjExist returns whether an object found in the DB according to the given searchObj or an error. The searchObj is the desired object in the DB. It does not need to be complete, but should include the primary key in the database.
func (c db[T]) DoesObjExist(searchObj T) (*bool, error) {
slog.Default().Debug("marshalling", "obj", searchObj)
attributeValueObj, err := attributevalue.MarshalMap(searchObj)
if err != nil {
return nil, err
}
slog.Default().Debug("marshalled", "obj", attributeValueObj)

// DoesObjExist returns whether an object found in the DB according to the given searchObj or an error. The searchObj is the desired object in the DB. It does not need to be complete, but should include the primary key in the database.
func (c db[T]) DoesKeyExist(searchObj map[string]types.AttributeValue) (*bool, error) {
slog.Default().Debug("getting item")
res, err := c.DynamoDbClient.GetItem(context.TODO(), &dynamodb.GetItemInput{
TableName: aws.String(c.TableName),
Key: attributeValueObj,
Key: searchObj,
})
if err != nil {
return nil, err
Expand Down
8 changes: 7 additions & 1 deletion services/notifications/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync"

webpush "github.com/SherClockHolmes/webpush-go"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/will-lol/personalWebsiteAwesome/dependencies/db"
)

Expand Down Expand Up @@ -191,7 +193,11 @@ func (n notificationsService) Subscribe(sub Subscription) error {
n.Log.Debug("url valid")

n.Log.Debug("checking if obj exists")
alreadyExists, err := n.db.DoesObjExist(sub)
alreadyExists, err := n.db.DoesKeyExist(map[string]types.AttributeValue{
"Endpoint": &types.AttributeValueMemberS{
Value: sub.Endpoint,
},
})
if err != nil {
return err
}
Expand Down

0 comments on commit b019c0a

Please sign in to comment.