Skip to content

Commit

Permalink
Merge pull request #207 from pixlise/development
Browse files Browse the repository at this point in the history
User Expression Display Settings
  • Loading branch information
RyanStonebraker authored Apr 2, 2024
2 parents 40c1a46 + 8aebab0 commit 9525882
Show file tree
Hide file tree
Showing 8 changed files with 2,093 additions and 1,533 deletions.
1 change: 1 addition & 0 deletions api/dbCollections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const TagsName = "tags"
const UserGroupJoinRequestsName = "userGroupJoinRequests"
const UserGroupsName = "userGroups"
const UserROIDisplaySettings = "userROIDisplaySettings"
const UserExpressionDisplaySettings = "userExpressionDisplaySettings"
const UsersName = "users"
const ViewStatesName = "viewStates"
const WidgetDataName = "widgetData"
Expand Down
1 change: 1 addition & 0 deletions api/dbCollections/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func InitCollections(db *mongo.Database, iLog logger.ILogger, environment string
RegionsOfInterestName,
ScreenConfigurationName,
UserROIDisplaySettings,
UserExpressionDisplaySettings,
WidgetDataName,
}

Expand Down
4 changes: 2 additions & 2 deletions api/ws/handlers/expression-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func HandleExpressionGroupGetReq(req *protos.ExpressionGroupGetReq, hctx wsHelpe
}

func validateExpressionGroup(egroup *protos.ExpressionGroup) error {
if err := wsHelpers.CheckStringField(&egroup.Name, "Name", 1, 50); err != nil {
if err := wsHelpers.CheckStringField(&egroup.Name, "Name", 1, 100); err != nil {
return err
}
if err := wsHelpers.CheckStringField(&egroup.Description, "Description", 0, wsHelpers.DescriptionFieldMaxLength); err != nil {
Expand All @@ -74,7 +74,7 @@ func validateExpressionGroup(egroup *protos.ExpressionGroup) error {
if err := wsHelpers.CheckFieldLength(egroup.Tags, "Tags", 0, wsHelpers.TagListMaxLength); err != nil {
return err
}
if err := wsHelpers.CheckFieldLength(egroup.GroupItems, "GroupItems", 2, 5); err != nil {
if err := wsHelpers.CheckFieldLength(egroup.GroupItems, "GroupItems", 2, 10); err != nil {
return err
}

Expand Down
108 changes: 108 additions & 0 deletions api/ws/handlers/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,111 @@ func HandleExpressionWriteExecStatReq(req *protos.ExpressionWriteExecStatReq, hc

return &protos.ExpressionWriteExecStatResp{}, nil
}

func formExpressionDisplaySettingsID(user *protos.UserInfo, expressionId string) string {
return user.Id + "-" + expressionId
}

func HandleExpressionDisplaySettingsGetReq(req *protos.ExpressionDisplaySettingsGetReq, hctx wsHelpers.HandlerContext) (*protos.ExpressionDisplaySettingsGetResp, error) {
// Validate request
if len(req.Id) <= 0 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("Expression ID must be specified"))
}

if hctx.SessUser.User.Id == "" {
return nil, errorwithstatus.MakeBadRequestError(errors.New("User must be logged in"))
}

displaySettingsId := formExpressionDisplaySettingsID(hctx.SessUser.User, req.Id)

// Get the display settings
displaySettings := &protos.ExpressionDisplaySettings{}
err := hctx.Svcs.MongoDB.Collection(dbCollections.UserExpressionDisplaySettings).FindOne(context.Background(), bson.M{"_id": displaySettingsId}).Decode(&displaySettings)
if err != nil {
// We don't care about errors. If it doesn't exist, we just return a blank one
return &protos.ExpressionDisplaySettingsGetResp{
DisplaySettings: &protos.ExpressionDisplaySettings{Id: req.Id},
}, nil
}

// Set the ID back before returning it
displaySettings.Id = req.Id
return &protos.ExpressionDisplaySettingsGetResp{
DisplaySettings: displaySettings,
}, nil
}

func HandleExpressionDisplaySettingsWriteReq(req *protos.ExpressionDisplaySettingsWriteReq, hctx wsHelpers.HandlerContext) (*protos.ExpressionDisplaySettingsWriteResp, error) {
if len(req.Id) <= 0 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("Expression ID must be specified"))
}

if req.DisplaySettings == nil {
return nil, errorwithstatus.MakeBadRequestError(errors.New("DisplaySettings must be specified"))
}

if hctx.SessUser.User.Id == "" {
return nil, errorwithstatus.MakeBadRequestError(errors.New("User must be logged in"))
}

displaySettingsId := formExpressionDisplaySettingsID(hctx.SessUser.User, req.Id)

// Check if the display settings already exist
filter := bson.M{"_id": displaySettingsId}
opts := options.Find().SetProjection(bson.M{"_id": true})
cursor, err := hctx.Svcs.MongoDB.Collection(dbCollections.UserExpressionDisplaySettings).Find(context.TODO(), filter, opts)
if err != nil {
return nil, err
}

existingIds := []*IdOnly{}
err = cursor.All(context.TODO(), &existingIds)
if err != nil {
return nil, err
}

// Update the display settings
ctx := context.TODO()
sess, err := hctx.Svcs.MongoDB.Client().StartSession()
if err != nil {
return nil, err
}
defer sess.EndSession(ctx)

wc := writeconcern.New(writeconcern.WMajority())
rc := readconcern.Snapshot()
txnOpts := options.Transaction().SetWriteConcern(wc).SetReadConcern(rc)

callback := func(sessCtx mongo.SessionContext) (interface{}, error) {
var err error

if len(existingIds) > 0 {
_, err = hctx.Svcs.MongoDB.Collection(dbCollections.UserExpressionDisplaySettings).UpdateByID(sessCtx, displaySettingsId, bson.D{{
Key: "$set",
Value: bson.D{
{Key: "colourRamp", Value: req.DisplaySettings.ColourRamp},
},
}})
} else {
_, err = hctx.Svcs.MongoDB.Collection(dbCollections.UserExpressionDisplaySettings).InsertOne(sessCtx, &protos.ExpressionDisplaySettings{
Id: displaySettingsId,
ColourRamp: req.DisplaySettings.ColourRamp,
})
}

if err != nil {
return nil, err
}

return nil, nil
}

_, err = sess.WithTransaction(ctx, callback, txnOpts)
if err != nil {
return nil, err
}

return &protos.ExpressionDisplaySettingsWriteResp{
DisplaySettings: req.DisplaySettings,
}, nil
}
2 changes: 1 addition & 1 deletion data-formats
Loading

0 comments on commit 9525882

Please sign in to comment.