본문 바로가기
개발/Spring

[SpringBoot] WebSocket 채팅방 구현(5) - service

by 코딩하는 흰둥이 2024. 8. 18.

이전글

https://greed-yb.tistory.com/283

 

[SpringBoot] WebSocket 채팅방 구현(4) - controller

이전글 https://greed-yb.tistory.com/282 [SpringBoot] WebSocket 채팅방 구현(3) - html , js이전글https://greed-yb.tistory.com/281 [SpringBoot] WebSocket 채팅방 구현(2) - WebSocketConfigJava 로 구현WebSocketConfigimport org.springframewo

greed-yb.tistory.com

 

service
import com.example.practice.vo.UserVo;
import com.example.practice.vo.messenger.MessageVo;
import com.example.practice.vo.messenger.RoomVo;

import java.util.List;

public interface WidgetService {

    /**
     * 유저 정보 가져오기
     * @return
     * @throws Exception
     */
    List<UserVo> messengerInfo(String username) throws Exception;

    /**
     * 선택한 대상과의 채팅방이 있으면 ID 반환
     * @param targetId
     * @param userId
     * @return
     * @throws Exception
     */
    Long chatRoomInfo(String targetId , String userId) throws Exception;

    /**
     * 채팅방 생성
     * @param roomId
     * @throws Exception
     */
    void createRoom(Long roomId) throws Exception;

    /**
     * 마지막 채팅방 다음 id
     * @return
     * @throws Exception
     */
    Long selectNextRoomId() throws Exception;

    /**
     * 접속한 채팅방 메시지 가져오기
     * @param id
     * @return
     * @throws Exception
     */
    List<MessageVo> selectChatInfo(Long id) throws Exception;

    /**
     * 채팅방에 참여한 유저(본인, 상대방) 저장하기
     * @param roomId
     * @param userId
     * @throws Exception
     */
    void createRoomUser(Long roomId , String userId) throws Exception;

    /**
     * 웹소켓 전달할 대상의 ID 가져오기
     * @param roomId
     * @param userId
     * @return
     * @throws Exception
     */
    String targetId(String roomId, String userId) throws Exception;

    /**
     * 메세지 저장
     * @param roomId
     * @param roomDetail
     * @param userId
     * @throws Exception
     */
    void insertMsg(Long roomId , String roomDetail, String userId) throws Exception;

    /**
     * 읽지않은 메세지 수
     * @param username
     * @return
     * @throws Exception
     */
    Long messengerReadCount(String username) throws Exception;

    /**
     * 채팅방 목록
     * @param username
     * @return
     * @throws Exception
     */
    List<RoomVo> roomList(String username) throws Exception;

    /**
     * 채팅방 들어가기
     * @param id
     * @return
     * @throws Exception
     */
    List<MessageVo> chatRoomEnter(String id) throws Exception;

    /**
     * 채팅방 접속 시 글 읽음으로 업데이트
     * @param id
     * @param username
     * @throws Exception
     */
    void readChat(String id, String username) throws Exception;
}

 

 

 

serviceImpl
import com.example.practice.mapper.widget.WidgetMapper;
import com.example.practice.service.widget.WidgetService;
import com.example.practice.vo.UserVo;
import com.example.practice.vo.messenger.MessageVo;
import com.example.practice.vo.messenger.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WidgetServiceImpl implements WidgetService {

    @Autowired
    private WidgetMapper widgetMapper;

    @Override
    public List<UserVo> messengerInfo(String username) throws Exception {
        return widgetMapper.messengerInfo(username);
    }

    @Override
    public Long chatRoomInfo(String targetId, String userId) throws Exception {
        return widgetMapper.chatRoomInfo(targetId, userId);
    }

    @Override
    @Transactional
    public void createRoom(Long roomId) throws Exception {
        widgetMapper.createRoom(roomId);
    }

    @Override
    public Long selectNextRoomId() throws Exception {
        return widgetMapper.selectNextRoomId();
    }

    @Override
    public List<MessageVo> selectChatInfo(Long id) throws Exception {
        return widgetMapper.selectChatInfo(id);
    }

    @Override
    @Transactional
    public void createRoomUser(Long roomId, String userId) {
        widgetMapper.createRoomUser(roomId, userId);
    }

    @Override
    public String targetId(String roomId, String userId) throws Exception {
        return widgetMapper.targetId(roomId, userId);
    }

    @Override
    @Transactional
    public void insertMsg(Long roomId, String roomDetail, String userId) throws Exception {
        widgetMapper.insertMsg(roomId, roomDetail, userId);
    }

    @Override
    public Long messengerReadCount(String username) throws Exception {
        return widgetMapper.messengerReadCount(username);
    }

    @Override
    public List<RoomVo> roomList(String username) throws Exception {
        return widgetMapper.roomList(username);
    }

    @Override
    public List<MessageVo> chatRoomEnter(String id) throws Exception {
        return widgetMapper.chatRoomEnter(id);
    }

    @Override
    public void readChat(String id, String username) throws Exception {
        widgetMapper.readChat(id, username);
    }
}

댓글