Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L6 #1031

Open
wants to merge 1 commit into
base: lecture06
Choose a base branch
from
Open

L6 #1031

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lecture06/src/main/java/ru/atom/controller/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ public ResponseEntity<String> login(@RequestParam("name") String name) {
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity logout(@RequestParam("name") String name) {
throw new UnsupportedOperationException();
List<User> allUsers = userDao.getAll();
if (allUsers.stream().noneMatch(l -> l.getLogin().equals(name))) {
return ResponseEntity.badRequest()
.body("No user with name " + name);
}
User user = new User().setLogin(name);

log.info("[" + name + "] logged out");

return ResponseEntity.ok().build();
}


Expand Down
8 changes: 4 additions & 4 deletions lecture06/src/main/java/ru/atom/dao/DbConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class DbConnector {

private static final String URL_TEMPLATE = "jdbc:postgresql://%s:%d/%s";
private static final String URL;
private static final String HOST = "54.224.37.210";
private static final String HOST = "localhost";
private static final int PORT = 5432;
private static final String DB_NAME = "chatdb_atomN";
private static final String USER = "atomN";
private static final String PASSWORD = "atomN";
private static final String DB_NAME = "postgres";
private static final String USER = "postgres";
private static final String PASSWORD = "password1997";

static {
try {
Expand Down
17 changes: 16 additions & 1 deletion lecture06/src/main/java/ru/atom/dao/MessageDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class MessageDao implements Dao<Message> {
" on m.user = u.id " +
"order by m.time";

private static final String SELECT_ALL_MESSAGES_WHERE = SELECT_ALL_MESSAGES + " where ";

private static final String INSERT_MESSAGE_TEMPLATE =
"insert into chat.message (\"user\", time, value) " +
"values (%d, now(), '%s')";
Expand All @@ -50,7 +52,20 @@ public List<Message> getAll() {

@Override
public List<Message> getAllWhere(String... conditions) {
throw new UnsupportedOperationException();
List<Message> messages = new ArrayList<>();
try (Connection con = DbConnector.getConnection();
Statement stm = con.createStatement()
) {
String condition = String.join(" and ", conditions);
ResultSet rs = stm.executeQuery(SELECT_ALL_MESSAGES_WHERE + condition);
while (rs.next()) {
messages.add(mapToMessage(rs));
}
} catch (SQLException e) {
log.error("Failed to getAll where.", e);
return Collections.emptyList();
}
return messages;
}

@Override
Expand Down
16 changes: 15 additions & 1 deletion lecture06/src/main/java/ru/atom/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,21 @@ public void insert(User user) {
}

public User getByName(String name) {
throw new UnsupportedOperationException();
User person;
try (Connection con = DbConnector.getConnection();
Statement stm = con.createStatement()
) {
ResultSet rs = stm.executeQuery(SELECT_ALL_USERS_WHERE + "chat.user.login = '" + name + "'");
if (rs.next()) {
return mapToUser(rs);
} else {
log.info("No user with name " + name);
return null;
}
} catch (SQLException e) {
log.error("Failed to getAll where.", e);
return null;
}
}

private static User mapToUser(ResultSet rs) throws SQLException {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*C4*/SET SCHEMA PUBLIC
DISCONNECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#HSQL Database Engine 2.4.0
#Wed Apr 01 19:33:59 UTC 2020
tx_timestamp=0
modified=yes
version=2.4.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SET DATABASE UNIQUE NAME HSQLDB71373C41C8
SET DATABASE GC 0
SET DATABASE DEFAULT RESULT MEMORY ROWS 0
SET DATABASE EVENT LOG LEVEL 0
SET DATABASE TRANSACTION CONTROL LOCKS
SET DATABASE DEFAULT ISOLATION LEVEL READ COMMITTED
SET DATABASE TRANSACTION ROLLBACK ON CONFLICT TRUE
SET DATABASE TEXT TABLE DEFAULTS ''
SET DATABASE SQL NAMES FALSE
SET DATABASE SQL REFERENCES FALSE
SET DATABASE SQL SIZE TRUE
SET DATABASE SQL TYPES FALSE
SET DATABASE SQL TDC DELETE TRUE
SET DATABASE SQL TDC UPDATE TRUE
SET DATABASE SQL CONCAT NULLS TRUE
SET DATABASE SQL UNIQUE NULLS TRUE
SET DATABASE SQL CONVERT TRUNCATE TRUE
SET DATABASE SQL AVG SCALE 0
SET DATABASE SQL DOUBLE NAN TRUE
SET FILES WRITE DELAY 500 MILLIS
SET FILES BACKUP INCREMENT TRUE
SET FILES CACHE SIZE 10000
SET FILES CACHE ROWS 50000
SET FILES SCALE 32
SET FILES LOB SCALE 32
SET FILES DEFRAG 0
SET FILES NIO TRUE
SET FILES NIO SIZE 256
SET FILES LOG TRUE
SET FILES LOG SIZE 50
CREATE USER "postgres" PASSWORD DIGEST '922ce8539ca827eb587528c246da710d'
ALTER USER "postgres" SET LOCAL TRUE
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.TIME_STAMP TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CARDINAL_NUMBER TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CHARACTER_DATA TO PUBLIC
GRANT DBA TO "postgres"
SET SCHEMA SYSTEM_LOBS
INSERT INTO BLOCKS VALUES(0,2147483647,0)
2 changes: 1 addition & 1 deletion lecture06/src/test/java/ru/atom/dao/MessageDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Created by sergey on 3/25/17.
*/
@Ignore

public class MessageDaoTest {
private MessageDao messageDao;
private String msg ;
Expand Down
3 changes: 2 additions & 1 deletion lecture06/src/test/java/ru/atom/dao/UserDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Created by sergey on 3/25/17.
*/
@Ignore

public class UserDaoTest {
private UserDao userDao;
private String login;
Expand All @@ -34,6 +34,7 @@ public void setUp() throws Exception {

@Test
public void getAllTest() throws Exception {
System.out.println(userDao.getAll());
assertTrue(userDao.getAll().size() > 0);
}

Expand Down