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

Commit 678763e

Browse files
committed
Introduce basic authentication, improve ActorServlet
1 parent d7341c4 commit 678763e

25 files changed

+185
-174
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<version>1.0-SNAPSHOT</version>
1111
<modules>
1212
<module>server</module>
13-
<module>webapp</module>
1413
</modules>
1514

1615

server/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
<artifactId>jetty-servlet</artifactId>
3030
<version>9.4.25.v20191220</version>
3131
</dependency>
32-
<dependency>
33-
<groupId>org.eclipse.jetty</groupId>
34-
<artifactId>jetty-webapp</artifactId>
35-
<version>9.4.25.v20191220</version>
36-
</dependency>
3732
<dependency>
3833
<groupId>com.h2database</groupId>
3934
<artifactId>h2</artifactId>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010

1111
public class Main {
12-
12+
//"name":"jakub","token":"7f078d288631a4de5fc1ce01fa4e0addeebed19ad5c6d6f543fcc160c5c5cdfc"
1313
public static void main(String[] args) {
1414
//testDatabase();
1515
runApplication();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static int queryUpdate(String sql, boolean transaction) throws DatabaseEx
6969
connection.commit();
7070
}
7171
} catch (SQLException e) {
72+
e.printStackTrace();
7273
throw new DatabaseException("An error occurred when trying to update the database.", e);
7374
} finally {
7475
closeConnection();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.util.*;
66

77
public class ActorDao extends Dao<Actor> {
8-
public ActorDao(boolean transaction) {
9-
super(DaoType.ACTOR, transaction);
8+
public ActorDao() {
9+
super(DaoType.ACTOR);
1010
}
1111

1212
public List<Actor> getByFirstName(String firstName) throws DaoException {

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
import java.util.*;
66

77
abstract class Dao<T extends DatabaseModel> {
8-
protected boolean transaction;
98
protected DaoType type;
109

11-
protected Dao(DaoType type, boolean transaction) {
10+
protected Dao(DaoType type) {
1211
this.type = type;
13-
this.transaction = transaction;
1412
}
1513

1614
public int getCount() throws DaoException {
@@ -52,10 +50,19 @@ public int add(T obj) throws DaoException {
5250

5351
String sql = String.format("INSERT INTO %s VALUES (%s);", type.toString(), obj.asCsv());
5452

55-
return DaoManager.modifyData(sql, transaction);
53+
return DaoManager.modifyData(sql, false);
5654
}
5755

58-
public int addAll(List<T> list) throws DaoException {
56+
public int update(T obj) throws DaoException {
57+
if(obj == null)
58+
return 0;
59+
60+
String sql = String.format("INSERT INTO %s VALUES (%s);", type.toString(), obj.asCsv());
61+
62+
return DaoManager.modifyData(sql, false);
63+
}
64+
65+
public int addAll(List<T> list, boolean transaction) throws DaoException {
5966
if(list == null || list.size() == 0)
6067
return 0;
6168

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,19 @@ public class DaoManager {
1010
private DaoManager(){}
1111

1212
public static ActorDao getActorDao() {
13-
return new ActorDao(false);
13+
return new ActorDao();
1414
}
1515

1616
public static FilmDao getFilmDao() {
17-
return new FilmDao(false);
17+
return new FilmDao();
1818
}
1919

2020
public static LanguageDao getLanguageDao() {
21-
return new LanguageDao(false);
21+
return new LanguageDao();
2222
}
2323

2424
public static UserDao getUserDao() {
25-
return new UserDao(false);
26-
}
27-
28-
public static ActorDao getActorDao(boolean transaction) {
29-
return new ActorDao( transaction);
30-
}
31-
32-
public static FilmDao getFilmDao(boolean transaction) {
33-
return new FilmDao(transaction);
34-
}
35-
36-
public static LanguageDao getLanguageDao (boolean transaction) {
37-
return new LanguageDao( transaction);
38-
}
39-
40-
public static UserDao getUserDao (boolean transaction) {
41-
return new UserDao(transaction);
25+
return new UserDao();
4226
}
4327

4428
static List<Map<String, Object>> getData(String sql) throws DaoException {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
public class FilmDao extends Dao<Film> {
1212
static final DateTimeFormatter toDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
1313

14-
public FilmDao(boolean transaction) {
15-
super(DaoType.FILM, transaction);
14+
public FilmDao() {
15+
super(DaoType.FILM);
1616
}
1717

1818
public List<Film> getByTitle(String title) throws DaoException {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.util.*;
66

77
public class LanguageDao extends Dao<Language> {
8-
public LanguageDao(boolean transaction) {
9-
super(DaoType.LANGUAGE, transaction);
8+
public LanguageDao() {
9+
super(DaoType.LANGUAGE);
1010
}
1111

1212
public List<Language> getByName(String name) throws DaoException {

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.balinski.api_project.database.model.User;
44

5+
import java.time.LocalDateTime;
6+
import java.time.format.DateTimeFormatter;
57
import java.util.List;
68
import java.util.Map;
79

810
public class UserDao extends Dao<User> {
9-
public UserDao(boolean transaction) {
10-
super(DaoType.USER, transaction);
11+
static final DateTimeFormatter toDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
12+
public UserDao() {
13+
super(DaoType.USER);
1114
}
1215

1316
public List<User> getByName(String name) throws DaoException {
@@ -17,4 +20,21 @@ public List<User> getByName(String name) throws DaoException {
1720

1821
return toListOfObjects(result);
1922
}
23+
24+
public List<User> getByToken(String token) throws DaoException {
25+
List<Map<String, Object>> result = DaoManager.getData(
26+
String.format("SELECT * FROM USER U WHERE lower(U.token) = '%s';", token.toLowerCase())
27+
);
28+
29+
return toListOfObjects(result);
30+
}
31+
32+
public boolean incrementUses(int id) throws DaoException {
33+
return DaoManager.modifyData(
34+
String.format("UPDATE USER SET REQUESTS_SENT=" +
35+
"(SELECT U.REQUESTS_SENT FROM USERS U WHERE U.ID=%d)+1, " +
36+
"LAST_UPDATE='%s' " +
37+
"WHERE U.ID=%d;", id, LocalDateTime.now().format(toDateTime), id),
38+
true) > 0;
39+
}
2040
}

0 commit comments

Comments
 (0)