From 8a0fbbd35c13054a947ea0d27ca117542bc452b9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 18 May 2018 15:33:38 +0300 Subject: JPA WIP --- .../com/juick/service/MessagesServiceImpl.java | 39 ++++++++++++---------- 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java') diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java index a78c9203..b981b37f 100644 --- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -20,6 +20,9 @@ package com.juick.service; import com.juick.*; import com.juick.server.helpers.PrivacyOpts; import com.juick.server.helpers.ResponseReply; +import com.juick.service.data.MessagesRepository; +import com.juick.service.data.UsersRepository; +import com.juick.service.data.entities.MessageEntity; import com.juick.util.MessageUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -51,6 +54,10 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Inject private UserService userService; @Inject + private UsersRepository usersRepository; + @Inject + private MessagesRepository messagesRepository; + @Inject private TagService tagService; @Inject private SearchService searchService; @@ -110,19 +117,15 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ */ @Transactional @Override - public int createMessage(final int uid, final String txt, final String attachment, final Collection tags) { - SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName("messages") - .usingColumns("user_id", "attach", "ts") - .usingGeneratedKeyColumns("message_id"); - Map insertMap = new HashMap<>(); - insertMap.put("user_id", uid); + public Message createMessage(final User user, final String txt, final String attachment, final Collection tags) { + MessageEntity newMsg = new MessageEntity(); + newMsg.setText(txt); + newMsg.setUser(usersRepository.findById(user.getUid()).orElseThrow(IllegalStateException::new)); Instant now = Instant.now(); - insertMap.put("ts", Timestamp.from(now)); - if (attachment != null) { - insertMap.put("attach", attachment); - } - int mid = simpleJdbcInsert.executeAndReturnKey(insertMap).intValue(); - if (mid > 0) { + newMsg.setTimestamp(now); + + newMsg = messagesRepository.save(newMsg); + if (newMsg.getId() > 0) { String tagsNames = StringUtils.EMPTY; if (CollectionUtils.isNotEmpty(tags)) { @@ -138,7 +141,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ next = true; tasNamesBuilder.append(tag.getName()); - params.add(new Object[]{mid, tag.TID}); + params.add(new Object[]{newMsg.getId(), tag.TID}); } tagsNames = tasNamesBuilder.toString(); @@ -148,13 +151,13 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } getJdbcTemplate().update( - "INSERT INTO messages_txt(message_id, tags, txt) VALUES (?, ?, ?)", - new Object[]{mid, tagsNames, txt}, - new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR}); - getJdbcTemplate().update("UPDATE users SET lastmessage=? where id=?", Timestamp.from(now), uid); + "UPDATE messages_txt SET tags=? WHERE message_id=?", + new Object[]{tagsNames, newMsg.getId()}, + new int[]{Types.VARCHAR, Types.INTEGER}); + getJdbcTemplate().update("UPDATE users SET lastmessage=? where id=?", Timestamp.from(now), user.getUid()); } - return mid; + return getMessage(newMsg.getId()); } /** -- cgit v1.2.3