Skip to content

Commit 2ca1aaf

Browse files
feat: add api & method for complaining topic
1 parent 0ca2a34 commit 2ca1aaf

File tree

5 files changed

+72
-19
lines changed

5 files changed

+72
-19
lines changed

src/main/java/com/chat/yourway/config/openapi/OpenApiMessages.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class OpenApiMessages {
5151
"Topic added to favourite successfully";
5252
public static final String SUCCESSFULLY_REMOVE_TOPIC_FROM_FAVOURITE =
5353
"Topic removed from favourite successfully";
54+
55+
public static final String SUCCESSFULLY_COMPLAIN_TOPIC =
56+
"Topic was complained successfully";
5457
public static final String USER_DID_NOT_SUBSCRIBED_TO_TOPIC =
5558
"User did not subscribe to topic";
5659

src/main/java/com/chat/yourway/controller/TopicController.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
package com.chat.yourway.controller;
22

3-
import static com.chat.yourway.config.openapi.OpenApiMessages.ALREADY_SUBSCRIBED;
4-
import static com.chat.yourway.config.openapi.OpenApiMessages.CONTACT_UNAUTHORIZED;
5-
import static com.chat.yourway.config.openapi.OpenApiMessages.CONTACT_WASNT_SUBSCRIBED;
6-
import static com.chat.yourway.config.openapi.OpenApiMessages.INVALID_VALUE;
7-
import static com.chat.yourway.config.openapi.OpenApiMessages.OWNER_CANT_UNSUBSCRIBED;
8-
import static com.chat.yourway.config.openapi.OpenApiMessages.RECIPIENT_EMAIL_NOT_EXIST;
9-
import static com.chat.yourway.config.openapi.OpenApiMessages.SEARCH_TOPIC_VALIDATION;
10-
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_ADD_TOPIC_TO_FAVOURITE;
11-
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_CREATED_TOPIC;
12-
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_DELETE_TOPIC;
13-
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_FOUND_TOPIC;
14-
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_REMOVE_TOPIC_FROM_FAVOURITE;
15-
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_SUBSCRIBED;
16-
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_UNSUBSCRIBED;
17-
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_UPDATED_TOPIC;
18-
import static com.chat.yourway.config.openapi.OpenApiMessages.TOPIC_NOT_ACCESS;
19-
import static com.chat.yourway.config.openapi.OpenApiMessages.TOPIC_NOT_FOUND;
20-
import static com.chat.yourway.config.openapi.OpenApiMessages.USER_DID_NOT_SUBSCRIBED_TO_TOPIC;
21-
import static com.chat.yourway.config.openapi.OpenApiMessages.VALUE_NOT_UNIQUE;
3+
import static com.chat.yourway.config.openapi.OpenApiMessages.*;
224
import static java.nio.charset.StandardCharsets.UTF_8;
235
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
246

@@ -355,5 +337,27 @@ public List<TopicResponseDto> findAllPopularPublicTopics() {
355337
return topicService.findPopularPublicTopics();
356338
}
357339

340+
@Operation(
341+
summary = "Complain topic",
342+
responses = {
343+
@ApiResponse(responseCode = "204", description = SUCCESSFULLY_COMPLAIN_TOPIC),
344+
@ApiResponse(
345+
responseCode = "403",
346+
description = CONTACT_UNAUTHORIZED,
347+
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
348+
@ApiResponse(
349+
responseCode = "404",
350+
description = TOPIC_NOT_FOUND,
351+
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
352+
@ApiResponse(
353+
responseCode = "409",
354+
description = USER_DID_NOT_SUBSCRIBED_TO_TOPIC,
355+
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
356+
})
357+
@PatchMapping("/{topic-id}/complain")
358+
@ResponseStatus(HttpStatus.NO_CONTENT)
359+
public void complainTopic(@AuthenticationPrincipal UserDetails userDetails, @PathVariable("topic-id") Integer topicId) {
360+
topicSubscriberService.complainTopic(topicId, userDetails);
361+
}
358362

359363
}

src/main/java/com/chat/yourway/repository/TopicSubscriberRepository.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,19 @@ void updateFavouriteTopicStatusByTopicIdAndContactEmail(
6767
"SELECT CASE WHEN COUNT(ts) > 0 then true else false end from TopicSubscriber ts " +
6868
"where ts.topic.id = :topicId and ts.contact.isPermittedSendingPrivateMessage = false")
6969
boolean checkIfExistProhibitionSendingPrivateMessage(@Param("topicId") Integer topicId);
70+
71+
@Modifying
72+
@Query(
73+
nativeQuery = true,
74+
value =
75+
"UPDATE chat.topic_subscriber "
76+
+ "SET has_complaint = :hasComplaint "
77+
+ "FROM chat.contact c "
78+
+ "WHERE contact_id = c.id "
79+
+ "AND topic_id = :topicId "
80+
+ "AND c.email = :contactEmail")
81+
void updateHasComplaintStatusByTopicIdAndContactEmail(
82+
@Param("topicId") int topicId,
83+
@Param("contactEmail") String contactEmail,
84+
@Param("hasComplaint") boolean hasComplaint);
7085
}

src/main/java/com/chat/yourway/service/TopicSubscriberServiceImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ public boolean hasProhibitionSendingPrivateMessages(Integer topicId) {
120120
return topicSubscriberRepository.checkIfExistProhibitionSendingPrivateMessage(topicId);
121121
}
122122

123+
@Override
124+
@Transactional
125+
public void complainTopic(Integer topicId, UserDetails userDetails) {
126+
String email = userDetails.getUsername();
127+
boolean hasComplaint = true;
128+
129+
if (!topicRepository.existsById(topicId)) {
130+
throw new TopicNotFoundException(String.format("Topic with id [%d] is not found.", topicId));
131+
} else if (!topicSubscriberRepository.existsByContactEmailAndTopicIdAndUnsubscribeAtIsNull(email, topicId)) {
132+
throw new NotSubscribedTopicException("You cannot complain topic because you did not subscribe before.");
133+
}
134+
135+
topicSubscriberRepository.updateHasComplaintStatusByTopicIdAndContactEmail(topicId, email, hasComplaint);
136+
}
137+
123138
private boolean isTopicCreator(Integer topicId, String topicCreator) {
124139
return topicSubscriberRepository.existsByTopicIdAndTopicCreatedBy(topicId, topicCreator);
125140
}

src/main/java/com/chat/yourway/service/interfaces/TopicSubscriberService.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,20 @@ public interface TopicSubscriberService {
7575
* @return true if sending private messages is prohibited, false otherwise
7676
*/
7777
boolean hasProhibitionSendingPrivateMessages(Integer topicId);
78+
79+
/**
80+
* Registers a complaint for a specific topic.
81+
*
82+
* This method allows a user to complain about a particular topic identified by its unique identifier.
83+
* The complaint details, such as the user's information, will be recorded for further investigation.
84+
*
85+
* @param topicId The unique identifier of the topic being complained about.
86+
* @param userDetails The details of the user lodging the complaint.
87+
* This should include relevant information like user ID, username, etc.
88+
* Ensure that the userDetails parameter is not null.
89+
*
90+
* @throws TopicNotFoundException If topic does not exist.
91+
* @throws NotSubscribedTopicException If contact does not subscribed to topic.
92+
*/
93+
void complainTopic(Integer topicId, UserDetails userDetails);
7894
}

0 commit comments

Comments
 (0)