2
2
3
3
import com .balinski .api_project .database .dao .DaoException ;
4
4
import com .balinski .api_project .database .dao .DaoManager ;
5
+ import com .balinski .api_project .database .dao .UserDao ;
5
6
import com .balinski .api_project .database .model .User ;
6
7
import com .balinski .api_project .servlet .util .JsonResponseBuilder ;
7
8
import com .balinski .api_project .servlet .util .UserAuthenticator ;
15
16
import java .security .MessageDigest ;
16
17
import java .security .NoSuchAlgorithmException ;
17
18
import java .time .LocalDateTime ;
19
+ import java .util .ArrayList ;
18
20
import java .util .List ;
19
21
20
22
@@ -35,16 +37,46 @@ private String sha256(String name) throws NoSuchAlgorithmException {
35
37
return hexString .toString ();
36
38
}
37
39
38
- private User addUser (String name , int limit ) throws DaoException , NoSuchAlgorithmException {
39
- if (limit < 0 )
40
- throw new DaoException ("Limit can not be a negative number ." );
40
+ private List < User > addUsers (String [] names , String [] limits ) throws DaoException , NoSuchAlgorithmException {
41
+ if (names . length != limits . length )
42
+ throw new DaoException ("Quantity of names and limits do not match ." );
41
43
42
- int newId = DaoManager .getUserDao ().getMaxId ()+1 ;
43
- User user = new User (newId , "user" , name , sha256 (name ), 0 ,
44
- limit , LocalDateTime .now (), LocalDateTime .now ());
45
- DaoManager .getUserDao ().add (user );
44
+ UserDao dao = DaoManager .getUserDao ();
45
+ int id = dao .getMaxId ()+1 ;
46
+ List <User > users = new ArrayList <>(names .length );
46
47
47
- return user ;
48
+ for (int i = 0 ; i < names .length ; i ++) {
49
+ if (Integer .parseInt (limits [i ]) < 0 )
50
+ limits [i ] = "0" ;
51
+
52
+ users .add (new User (id , "user" , names [i ], sha256 (names [i ]), 0 ,
53
+ Integer .parseInt (limits [i ]), LocalDateTime .now (), LocalDateTime .now ()));
54
+
55
+ id ++;
56
+ }
57
+
58
+ return DaoManager .getUserDao ().add (users , true );
59
+ }
60
+
61
+ private List <User > renewAccess (String [] ids , String [] limits ) throws DaoException {
62
+ List <User > users = new ArrayList <>(ids .length );
63
+
64
+ for (int i = 0 ; i < ids .length ; i ++) {
65
+ if (Integer .parseInt (limits [i ]) < 0 )
66
+ limits [i ] = "0" ;
67
+
68
+ users .addAll (DaoManager .getUserDao ().renewAccess (Integer .parseInt (ids [i ]), Integer .parseInt (limits [i ])));
69
+ }
70
+
71
+ return users ;
72
+ }
73
+
74
+ private List <User > deleteUsers (String [] ids ) throws DaoException {
75
+ List <User > users = new ArrayList <>(ids .length );
76
+ for (String id : ids )
77
+ users .addAll (DaoManager .getUserDao ().delete (Integer .parseInt (id )));
78
+
79
+ return users ;
48
80
}
49
81
50
82
@ Override
@@ -64,42 +96,46 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
64
96
65
97
String action = req .getParameter ("action" ).toLowerCase ();
66
98
String response ;
67
- User user ;
68
- boolean success ;
99
+ List < User > users ;
100
+ String [] userIds , userNames , userLimits ;
69
101
70
102
switch (action ) {
71
103
case "add" :
72
104
if (req .getParameter ("name" ) == null || req .getParameter ("limit" ) == null )
73
105
throw new DaoException ("You have to provide name and limit to add new user." );
74
106
75
- user = addUser (req .getParameter ("name" ), Integer .parseInt (req .getParameter ("limit" )));
76
- response = JsonResponseBuilder .mergeFromList (List .of (user ));
107
+ userNames = req .getParameter ("name" ).split ("," );
108
+ userLimits = req .getParameter ("limit" ).split ("," );
109
+
110
+ users = addUsers (userNames , userLimits );
111
+
112
+ response = JsonResponseBuilder .mergeFromList (users );
77
113
break ;
78
114
79
115
case "renew" :
80
116
if (req .getParameter ("id" ) == null || req .getParameter ("limit" ) == null )
81
117
throw new DaoException ("You have to provide id and new limit to renew user's access." );
82
118
83
- success = DaoManager .getUserDao ()
84
- .renewAccess (Integer .parseInt (req .getParameter ("id" )),
85
- Integer .parseInt (req .getParameter ("limit" )));
86
- if (!success )
87
- throw new DaoException ("Could not update the user. Check if provided parameters are valid." );
119
+ userIds = req .getParameter ("id" ).split ("," );
120
+ userLimits = req .getParameter ("limit" ).split ("," );
121
+
122
+ if (userIds .length != userLimits .length )
123
+ throw new DaoException ("Quantity of IDs and limits do not match." );
124
+
125
+ users = renewAccess (userIds , userLimits );
88
126
89
- response = "User's access has been successfully renewed." ;
127
+ response = JsonResponseBuilder . mergeFromList ( users ) ;
90
128
break ;
91
129
92
130
case "delete" :
93
131
if (req .getParameter ("id" ) == null )
94
132
throw new DaoException ("You have to provide id of user that you want to be deleted." );
95
133
96
- success = DaoManager .getUserDao ()
97
- .delete (Integer .parseInt (req .getParameter ("id" )));
134
+ userIds = req .getParameter ("id" ).split ("," );
98
135
99
- if (!success )
100
- throw new DaoException ("Could not delete the user. Check if provided id is valid." );
136
+ users = deleteUsers (userIds );
101
137
102
- response = "The user has been successfully removed." ;
138
+ response = JsonResponseBuilder . mergeFromList ( users ) ;
103
139
break ;
104
140
105
141
default :
0 commit comments