diff --git a/src/main/java/com/hangang/HangangRiver/common/web/MessageManager.java b/src/main/java/com/hangang/HangangRiver/common/web/MessageManager.java index 7456f98..f9743a2 100644 --- a/src/main/java/com/hangang/HangangRiver/common/web/MessageManager.java +++ b/src/main/java/com/hangang/HangangRiver/common/web/MessageManager.java @@ -67,7 +67,7 @@ private static void sendMessage(JsonObject fcmMessage) throws IOException { HttpURLConnection connection = getConnection(); connection.setDoOutput(true); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); - outputStream.writeBytes(fcmMessage.toString()); + outputStream.write(fcmMessage.toString().getBytes("UTF-8")); outputStream.flush(); outputStream.close(); @@ -138,7 +138,11 @@ public static void sendCommonMessage(String deviceToken, String title, String bo sendMessage(notificationMessage); } - + /** + * Send notification message to FCM for delivery to specific topic. + * + * @throws IOException + */ public static void sendTopicMessage(String topic, String title, String body) throws IOException { JsonObject notificationMessage = buildNotificationMessage(topic, title, body); System.out.println("FCM request body for message using common notification object:"); diff --git a/src/main/java/com/hangang/HangangRiver/meeting/dao/NotificationLogMapper.java b/src/main/java/com/hangang/HangangRiver/meeting/dao/NotificationLogMapper.java index c91a798..b3f694a 100644 --- a/src/main/java/com/hangang/HangangRiver/meeting/dao/NotificationLogMapper.java +++ b/src/main/java/com/hangang/HangangRiver/meeting/dao/NotificationLogMapper.java @@ -4,8 +4,11 @@ import java.util.List; import com.hangang.HangangRiver.meeting.model.NotificationLog; +import org.apache.ibatis.annotations.Param; public interface NotificationLogMapper { - int insert(NotificationLog notificationLog); + int insertDetail(NotificationLog notificationLog); + int insert(@Param("user_id") String user_id, + @Param("message") String message); List selectNotificationLogList(String user_id); } diff --git a/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingBaseService.java b/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingBaseService.java index bac9864..9e70067 100644 --- a/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingBaseService.java +++ b/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingBaseService.java @@ -1,12 +1,17 @@ package com.hangang.HangangRiver.meeting.service; import com.hangang.HangangRiver.access.dao.AccessMapper; +import com.hangang.HangangRiver.access.model.User; +import com.hangang.HangangRiver.common.web.ExceptionController; +import com.hangang.HangangRiver.common.web.MessageManager; import com.hangang.HangangRiver.meeting.dao.CommentMapper; import com.hangang.HangangRiver.meeting.dao.JoinDetailMapper; import com.hangang.HangangRiver.meeting.dao.MatchingMapper; import com.hangang.HangangRiver.meeting.dao.MeetingDetailMapper; import com.hangang.HangangRiver.meeting.dao.NotificationLogMapper; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; public class MeetingBaseService { @@ -22,4 +27,18 @@ public class MeetingBaseService { public AccessMapper accessMapper; @Autowired public NotificationLogMapper notificationLogMapper; + + private final Logger logger = LogManager.getLogger(ExceptionController.class); + + public void pushMessage(String user_id, String message) { + try { + User user = accessMapper.detail(user_id); + MessageManager.sendCommonMessage(user.getFcm_token(), "심심한강", message); + + notificationLogMapper.insert(user_id, message); + } + catch(Exception e) { + logger.error(user_id + " - " + message + " : " + e.getMessage(), e); + } + } } diff --git a/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingCommService.java b/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingCommService.java index 53a97d6..b56a2bf 100644 --- a/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingCommService.java +++ b/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingCommService.java @@ -1,7 +1,5 @@ package com.hangang.HangangRiver.meeting.service; -import com.hangang.HangangRiver.access.model.User; -import com.hangang.HangangRiver.common.web.MessageManager; import com.hangang.HangangRiver.exceptions.AlreadyContactedMeetingException; import com.hangang.HangangRiver.exceptions.InvalidMatchingInfoException; import com.hangang.HangangRiver.exceptions.InvalidMeetingException; @@ -9,8 +7,6 @@ import com.hangang.HangangRiver.meeting.model.ContactedMeeting; import com.hangang.HangangRiver.meeting.model.JoinDetail; import com.hangang.HangangRiver.meeting.model.MeetingDetail; -import com.hangang.HangangRiver.meeting.model.NotificationLog; - import org.springframework.stereotype.Service; import java.io.IOException; @@ -18,27 +14,25 @@ @Service public class MeetingCommService extends MeetingBaseService{ + private String CONTACT_MSG = "같이 놀강~ 난누굴강 완료모임을 확인해주세요."; + private String ADD_COMMENT_MSG = "누가 내가 만든 모임에 관심을 보여요."; + public Comment createComment(Comment comment) throws InvalidMeetingException, IOException { // 1. Check the target meeting to be valid int meeting_seq = comment.getMeeting_seq(); + MeetingDetail meetingDetail = meetingDetailMapper.detail(meeting_seq); - if (meetingDetailMapper.isExistMeetingDetail(meeting_seq)) { - commentMapper.insert(comment); + if (meetingDetail == null) { + throw new InvalidMeetingException(); + } - MeetingDetail meetingDetail = meetingDetailMapper.detail(meeting_seq); - User user = accessMapper.detail(meetingDetail.getUser_id()); - String message = "누가 내가 만든 모임에 관심을 보여요."; - MessageManager.sendCommonMessage(user.getFcm_token(), "심심한강", message); + commentMapper.insert(comment); - NotificationLog notificationLog = new NotificationLog(); - notificationLog.setUser_id(meetingDetail.getUser_id()); - notificationLog.setMessage(message); - notificationLogMapper.insert(notificationLog); - return comment; - } - else { - throw new InvalidMeetingException(); + if(comment.getUser_id().compareTo(meetingDetail.getUser_id()) != 0){ + pushMessage(meetingDetail.getUser_id(), ADD_COMMENT_MSG); } + + return comment; } public List getCommentsByMeeting(int meeting_seq){ @@ -72,21 +66,8 @@ public ContactedMeeting match(ContactedMeeting meeting) meeting.setGuest_user_id(joinDetailInfo.getUser_id()); matchingMapper.insert(meeting); - User hostUser = accessMapper.detail(meeting.getHost_user_id()); - User guestUser = accessMapper.detail(meeting.getGuest_user_id()); - String message = "같이 놀강~ 난누굴강 완료모임을 확인해주세요."; - MessageManager.sendCommonMessage(hostUser.getFcm_token(), "심심한강", message); - MessageManager.sendCommonMessage(guestUser.getFcm_token(), "심심한강", message); - - NotificationLog hostNotificationLog = new NotificationLog(); - hostNotificationLog.setUser_id(meeting.getHost_user_id()); - hostNotificationLog.setMessage(message); - notificationLogMapper.insert(hostNotificationLog); - - NotificationLog guestNotificationLog = new NotificationLog(); - guestNotificationLog.setUser_id(meeting.getGuest_user_id()); - guestNotificationLog.setMessage(message); - notificationLogMapper.insert(guestNotificationLog); + pushMessage(meeting.getHost_user_id(), CONTACT_MSG); + pushMessage(meeting.getGuest_user_id(), CONTACT_MSG); return meeting; } @@ -102,12 +83,4 @@ public ContactedMeeting getContactedMeetingByMatchingInfo(int meeting_seq, int a public void unmatch(int contact_seq){ matchingMapper.delete(contact_seq); } - -/* public List selectMyMatchingMeetings(String user_id){ - return matchingMapper.selectMyMatchingMeetings(user_id); - } - - public List selectMyMatchingApplications(String user_id){ - return matchingMapper.selectMyMatchingApplications(user_id); - }*/ } diff --git a/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingGuestService.java b/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingGuestService.java index 77223b9..5dc660c 100644 --- a/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingGuestService.java +++ b/src/main/java/com/hangang/HangangRiver/meeting/service/MeetingGuestService.java @@ -1,13 +1,10 @@ package com.hangang.HangangRiver.meeting.service; -import com.hangang.HangangRiver.access.model.User; -import com.hangang.HangangRiver.common.web.MessageManager; import com.hangang.HangangRiver.exceptions.ExistJoinDetailException; import com.hangang.HangangRiver.exceptions.InvalidMeetingException; import com.hangang.HangangRiver.exceptions.OverCountJoinDetailException; import com.hangang.HangangRiver.meeting.model.JoinDetail; import com.hangang.HangangRiver.meeting.model.MeetingDetail; -import com.hangang.HangangRiver.meeting.model.NotificationLog; import org.springframework.stereotype.Service; @@ -16,6 +13,8 @@ @Service public class MeetingGuestService extends MeetingBaseService{ + private final String JOIN_MSG = "똑똑! 내가 만든 모임에 신청자가 왔습니다!"; + public JoinDetail join(JoinDetail joinDetail) throws ExistJoinDetailException, InvalidMeetingException, OverCountJoinDetailException, IOException { @@ -35,15 +34,10 @@ public JoinDetail join(JoinDetail joinDetail) } joinDetailMapper.insert(joinDetail); + MeetingDetail meetingDetail = meetingDetailMapper.detail(meeting_seq); - User user = accessMapper.detail(meetingDetail.getUser_id()); - String message = "똑똑! 내가 만든 모임에 신청자가 왔습니다!"; - MessageManager.sendCommonMessage(user.getFcm_token(), "모임 참여", "똑똑! 내가 만든 모임에 신청자가 왔습니다!"); - - NotificationLog notificationLog = new NotificationLog(); - notificationLog.setUser_id(meetingDetail.getUser_id()); - notificationLog.setMessage(message); - notificationLogMapper.insert(notificationLog); + pushMessage(meetingDetail.getUser_id(), JOIN_MSG); + return joinDetail; } diff --git a/src/main/java/com/hangang/HangangRiver/meeting/service/NotificationLogService.java b/src/main/java/com/hangang/HangangRiver/meeting/service/NotificationLogService.java index e167554..6db7ad6 100644 --- a/src/main/java/com/hangang/HangangRiver/meeting/service/NotificationLogService.java +++ b/src/main/java/com/hangang/HangangRiver/meeting/service/NotificationLogService.java @@ -11,7 +11,7 @@ @Service public class NotificationLogService extends MeetingBaseService{ public NotificationLog createNotificationLog(NotificationLog notificationLog){ - notificationLogMapper.insert(notificationLog); + notificationLogMapper.insertDetail(notificationLog); return notificationLog; } diff --git a/src/main/resources/com/hangang/HangangRiver/NotificationLogMapper.xml b/src/main/resources/com/hangang/HangangRiver/NotificationLogMapper.xml index 9fb8ce3..72c92cf 100644 --- a/src/main/resources/com/hangang/HangangRiver/NotificationLogMapper.xml +++ b/src/main/resources/com/hangang/HangangRiver/NotificationLogMapper.xml @@ -2,7 +2,18 @@ - + + INSERT INTO + notification_log + (user_id, message, creation_time) + VALUES + (#{user_id}, #{message}, current_timestamp) + + SELECT LAST_INSERT_ID() + + + + INSERT INTO notification_log (user_id, message, creation_time)