func TestSaveUser(t *testing.T) { db := newTestDynamoDB(t) // create a user u := models.User{ Email: "test@user", Name: "Test User", JWTToken: "345565", } if err := db.SaveUser(&u); err != nil { t.Fatal("cannot create user") } else if u.ID == "" { t.Fatal("user was not assigned a unique ID") } // update the user u.Email = "test2@user" if err := db.SaveUser(&u); err != nil { t.Fatal("cannot create user") } ur, ok := db.GetByID(u.ID) if !ok { t.Fatal("coulnd't get user") } compareUsersForEquality(t, ur, &u) }
// SaveUser creates or updates a user. Upon creation, users are assigned a unique ID. func (db *DynamoDB) SaveUser(u *models.User) error { if u.ID == "" { id, err := shortid.ID(64) if err != nil { return err } u.ID = id } item, err := dynamodbattribute.MarshalMap(u) if err != nil { return err } _, err = db.DB.PutItem(&dynamodb.PutItemInput{ TableName: aws.String("users"), Item: item, }) if err != nil { return err } return nil }
// SaveUser save or updates a user object in the database. func (db UserDB) SaveUser(u *models.User) error { if u.ID == "" { u.ID = strconv.Itoa(len(db.ids) + 1) } db.ids[u.ID] = u db.emails[u.Email] = u return nil }