diff --git a/src/main/java/com/example/cosmos_serversb/models/Users.java b/src/main/java/com/example/cosmos_serversb/models/Users.java index 8ccc4c5..18a6cd2 100644 --- a/src/main/java/com/example/cosmos_serversb/models/Users.java +++ b/src/main/java/com/example/cosmos_serversb/models/Users.java @@ -4,16 +4,19 @@ import javax.inject.Singleton; import com.example.cosmos_serversb.entities.*; +import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; + @Singleton public class Users { private static Users theInstance = null; private static SessionFactory sessionFactory; - + private static String baseURI = "http://nitta-lab-www.is.konan-u.ac.jp/"; + private static String AppName = "cosmos"; public static void setUp() { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() @@ -50,10 +53,16 @@ return theInstance; } - public static String createToken(String uId) { + public static Token createToken(String uId) { //Tokenを作成しuIdと関連付けしDBに保存し作成したTokenを返す String token = UUID.randomUUID().toString(); - return token; + Token testToken =new Token(token, uId); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(testToken); + session.getTransaction().commit(); + session.close(); + return testToken; } public static String createUId() { @@ -62,33 +71,32 @@ return uId; } - public static User createUser(String name, String pw, String iconImage) { - User testUser = new User("test", getInstance().createUId(), "http://test.com", "testpw", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); -// testUser.token.add(getInstance().createToken(testUser.uId)); -// testUser.token.add(getInstance().createToken(testUser.uId)); + public static User createUser(String name, String iconImage) { + String uId = getInstance().createUId(); + String uri = baseURI + AppName +"/users/" +uId; + User testUser = new User("test", uId, uri, "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(testUser); + session.getTransaction().commit(); + session.close(); return testUser; } public static User setUser(String uId, String name, String pw, String iconImage) { - User testUser = new User("test", getInstance().createUId(), "http://test.com", "testpw", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); -// testUser.token.add(getInstance().createToken(testUser.uId)); -// testUser.token.add(getInstance().createToken(testUser.uId)); + User testUser = new User("test", getInstance().createUId(), "http://test.com", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); return testUser; } public static User getUserById(String uId) { //与えられたuIdのUser情報を返す - User testUser = new User("test", getInstance().createUId(), "http://test.com", "testpw", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); -// testUser.token.add(getInstance().createToken(testUser.uId)); -// testUser.token.add(getInstance().createToken(testUser.uId)); + User testUser = new User("test", getInstance().createUId(), "http://test.com", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); return testUser; } public static User getUserByToken(String token) { //与えられたTokenに紐づくUser情報を返す - User testUser = new User("test", getInstance().createUId(), "http://test.com", "testpw", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); -// testUser.token.add(getInstance().createToken(testUser.uId)); -// testUser.token.add(getInstance().createToken(testUser.uId)); + User testUser = new User("test", getInstance().createUId(), "http://test.com","http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); return testUser; } @@ -97,7 +105,7 @@ } public static Token login(String uId) { - Token testtoken = new Token(getInstance().createToken(uId)); + Token testtoken = getInstance().createToken(uId); //tokenを作成後値を返す return testtoken;//testToken }