Exemple #1
0
// Create inserts a new user into the database.
func (usersService) Create(c *app.Context, u *models.User) ([]app.Invalid, error) {
	log.Println(c.SessionID, ": services : Users : Create : Started")

	now := time.Now()

	u.UserID = bson.NewObjectId().Hex()
	u.DateCreated = &now
	u.DateModified = &now
	for _, ua := range u.Addresses {
		ua.DateCreated = &now
		ua.DateModified = &now
	}

	if v, err := u.Validate(); err != nil {
		log.Println(c.SessionID, ": services : Users : Create : Completed : ERROR :", err)
		return v, app.ErrValidation
	}

	f := func(collection *mgo.Collection) error {
		log.Printf("%s : services : Users : Create : MGO :\n\ndb.users.insert(%s)\n\n", c.SessionID, app.Query(u))
		return collection.Insert(u)
	}

	if err := app.ExecuteDB(c.Session, usersCollection, f); err != nil {
		log.Println(c.SessionID, ": services : Users : Create : Completed : ERROR :", err)
		return nil, err
	}

	log.Println(c.SessionID, ": services : Users : Create : Completed")
	return nil, nil
}
Exemple #2
0
// Update replaces a user document in the database.
func (usersService) Update(c *app.Context, userID string, u *models.User) ([]app.Invalid, error) {
	log.Println(c.SessionID, ": services : Users : Update : Started")

	if v, err := u.Validate(); err != nil {
		log.Println(c.SessionID, ": services : Users : Update : Completed : ERROR :", err)
		return v, app.ErrValidation
	}

	if u.UserID == "" {
		u.UserID = userID
	}

	if userID != u.UserID {
		log.Println(c.SessionID, ": services : Users : Update : Completed : ERROR :", app.ErrValidation)
		return []app.Invalid{{Fld: "UserID", Err: "Specified UserID does not match user value."}}, app.ErrValidation
	}

	// This is a bug that needs to be fixed.
	// I am re-writing the dates so the tests pass. :(
	now := time.Now()
	u.DateCreated = &now
	u.DateModified = &now
	for _, ua := range u.Addresses {
		ua.DateCreated = &now
		ua.DateModified = &now
	}

	f := func(collection *mgo.Collection) error {
		q := bson.M{"user_id": u.UserID}
		log.Printf("%s : services : Users : Update : MGO :\n\ndb.users.update(%s, %s)\n\n", c.SessionID, app.Query(q), app.Query(u))
		return collection.Update(q, u)
	}

	if err := app.ExecuteDB(c.Session, usersCollection, f); err != nil {
		log.Println(c.SessionID, ": services : Users : Create : Completed : ERROR :", err)
		return nil, err
	}

	log.Println(c.SessionID, ": services : Users : Update : Completed")
	return nil, nil
}
Exemple #3
0
// usersCreateRetrieveRemove validates a user can be created, retrieved and
// then removed from the system.
func usersCreateRetrieveRemove(t *testing.T, c *app.Context) {
	now := time.Now()

	u := models.User{
		UserType:     1,
		FirstName:    "Bill",
		LastName:     "Kennedy",
		Email:        "*****@*****.**",
		Company:      "Ardan Labs",
		DateModified: &now,
		DateCreated:  &now,
		Addresses: []models.UserAddress{
			{
				Type:         1,
				LineOne:      "12973 SW 112th ST",
				LineTwo:      "Suite 153",
				City:         "Miami",
				State:        "FL",
				Zipcode:      "33172",
				Phone:        "305-527-3353",
				DateModified: &now,
				DateCreated:  &now,
			},
		},
	}

	t.Log("Given the need to add a new user, retrieve and remove that user from the system.")
	{
		if _, err := u.Validate(); err != nil {
			t.Fatal("\tShould be able to validate the user data.", tests.Failed)
		}
		t.Log("\tShould be able to validate the user data.", tests.Succeed)

		if _, err := services.Users.Create(c, &u); err != nil {
			t.Fatal("\tShould be able to create a user in the system.", tests.Failed)
		}
		t.Log("\tShould be able to create a user in the system.", tests.Succeed)

		if u.UserID == "" {
			t.Fatal("\tShould have an UserID for the user.", tests.Failed)
		}
		t.Log("\tShould have an UserID for the user.", tests.Succeed)

		ur, err := services.Users.Retrieve(c, u.UserID)
		if err != nil {
			t.Fatal("\tShould be able to retrieve the user back from the system.", tests.Failed)
		}
		t.Log("\tShould be able to retrieve the user back from the system.", tests.Succeed)

		if _, err := u.Compare(ur); err != nil {
			t.Fatal("\tShould find both the original and retrieved value are identical.", tests.Failed)
		}
		t.Log("\tShould find both the original and retrieved value are identical.", tests.Succeed)

		if ur == nil || u.UserID != ur.UserID {
			t.Fatal("\tShould have a match between the created user and the one retrieved.", tests.Failed)
		}
		t.Log("\tShould have a match between the created user and the one retrieved.", tests.Succeed)

		if err := services.Users.Delete(c, u.UserID); err != nil {
			t.Fatal("\tShould be able to remove the user from the system.", tests.Failed)
		}
		t.Log("\tShould be able to remove the user from the system", tests.Succeed)

		if _, err := services.Users.Retrieve(c, u.UserID); err == nil {
			t.Fatal("\tShould NOT be able to retrieve the user back from the system.", tests.Failed)
		}
		t.Log("\tShould NOT be able to retrieve the user back from the system.", tests.Succeed)
	}
}