Skip to content

Instantly share code, notes, and snippets.

@enterprisesaas
Created December 19, 2013 14:41
Show Gist options
  • Select an option

  • Save enterprisesaas/8040118 to your computer and use it in GitHub Desktop.

Select an option

Save enterprisesaas/8040118 to your computer and use it in GitHub Desktop.
Data access object representing a User
package concepts;
import model.User;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.LinkedList;
import java.util.List;
public class UserMongoDao {
private MongoOperations mongoOperations;
public void setMongoOperations(MongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}
public void create(User user) {
if (null != user) {
// Make sure we don't duplicate this user
Query findUser = new Query(Criteria.where("email").is(user.getEmail()));
User comparisonUser = mongoOperations.findOne(findUser, User.class);
if (comparisonUser == null) {
// create the new user, it's not a duplicate
// Persist
mongoOperations.save(user);
System.out.printf("Creating user with e-mail address - %s%n", user.getEmail());
} else {
// this is a duplicate, note it and move on.
System.out.println("Not creating, this is a duplicate, note it and move on.");
}
}
}
public List<User> find() {
List<User> retList = new LinkedList<>();
List<User> usersList = mongoOperations.findAll(User.class);
if (usersList.isEmpty()) {
System.out.println("There were no user's found.");
} else {
retList.addAll(usersList);
}
return retList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment