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 a3d0914..7bb3527 100644 --- a/src/main/java/com/example/cosmos_serversb/models/Users.java +++ b/src/main/java/com/example/cosmos_serversb/models/Users.java @@ -1,13 +1,16 @@ package com.example.cosmos_serversb.models; +import java.util.List; import java.util.UUID; 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; +import org.hibernate.criterion.Restrictions; @Singleton public class Users { @@ -62,36 +65,91 @@ } 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"); + + Session session = sessionFactory.openSession(); + session.beginTransaction(); + //名前、uId、ユーザーuri、パスワード、アイコンイメージを保存 + User testUser = new User(name,getInstance().createUId(), "http://nitta-lab-8080/cosmos/users/{uId}.com",pw,iconImage); + session.save(testUser); + //出力 + List userResult = session.createQuery( "from User" ).list(); + for ( User user : (List) userResult ) { + System.out.println( "UserId (" + user.getuId() + ") : " + user.getUri() + ": " + user.getName()); + } + session.getTransaction().commit(); + session.close(); + // testUser.token.add(getInstance().createToken(testUser.uId)); // testUser.token.add(getInstance().createToken(testUser.uId)); 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"); + + Session session = sessionFactory.openSession(); + session.beginTransaction(); + List usertList = session.createQuery( "from User" ).list(); + for ( User user : (List) usertList ) { + if(user.getuId() == uId){ + user.setuId(uId); + user.setName(name); + user.setPw(pw); + if(iconImage != null) { + user.setIconUri(iconImage); + } + session.update(user); + break; + } + } + session.getTransaction().commit(); + session.close(); + // testUser.token.add(getInstance().createToken(testUser.uId)); // testUser.token.add(getInstance().createToken(testUser.uId)); - return testUser; + return null; } 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"); + + Session session = sessionFactory.openSession(); + session.beginTransaction(); + List usertList = session.createQuery( "from User" ).list(); + for ( User user : (List) usertList ) { + if (user.getuId() == uId) { + System.out.println( "name: " + user.getName() + "iconUri : " + user.getIconUri()); + break; + } + } + session.getTransaction().commit(); + session.close(); + // testUser.token.add(getInstance().createToken(testUser.uId)); // testUser.token.add(getInstance().createToken(testUser.uId)); - return testUser; + return null; } 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"); + User testUser = new User("test", getInstance().createUId(), "http://nitta-lab-8080/cosmos/users/{uId}.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)); return testUser; } public static DeleteResult deleteUser(String uId) { + + Session session = sessionFactory.openSession(); + session = sessionFactory.openSession(); + session.beginTransaction(); + Request deleteRequest = (Request) session.load(Request.class, uId); + if(deleteRequest != null){ + session.delete(deleteRequest); + System.out.println("massage: Already Deleted"); + } + session.getTransaction().commit(); + session.close(); + return DeleteResult.SUCCESS; }