Hi
MongoDB team wrote code that get the user by email after inserting a user document. I think this is redundant because MongoDB automatically reads it after inserting it and populate the _Id field. Why should I read it again?
public async Task<UserResponse> AddUserAsync(string name, string email, string password,
CancellationToken cancellationToken = default)
{
try
{
var user = new User();
user.Name = name;
user.Email = email;
user.HashedPassword = PasswordHashOMatic.Hash(password);
InsertOneOptions insertOneOptions = new InsertOneOptions();
await _usersCollection.WithWriteConcern(WriteConcern.WMajority).InsertOneAsync(user, insertOneOptions, cancellationToken);
// I think the line below is redundant. Isn't it?
var newUser = GetUserAsync(user.Email, cancellationToken).Result;
return new UserResponse(newUser);
}
catch (Exception ex)
{
return ex.Message.StartsWith("MongoError: E11000 duplicate key error")
? new UserResponse(false, "A user with the given email already exists.")
: new UserResponse(false, ex.Message);
}
}
Thanks