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

Fixed group chat room management methods. ChatRoomService refactoring. #103

Merged
merged 6 commits into from
Aug 12, 2024
Merged
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
5 changes: 3 additions & 2 deletions chat/src/main/java/greencity/client/RestClientUbs.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ public List<LocationsDto> getAllLocationsByCourierId(Long courierId) {
* @param locationId The ID of the location for which to retrieve the tariff ID.
* @return The tariff ID associated with the specified location ID.
*/
public Long getTariffIdByLocationId(Long locationId) {
public List<Long> getTariffIdByLocationId(Long locationId) {
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<>(headers);

return restTemplate.exchange(
greenCityUbsServerAddress + "/ubs/tariffs/" + locationId,
HttpMethod.GET,
entity,
Long.class).getBody();
new ParameterizedTypeReference<List<Long>>() {
}).getBody();
}

/**
Expand Down
38 changes: 21 additions & 17 deletions chat/src/main/java/greencity/controller/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import greencity.dto.CreateNewChatDto;
import greencity.dto.FriendsChatDto;
import greencity.dto.GroupChatRoomCreateDto;
import greencity.dto.LeaveChatDto;
import greencity.dto.MessageLike;
import greencity.dto.PageableDto;
import greencity.dto.ParticipantDto;
Expand All @@ -33,8 +32,10 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.data.domain.Pageable;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -125,9 +126,9 @@ public ResponseEntity<PageableDto<ChatMessageWithFileDto>> findAllMessages(
@ApiResponse(responseCode = "404", description = HttpStatuses.NOT_FOUND)
})
@GetMapping("/room/{room_id}")
public ResponseEntity<ChatRoomDto> findRoomById(@PathVariable("room_id") Long id) {
public ResponseEntity<ChatRoomDto> findRoomById(@PathVariable("room_id") Long id, Principal principal) {
return ResponseEntity.status(HttpStatus.OK)
.body(chatRoomService.findChatRoomById(id));
.body(chatRoomService.findChatRoomById(id, principal.getName()));
}

/**
Expand Down Expand Up @@ -228,39 +229,42 @@ public ResponseEntity<Long> getLastId() {
* Delete participants from group chat room.
*
* @param chatRoomDto of {@link ChatRoomDto}
* @param userId id of current user.
*/
@MessageMapping("/chat/users/delete-participants-room")
public void deleteParticipantsFromChatRoom(ChatRoomDto chatRoomDto) {
chatRoomService.deleteParticipantsFromChatRoom(chatRoomDto);
public void deleteParticipantsFromChatRoom(@Payload ChatRoomDto chatRoomDto, @Header("userId") Long userId) {
chatRoomService.deleteParticipantsFromChatRoom(chatRoomDto, userId);
}

/**
* Method return private chat for current user.
* Method return system chat for current user.
*/
@MessageMapping("/chat/user")
public void createNewPrivateChatIfNotExist(@RequestBody CreateNewChatDto createNewChatDto) {
chatRoomService.findPrivateByParticipantsForSockets(createNewChatDto.getLocationsIds(),
public void createNewSystemChatIfNotExist(@RequestBody CreateNewChatDto createNewChatDto) {
chatRoomService.findSystemChatByParticipantsForSockets(createNewChatDto.getTariffId(),
createNewChatDto.getCurrentUserId());
}

/**
* Add participants from group chat room.
*
* @param chatRoomDto of {@link ChatRoomDto}
* @param userId id of current user.
*/
@MessageMapping("/chat/users/update-room")
public void addParticipantsToChatRoom(ChatRoomDto chatRoomDto) {
chatRoomService.updateChatRoom(chatRoomDto);
public void updateChatRoom(@Payload ChatRoomDto chatRoomDto, @Header("userId") Long userId) {
chatRoomService.updateChatRoom(chatRoomDto, userId);
}

/**
* Delete current user from group chat room.
*
* @param leaveChatDto of {@link LeaveChatDto}
* @param chatRoomDto of {@link ChatRoomDto}
* @param userId id of current user.
*/
@MessageMapping("/chat/users/leave-room")
public void leaveRoom(LeaveChatDto leaveChatDto) {
chatRoomService.leaveChatRoom(leaveChatDto);
public void leaveRoom(@Payload ChatRoomDto chatRoomDto, @Header("userId") Long userId) {
chatRoomService.leaveChatRoom(chatRoomDto, userId);
}

/**
Expand All @@ -287,7 +291,7 @@ public void deleteChatRoom(@PathVariable long id, ChatRoomDto chatRoomDto) {
@GetMapping("/groups")
public ResponseEntity<List<ChatRoomDto>> getGroupChats(Principal principal) {
return ResponseEntity.status(HttpStatus.OK)
.body(chatRoomService.findGroupChatRooms(participantService.findByEmail(principal.getName()),
.body(chatRoomService.findChatRoomsByChatType(participantService.findByEmail(principal.getName()),
ChatType.GROUP));
}

Expand Down Expand Up @@ -503,7 +507,7 @@ public ResponseEntity<FriendsChatDto> chatExist(@PathVariable Long fistUserId, @
public ResponseEntity deleteAllMessagesFromChatRoom(@PathVariable Long userId,
@PathVariable Long chatId) {
chatRoomService.deleteMessagesFromChatRoom(chatId, userId);
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
return ResponseEntity.status(HttpStatus.OK).build();
}

/**
Expand Down Expand Up @@ -536,8 +540,8 @@ public ResponseEntity<List<ChatRoomDto>> findAllChatsByTariffId(@PathVariable Lo
@ApiResponse(responseCode = "404", description = HttpStatuses.NOT_FOUND)
})
@GetMapping(value = "/tariffs/byLocation/{locationId}")
public ResponseEntity<Long> getTariffIdByLocationId(@PathVariable("locationId") Long locationId) {
Long tariffId = chatRoomService.getTariffIdByLocationId(locationId);
public ResponseEntity<List<Long>> getTariffIdByLocationId(@PathVariable("locationId") Long locationId) {
List<Long> tariffId = chatRoomService.getTariffIdByLocationId(locationId);
return ResponseEntity.status(HttpStatus.OK).body(tariffId);
}

Expand Down
2 changes: 1 addition & 1 deletion chat/src/main/java/greencity/dto/CreateNewChatDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
@Builder
public class CreateNewChatDto {
private Long currentUserId;
private Long locationsIds;
private Long tariffId;
}
3 changes: 2 additions & 1 deletion chat/src/main/java/greencity/dto/GroupChatRoomCreateDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import greencity.enums.ChatStatus;
import greencity.enums.ChatType;
import lombok.Data;
import lombok.Builder;
import lombok.NoArgsConstructor;
Expand All @@ -13,9 +14,9 @@
@Builder
public class GroupChatRoomCreateDto {
private List<Long> usersId;
private Long tariffId;
private ChatStatus chatStatus;
private String chatName;
private Long ownerId;
private String logo;
private ChatType chatType;
}
2 changes: 1 addition & 1 deletion chat/src/main/java/greencity/entity/Participant.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
@ToString(exclude = {"rooms", "unreadMessages"})
@Table(name = "users")
public class Participant {
@Id
Expand Down
29 changes: 29 additions & 0 deletions chat/src/main/java/greencity/mapping/ChatRoomMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package greencity.mapping;

import greencity.dto.ChatRoomDto;
import greencity.entity.ChatRoom;
import greencity.entity.Participant;
import org.modelmapper.AbstractConverter;
import org.springframework.stereotype.Component;
import java.util.stream.Collectors;

@Component
public class ChatRoomMapper extends AbstractConverter<ChatRoomDto, ChatRoom> {
@Override
protected ChatRoom convert(ChatRoomDto chatRoomDto) {
return ChatRoom.builder()
.id(chatRoomDto.getId())
.name(chatRoomDto.getName())
.owner(Participant.builder().id(chatRoomDto.getOwnerId()).build())
.type(chatRoomDto.getChatType())
.chatStatus(chatRoomDto.getChatStatus())
.tariffId(chatRoomDto.getTariffId())
.participants(chatRoomDto.getParticipants().stream()
.map(participant -> Participant.builder()
.id(participant.getId())
.build())
.collect(Collectors.toSet()))
.logo(chatRoomDto.getLogo())
.build();
}
}
3 changes: 2 additions & 1 deletion chat/src/main/java/greencity/repository/ChatRoomRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ List<ChatRoom> findByParticipantsAndStatus(@Param("participants") Set<Participan
+ " WHERE p IN :participant"
+ " AND cr.messages IS NOT EMPTY"
+ " AND cr.type = :chatType")
List<ChatRoom> findGroupChats(@Param("participant") Participant participant, @Param("chatType") ChatType chatType);
List<ChatRoom> findChatRoomsByChatType(@Param("participant") Participant participant,
@Param("chatType") ChatType chatType);

/**
* {@inheritDoc}
Expand Down
32 changes: 7 additions & 25 deletions chat/src/main/java/greencity/service/ChatRoomService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package greencity.service;

import java.util.List;
import java.util.Set;
import greencity.dto.ChatRoomDto;
import greencity.dto.GroupChatRoomCreateDto;
import greencity.dto.LeaveChatDto;
import greencity.dto.PageableDto;
import greencity.dto.LocationsDto;
import greencity.entity.ChatMessage;
Expand All @@ -22,34 +20,18 @@ public interface ChatRoomService {
*/
List<ChatRoomDto> findAllByParticipantName(String name);

/**
* Method to find all {@link ChatRoom}'s by {@link Participant}/{@code User} and
* {@link ChatType}.
*
* @param participants {@link Set} of {@link Participant}'s that are in certain
* rooms.
* @param chatType {@link ChatType} room type.
* @return list of {@link ChatRoom} instances.
*/
List<ChatRoomDto> findAllRoomsByParticipantsAndStatus(Set<Participant> participants, ChatType chatType);

/**
* Method to find {@link ChatRoom} by it's id.
*
* @param id {@link ChatRoom} id.
* @return {@link ChatRoom} instance.
*/
ChatRoomDto findChatRoomById(Long id);

/**
* {@inheritDoc}
*/
List<ChatRoomDto> findGroupByParticipants(List<Long> id, String name, String chatName);
ChatRoomDto findChatRoomById(Long id, String name);

/**
* {@inheritDoc}
*/
List<ChatRoomDto> findGroupChatRooms(Participant participant, ChatType chatType);
List<ChatRoomDto> findChatRoomsByChatType(Participant participant, ChatType chatType);

/**
* {@inheritDoc}
Expand Down Expand Up @@ -79,12 +61,12 @@ public interface ChatRoomService {
/**
* {@inheritDoc}
*/
void deleteParticipantsFromChatRoom(ChatRoomDto chatRoomDto);
void deleteParticipantsFromChatRoom(ChatRoomDto chatRoomDto, Long userId);

/**
* {@inheritDoc}
*/
void updateChatRoom(ChatRoomDto chatRoomDto);
void updateChatRoom(ChatRoomDto chatRoomDto, Long userId);

/**
* {@inheritDoc}
Expand All @@ -94,12 +76,12 @@ public interface ChatRoomService {
/**
* {@inheritDoc}
*/
void leaveChatRoom(LeaveChatDto leaveChatDto);
void leaveChatRoom(ChatRoomDto chatRoomDto, Long userId);

/**
* {@inheritDoc}
*/
void findPrivateByParticipantsForSockets(Long id, Long currentUserId);
void findSystemChatByParticipantsForSockets(Long id, Long currentUserId);

/**
* Method deletes all {@link ChatMessage}s from chatroom.
Expand All @@ -123,7 +105,7 @@ public interface ChatRoomService {
* @param locationId The ID of the location for which to retrieve the tariff ID.
* @return The tariff ID associated with the specified location ID.
*/
Long getTariffIdByLocationId(Long locationId);
List<Long> getTariffIdByLocationId(Long locationId);

/**
* Retrieves a pageable list of active chat rooms for the admin associated with
Expand Down
Loading
Loading