Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.

Commit 82b1a7a

Browse files
committed
Finish all major functionalities
1 parent 67f907a commit 82b1a7a

16 files changed

+331
-103
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
To use your own database, configure properly properties file under server/src/main/resources/database.properties,
2+
then initialize the database with sakila-min.sql script.
3+
4+
API Default Admin User: user=sa, token=bdd69043cade8ba513813e2e17cd25e241e259bdac956dab2d437b43f40d9514

sakila-min.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
CREATE CACHED TABLE PUBLIC.USER(
44
USER_ID INT IDENTITY NOT NULL PRIMARY KEY,
5-
NAME VARCHAR(45) NOT NULL UNIQUE,
5+
ROLE VARCHAR(5) NOT NULL,
6+
NAME VARCHAR(45) NOT NULL,
67
TOKEN VARCHAR(64) NOT NULL,
78
REQUESTS_SENT INT NOT NULL DEFAULT 0,
89
USE_LIMIT INT NOT NULL,
@@ -38,6 +39,8 @@ CREATE CACHED TABLE PUBLIC.LANGUAGE(
3839
LAST_UPDATE TIMESTAMP NOT NULL
3940
);
4041

42+
INSERT INTO PUBLIC.USER (ROLE, NAME, TOKEN, REQUESTS_SENT, USE_LIMIT, DATE_REGISTERED, LAST_UPDATE) VALUES ('admin', 'sa', 'bdd69043cade8ba513813e2e17cd25e241e259bdac956dab2d437b43f40d9514', 0, 100000000, '1970-01-01', '1970-01-01');
43+
4144
INSERT INTO PUBLIC.ACTOR (FIRST_NAME, LAST_NAME, LAST_UPDATE) VALUES ('PENELOPE', 'GUINESS', '2006-02-15 04:34:33.000000000');
4245
INSERT INTO PUBLIC.ACTOR (FIRST_NAME, LAST_NAME, LAST_UPDATE) VALUES ('NICK', 'WAHLBERG', '2006-02-15 04:34:33.000000000');
4346
INSERT INTO PUBLIC.ACTOR (FIRST_NAME, LAST_NAME, LAST_UPDATE) VALUES ('ED', 'CHASE', '2006-02-15 04:34:33.000000000');

server/src/main/java/com/balinski/api_project/database/DatabaseProxy.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class DatabaseProxy {
1616
protected static String password;
1717

1818
static {
19-
loadDatabaseProperties("server/src/main/resources/database.properties");
19+
loadDatabaseProperties();
2020
loadDriver();
2121
testConnection();
2222
initDataSource();
@@ -122,13 +122,13 @@ protected static void initDataSource() {
122122
dataSource.setMaxOpenPreparedStatements(100);
123123
}
124124

125-
protected static void loadDatabaseProperties(String path) throws DatabaseException {
125+
protected static void loadDatabaseProperties() throws DatabaseException {
126126
Properties props;
127127
try {
128-
props = FilePropertiesLoader.load(path);
128+
props = FilePropertiesLoader.load("server/src/main/resources/database.properties");
129129
} catch (IOException e) {
130130
throw new DatabaseException("Could not find database properties file under given path: "
131-
+ path, e);
131+
+ "server/src/main/resources/database.properties", e);
132132
}
133133

134134
url = props.getProperty("url");

server/src/main/java/com/balinski/api_project/database/dao/Dao.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,6 @@ public int add(T obj) throws DaoException {
6161
return DaoManager.modifyData(sql, false);
6262
}
6363

64-
public int update(T obj) throws DaoException {
65-
if(obj == null)
66-
return 0;
67-
68-
String sql = String.format("INSERT INTO %s VALUES (%s);", type.toString(), obj.asCsv());
69-
70-
return DaoManager.modifyData(sql, false);
71-
}
72-
7364
public int addAll(List<T> list, boolean transaction) throws DaoException {
7465
if(list == null || list.size() == 0)
7566
return 0;
@@ -87,6 +78,12 @@ public int addAll(List<T> list, boolean transaction) throws DaoException {
8778
return DaoManager.modifyData(sql.toString(), transaction);
8879
}
8980

81+
public boolean delete(int id) throws DaoException {
82+
return DaoManager.modifyData(
83+
String.format("DELETE FROM %s WHERE %s_ID=%d;", type.toString(), type.toString(), id),
84+
false) > 0;
85+
}
86+
9087
protected List<T> toListOfObjects(List<Map<String, Object>> listOfMaps) throws DaoException {
9188
if(listOfMaps == null)
9289
return null;

server/src/main/java/com/balinski/api_project/database/dao/FilmDao.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ public List<Film> getWithLengthBetween(int min, int max) throws DaoException {
116116

117117
public List<Film> getShorterThan(int minutes) throws DaoException {
118118
List<Map<String, Object>> result = DaoManager.getData(
119-
String.format("SELECT * FROM FILM F WHERE F.LENGTH < %d;", minutes)
119+
String.format("SELECT * FROM FILM F WHERE F.LENGTH < %d;", minutes+1)
120120
);
121121

122122
return toListOfObjects(result);
123123
}
124124

125125
public List<Film> getLongerThan(int minutes) throws DaoException {
126126
List<Map<String, Object>> result = DaoManager.getData(
127-
String.format("SELECT * FROM FILM F WHERE F.LENGTH > %d;", minutes)
127+
String.format("SELECT * FROM FILM F WHERE F.LENGTH > %d;", minutes-1)
128128
);
129129

130130
return toListOfObjects(result);

server/src/main/java/com/balinski/api_project/database/dao/UserDao.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public List<User> getByName(String name) throws DaoException {
2121
return toListOfObjects(result);
2222
}
2323

24+
public List<User> getByRole(String role) throws DaoException {
25+
List<Map<String, Object>> result = DaoManager.getData(
26+
String.format("SELECT * FROM USER U WHERE lower(U.ROLE) = '%s';", role.toLowerCase())
27+
);
28+
29+
return toListOfObjects(result);
30+
}
31+
2432
public List<User> getByToken(String token) throws DaoException {
2533
List<Map<String, Object>> result = DaoManager.getData(
2634
String.format("SELECT * FROM USER U WHERE lower(U.token) = '%s';", token.toLowerCase())
@@ -29,12 +37,19 @@ public List<User> getByToken(String token) throws DaoException {
2937
return toListOfObjects(result);
3038
}
3139

40+
public boolean renewAccess(int id, int newLimit) throws DaoException {
41+
return DaoManager.modifyData(
42+
String.format("UPDATE USER SET REQUESTS_SENT=0, USE_LIMIT=%d, LAST_UPDATE='%s' WHERE USER_ID=%d;",
43+
newLimit, LocalDateTime.now().format(toDateTime), id),
44+
false) > 0;
45+
}
46+
3247
public boolean incrementUses(int id) throws DaoException {
3348
return DaoManager.modifyData(
3449
String.format("UPDATE USER SET REQUESTS_SENT=" +
35-
"(SELECT U.REQUESTS_SENT FROM USERS U WHERE U.ID=%d)+1, " +
50+
"((SELECT U.REQUESTS_SENT FROM USER U WHERE U.USER_ID=%d)+1), " +
3651
"LAST_UPDATE='%s' " +
37-
"WHERE U.ID=%d;", id, LocalDateTime.now().format(toDateTime), id),
38-
true) > 0;
52+
"WHERE USER_ID=%d;", id, LocalDateTime.now().format(toDateTime), id),
53+
false) > 0;
3954
}
4055
}

server/src/main/java/com/balinski/api_project/database/model/ModelFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private T languageFromMap(Map<String, Object> map) {
4141
}
4242

4343
private T userFromMap(Map<String, Object> map) {
44-
return (T) new User((int)map.get("USER_ID"), (String)map.get("NAME"),
44+
return (T) new User((int)map.get("USER_ID"), (String)map.get("ROLE"), (String)map.get("NAME"),
4545
(String)map.get("TOKEN"), (int)map.get("REQUESTS_SENT"), (int)map.get("USE_LIMIT"),
4646
((Timestamp)map.get("DATE_REGISTERED")).toLocalDateTime(),
4747
((Timestamp)map.get("LAST_UPDATE")).toLocalDateTime());

server/src/main/java/com/balinski/api_project/database/model/User.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44

55
public class User extends DatabaseModel {
66
final int id;
7+
final String role;
78
final String name;
8-
final String token;
9+
String token;
910
int used;
1011
final int limit;
1112
final LocalDateTime dateRegistered;
1213
LocalDateTime lastUpdate;
1314

14-
public User(int id, String name, String token, int used, int limit,
15+
16+
public User(int id, String role, String name, String token, int used, int limit,
1517
LocalDateTime dateRegistered, LocalDateTime lastUpdate) {
1618
this.id = id;
19+
this.role = role;
1720
this.name = name;
1821
this.token = token;
1922
this.used = used;
@@ -26,6 +29,10 @@ public int getId() {
2629
return id;
2730
}
2831

32+
public String getRole() {
33+
return role;
34+
}
35+
2936
public String getName() {
3037
return name;
3138
}
@@ -50,6 +57,10 @@ public LocalDateTime getLastUpdate() {
5057
return lastUpdate;
5158
}
5259

60+
public void setToken(String token) {
61+
this.token = token;
62+
}
63+
5364
public void setUsed(int used) {
5465
this.used = used;
5566
}
@@ -61,14 +72,14 @@ public void setLastUpdate(LocalDateTime lastUpdate) {
6172
@Override
6273
public String asJson(){
6374
return String.format("{\"type\":\"%s\",\"id\":\"%d\",\"attributes\":" +
64-
"{\"name\":\"%s\",\"token\":\"%s\",\"used\":\"%d\",\"limit\":\"%d\"," +
75+
"{\"role\":\"%s\",\"name\":\"%s\",\"token\":\"%s\",\"used\":\"%d\",\"limit\":\"%d\"," +
6576
"\"dateRegistered\":\"%s\",\"lastUpdate\":\"%s\"}}",
66-
"users", id, name, token, used, limit, dateRegistered.format(toDateTime), lastUpdate.format(toDateTime));
77+
"users", id, role, name, token, used, limit, dateRegistered.format(toDateTime), lastUpdate.format(toDateTime));
6778
}
6879

6980
@Override
7081
public String asCsv() {
71-
return String.format("%d, '%s', '%s', %d, %d, TIMESTAMP '%s', TIMESTAMP '%s'",
72-
id, name, token, used, limit, dateRegistered.format(toDateTime), lastUpdate.format(toDateTime));
82+
return String.format("%d, '%s', '%s', '%s', %d, %d, TIMESTAMP '%s', TIMESTAMP '%s'",
83+
id, role, name, token, used, limit, dateRegistered.format(toDateTime), lastUpdate.format(toDateTime));
7384
}
7485
}

server/src/main/java/com/balinski/api_project/server/JettyServer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static void registerServlets() {
2626
context.addServlet(FilmServlet.class, "/films/*");
2727
context.addServlet(LanguageServlet.class, "/languages/*");
2828
context.addServlet(UserServlet.class, "/users/*");
29-
context.addServlet(AddUserServlet.class, "/adduser/*");
29+
context.addServlet(AdminServlet.class, "/admin/*");
3030
}
3131

3232
}

server/src/main/java/com/balinski/api_project/servlet/ActorServlet.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,8 @@ else if(param.get("firstName") != null)
4444
actors = dao.getByFirstName(param.get("firstName"));
4545
else if(param.get("lastName") != null)
4646
actors = dao.getByLastName(param.get("lastName"));
47-
else if(param.get("perPage") != null && param.get("page") != null) {
48-
int perPage = Integer.parseInt(param.get("perPage"));
49-
int page = Integer.parseInt(param.get("page"));
50-
actors = dao.getIdBetween(perPage*page+1, perPage*(page+1));
51-
} else {
47+
else
5248
actors = dao.getAll();
53-
}
5449

5550
if(param.get("order") != null) {
5651
if(param.get("order").equalsIgnoreCase("desc"))
@@ -59,6 +54,14 @@ else if(param.get("perPage") != null && param.get("page") != null) {
5954
actors.sort(Comparator.comparing(Actor::getLastName).thenComparing(Actor::getFirstName));
6055
}
6156

57+
if(param.get("perPage") != null && param.get("page") != null) {
58+
int perPage = Integer.parseInt(param.get("perPage"));
59+
int page = Integer.parseInt(param.get("page"));
60+
int firstRecord = Math.min(perPage*page, actors.size());
61+
int lastRecord = Math.min(perPage * (page + 1), actors.size());
62+
actors = actors.subList(firstRecord, lastRecord);
63+
}
64+
6265
String response = JsonResponseBuilder.mergeFromList(actors);
6366
writer.print(response);
6467
} catch (DaoException e) {

server/src/main/java/com/balinski/api_project/servlet/AddUserServlet.java

-63
This file was deleted.

0 commit comments

Comments
 (0)