Commit e1ed874940a01f1b124bb8f1d2ba8249e6f23bd5
1 parent
ef4eb263
save before merge
Showing
6 changed files
with
54 additions
and
18 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java
... | ... | @@ -67,7 +67,8 @@ public class RightRepositoryJdbc implements RightRepository{ |
67 | 67 | ResultSet rs = null; |
68 | 68 | try { |
69 | 69 | stmt = dataSource.getConnection().createStatement(); |
70 | - stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + right.getName() + "\')", | |
70 | + | |
71 | + stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C, PARENT) VALUES (\'" + right.getName() + "\', \'" + right.getParent().getId() + "\')", | |
71 | 72 | Statement.RETURN_GENERATED_KEYS); |
72 | 73 | rs = stmt.getGeneratedKeys(); |
73 | 74 | if (rs.next()) { | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
... | ... | @@ -5,23 +5,42 @@ import fr.plil.sio.persistence.api.RightService; |
5 | 5 | import org.springframework.stereotype.Service; |
6 | 6 | |
7 | 7 | import java.util.List; |
8 | +import org.springframework.beans.factory.annotation.Autowired; | |
8 | 9 | |
9 | 10 | @Service |
10 | 11 | public class RightServiceJdbc implements RightService { |
11 | 12 | |
13 | + @Autowired | |
14 | + private GroupRepository groupRepository; | |
15 | + private RightRepository rightRepository; | |
16 | + | |
12 | 17 | @Override |
13 | 18 | public Right create(String name) { |
14 | 19 | if(name==null){ |
15 | 20 | throw new IllegalArgumentException("name cannot be null"); |
16 | 21 | } |
17 | - else{ | |
18 | - return null; | |
22 | + Right right = new Right(); | |
23 | + right.setName(name); | |
24 | + if(right==null){ | |
25 | + throw new IllegalArgumentException("name cannot be null"); | |
19 | 26 | } |
27 | + rightRepository.save(right); | |
28 | + return right; | |
20 | 29 | } |
21 | 30 | |
22 | 31 | @Override |
23 | 32 | public Right create(String name, Right parent) { |
24 | - return null; | |
33 | + if(name==null || parent.getId()==null){ | |
34 | + throw new IllegalArgumentException("name cannot be null"); | |
35 | + } | |
36 | + Right right = new Right(); | |
37 | + right.setName(name); | |
38 | + right.setParent(parent); | |
39 | + if(right==null){ | |
40 | + throw new IllegalArgumentException("name cannot be null"); | |
41 | + } | |
42 | + rightRepository.save(right); | |
43 | + return right; | |
25 | 44 | } |
26 | 45 | |
27 | 46 | @Override |
... | ... | @@ -29,9 +48,9 @@ public class RightServiceJdbc implements RightService { |
29 | 48 | if(right==null){ |
30 | 49 | throw new IllegalArgumentException("name cannot be null"); |
31 | 50 | } |
32 | - else{ | |
33 | - return true; | |
34 | - } | |
51 | + | |
52 | + rightRepository.delete(right.getId()); | |
53 | + return true; | |
35 | 54 | } |
36 | 55 | |
37 | 56 | @Override | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java
... | ... | @@ -36,10 +36,9 @@ public class UserRepositoryJdbc implements UserRepository{ |
36 | 36 | User user = new User(); |
37 | 37 | user.setId(rs.getLong("T.USER_ID")); |
38 | 38 | user.setName(rs.getString("T.NAME_U")); |
39 | - Group group = new Group(); | |
40 | - GroupRepositoryJdbc g = new GroupRepositoryJdbc(); | |
41 | - group = g.findByName(rs.getString("G.NAME_C")); | |
42 | - user.setGroup(group); | |
39 | +// Group group = new Group(); | |
40 | +// group = g.findByName(rs.getString("G.NAME_C")); | |
41 | +// user.setGroup(group); | |
43 | 42 | return user; |
44 | 43 | } else { |
45 | 44 | logger.debug("not found " + name); | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java
... | ... | @@ -16,7 +16,7 @@ public class UserServiceJdbc implements UserService { |
16 | 16 | |
17 | 17 | @Override |
18 | 18 | public User create(String name, String groupName) { |
19 | - if(name==null){ | |
19 | + if(name==null || groupName==null){ | |
20 | 20 | throw new IllegalArgumentException("name cannot be null"); |
21 | 21 | } |
22 | 22 | User user = userRepository.findByName(name); |
... | ... | @@ -25,7 +25,9 @@ public class UserServiceJdbc implements UserService { |
25 | 25 | } |
26 | 26 | user = new User(); |
27 | 27 | user.setName(name); |
28 | - Group group = groupRepository.findByName(groupName); | |
28 | + Group group = new Group(); | |
29 | + group.setName(groupName); | |
30 | +// Group group = groupRepository.findByName(groupName); | |
29 | 31 | userRepository.save(user, group); |
30 | 32 | return user; |
31 | 33 | } | ... | ... |
src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java
... | ... | @@ -13,8 +13,17 @@ public abstract class AbstractServiceSupport { |
13 | 13 | |
14 | 14 | private static final String create_table_group = "CREATE TABLE GROUP_T (GROUP_ID INT NOT NULL AUTO_INCREMENT, " + |
15 | 15 | "NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID))"; |
16 | - | |
16 | + | |
17 | + private static final String create_table_right = "CREATE TABLE RIGHT_T (RIGHT_ID INT NOT NULL AUTO_INCREMENT, " + | |
18 | + "NAME_R VARCHAR(50) UNIQUE NOT NULL, PARENT INT, PRIMARY KEY(RIGHT_ID))"; | |
19 | + | |
20 | + private static final String create_table_user = "CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, " + | |
21 | + "NAME_U VARCHAR(50) UNIQUE NOT NULL, GROUP_U INT, PRIMARY KEY(USER_ID), " + | |
22 | + "FOREIGN KEY(GROUP_U) REFERENCES GROUP_T(GROUP_ID))"; | |
23 | + | |
17 | 24 | private static final String drop_table_group = "DROP TABLE GROUP_T"; |
25 | + private static final String drop_table_right = "DROP TABLE RIGHT_T"; | |
26 | + private static final String drop_table_user = "DROP TABLE USER_T"; | |
18 | 27 | |
19 | 28 | @Autowired |
20 | 29 | private DataSource dataSource; |
... | ... | @@ -27,6 +36,8 @@ public abstract class AbstractServiceSupport { |
27 | 36 | public void createTables() throws SQLException { |
28 | 37 | openConnection(); |
29 | 38 | stmt.executeUpdate(create_table_group); |
39 | + stmt.executeUpdate(create_table_right); | |
40 | + stmt.executeUpdate(create_table_user); | |
30 | 41 | closeConnection(); |
31 | 42 | } |
32 | 43 | |
... | ... | @@ -34,6 +45,8 @@ public abstract class AbstractServiceSupport { |
34 | 45 | public void cleanupDatabase() throws SQLException { |
35 | 46 | openConnection(); |
36 | 47 | stmt.executeUpdate(drop_table_group); |
48 | + stmt.executeUpdate(drop_table_right); | |
49 | + stmt.executeUpdate(drop_table_user); | |
37 | 50 | closeConnection(); |
38 | 51 | } |
39 | 52 | ... | ... |
src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java
... | ... | @@ -27,10 +27,10 @@ public class UserServiceTest extends AbstractServiceSupport { |
27 | 27 | public void before() { |
28 | 28 | groupService.create("group"); |
29 | 29 | userService.create("user", "group"); |
30 | - Right right = rightService.create("parent"); | |
31 | - rightService.create("sibling", right); | |
32 | - groupService.addRight("group", right); | |
33 | - rightService.create("not-for-me"); | |
30 | +// Right right = rightService.create("parent"); | |
31 | +// rightService.create("sibling", right); | |
32 | +// groupService.addRight("group", right); | |
33 | +// rightService.create("not-for-me"); | |
34 | 34 | } |
35 | 35 | |
36 | 36 | @Test |
... | ... | @@ -63,10 +63,12 @@ public class UserServiceTest extends AbstractServiceSupport { |
63 | 63 | userService.create("user", "group"); |
64 | 64 | } |
65 | 65 | |
66 | + @Test | |
66 | 67 | public void testDeleteUser() { |
67 | 68 | assertTrue(userService.delete("user")); |
68 | 69 | } |
69 | 70 | |
71 | + @Test | |
70 | 72 | public void testDeleteUserIfNotFound() { |
71 | 73 | assertFalse(userService.delete("user")); |
72 | 74 | } | ... | ... |