|
@ -0,0 +1,52 @@ |
|
|
|
|
|
package de.fd.fh.server.user; |
|
|
|
|
|
|
|
|
|
|
|
import com.mongodb.MongoClient; |
|
|
|
|
|
import com.mongodb.WriteResult; |
|
|
|
|
|
import dev.morphia.Datastore; |
|
|
|
|
|
import dev.morphia.Key; |
|
|
|
|
|
import dev.morphia.Morphia; |
|
|
|
|
|
|
|
|
|
|
|
public class UserRepository { |
|
|
|
|
|
|
|
|
|
|
|
private final Datastore datastore; |
|
|
|
|
|
|
|
|
|
|
|
public UserRepository() |
|
|
|
|
|
{ |
|
|
|
|
|
System.out.println("UserRepo"); |
|
|
|
|
|
final Morphia morphia = new Morphia(); |
|
|
|
|
|
|
|
|
|
|
|
morphia.mapPackage("de.fd.fh.server.user"); |
|
|
|
|
|
|
|
|
|
|
|
this.datastore = morphia.createDatastore(new MongoClient(), "smartwarfare"); |
|
|
|
|
|
datastore.ensureIndexes(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
UserRepository(Datastore datastore) |
|
|
|
|
|
{ |
|
|
|
|
|
this.datastore = datastore; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Key<User> save(final User user) |
|
|
|
|
|
{ |
|
|
|
|
|
return datastore.save(user); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public User findUserById(final UserId userId) |
|
|
|
|
|
{ |
|
|
|
|
|
return datastore.createQuery(User.class) |
|
|
|
|
|
.field("_id.identifier").equal(userId.getIdentifier()) |
|
|
|
|
|
.first(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public User findUserByName(final String name) |
|
|
|
|
|
{ |
|
|
|
|
|
return datastore.createQuery(User.class) |
|
|
|
|
|
.field("name").equal(name) |
|
|
|
|
|
.first(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
WriteResult deleteUserById(final UserId userId) |
|
|
|
|
|
{ |
|
|
|
|
|
return datastore.delete(findUserById(userId)); |
|
|
|
|
|
} |
|
|
|
|
|
} |