Skip to content

Commit

Permalink
Merge pull request #47 from Chat-Your-Way/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
DmytroTeliukov authored Dec 5, 2023
2 parents 6eb0857 + 1d960b3 commit 5a38be2
Show file tree
Hide file tree
Showing 25 changed files with 371 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ public class OpenApiExamples {
"nickname": "editNickname",
"avatarId": 2
}""";

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class OpenApiMessages {
"Contact successfully restored password";
public static final String SUCCESSFULLY_UPDATED_CONTACT_PROFILE =
"Contact profile successfully updated";
public static final String SUCCESSFULLY_RECEIVED_CONTACT_PROFILE =
"Contact profile successfully received";
public static final String SUCCESSFULLY_REPORTED_MESSAGE = "Message is reported successfully";
public static final String MESSAGE_NOT_FOUND = "Message wasn't found in repository";
public static final String MESSAGE_HAS_ALREADY_REPORTED = "You have already made report";
Expand All @@ -42,7 +44,8 @@ public class OpenApiMessages {
public static final String SUCCESSFULLY_UPDATED_TOPIC = "Topic successfully updated";
public static final String INVALID_VALUE = "Invalid value of request object field";
public static final String SUCCESSFULLY_FOUND_MESSAGE = "Message successfully found";

public static final String SUCCESSFULLY_PERMITTED_SENDING_PRIVATE_MESSAGES = "Successfully permitted sending private messages";
public static final String SUCCESSFULLY_PROHIBITED_SENDING_PRIVATE_MESSAGES = "Successfully prohibited sending private messages";

public static final String SUCCESSFULLY_ADD_TOPIC_TO_FAVOURITE =
"Topic added to favourite successfully";
Expand Down
106 changes: 88 additions & 18 deletions src/main/java/com/chat/yourway/controller/ContactController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.chat.yourway.controller;

import static com.chat.yourway.config.openapi.OpenApiMessages.CONTACT_NOT_FOUND;
import static com.chat.yourway.config.openapi.OpenApiMessages.CONTACT_UNAUTHORIZED;
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_UPDATED_CONTACT_PROFILE;
import static com.chat.yourway.config.openapi.OpenApiMessages.*;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import com.chat.yourway.config.openapi.OpenApiExamples;
import com.chat.yourway.dto.request.EditContactProfileRequestDto;
import com.chat.yourway.dto.response.ApiErrorResponseDto;
import com.chat.yourway.dto.response.ContactProfileResponseDto;
import com.chat.yourway.service.interfaces.ContactService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -19,10 +18,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@Tag(name = "Contact")
@RestController
Expand All @@ -32,23 +28,97 @@ public class ContactController {

private final ContactService contactService;

@Operation(summary = "Edit contact profile",
@Operation(
summary = "Edit contact profile",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_UPDATED_CONTACT_PROFILE),
@ApiResponse(responseCode = "404", description = CONTACT_NOT_FOUND,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(responseCode = "403", description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_UPDATED_CONTACT_PROFILE),
@ApiResponse(
responseCode = "404",
description = CONTACT_NOT_FOUND,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(
responseCode = "403",
description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
},
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
content = @Content(schema = @Schema(implementation = EditContactProfileRequestDto.class),
examples = @ExampleObject(value = OpenApiExamples.EDIT_CONTACT_PROFILE,
description = "Edit Contact profile"))))
@PatchMapping(path = "/profile", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
requestBody =
@io.swagger.v3.oas.annotations.parameters.RequestBody(
content =
@Content(
schema = @Schema(implementation = EditContactProfileRequestDto.class),
examples =
@ExampleObject(
value = OpenApiExamples.EDIT_CONTACT_PROFILE,
description = "Edit Contact profile"))))
@PatchMapping(
path = "/profile",
consumes = APPLICATION_JSON_VALUE,
produces = APPLICATION_JSON_VALUE)
public void editContactProfile(
@Valid @RequestBody EditContactProfileRequestDto editContactProfileRequestDto,
@AuthenticationPrincipal UserDetails userDetails) {
contactService.updateContactProfile(editContactProfileRequestDto, userDetails);
}

@Operation(
summary = "Get contact profile",
responses = {
@ApiResponse(
responseCode = "200",
description = SUCCESSFULLY_RECEIVED_CONTACT_PROFILE,
content = @Content(schema = @Schema(implementation = ContactProfileResponseDto.class))),
@ApiResponse(
responseCode = "404",
description = CONTACT_NOT_FOUND,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(
responseCode = "403",
description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@GetMapping(path = "/profile", produces = APPLICATION_JSON_VALUE)
public ContactProfileResponseDto getContactProfile(
@AuthenticationPrincipal UserDetails userDetails) {
return contactService.getContactProfile(userDetails);
}

@Operation(
summary = "Prohibit sending private message",
responses = {
@ApiResponse(
responseCode = "200",
description = SUCCESSFULLY_PROHIBITED_SENDING_PRIVATE_MESSAGES),
@ApiResponse(
responseCode = "403",
description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(
responseCode = "404",
description = CONTACT_NOT_FOUND,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@PatchMapping(path = "/message/send/prohibit")
public void prohibitSendingPrivateMessages(@AuthenticationPrincipal UserDetails userDetails) {
contactService.prohibitSendingPrivateMessages(userDetails);
}

@Operation(
summary = "Permit sending private message",
responses = {
@ApiResponse(
responseCode = "200",
description = SUCCESSFULLY_PERMITTED_SENDING_PRIVATE_MESSAGES),
@ApiResponse(
responseCode = "403",
description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(
responseCode = "404",
description = CONTACT_NOT_FOUND,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@PatchMapping(path = "/message/send/permit")
public void permitSendingPrivateMessages(@AuthenticationPrincipal UserDetails userDetails) {
contactService.permitSendingPrivateMessages(userDetails);
}
}
50 changes: 3 additions & 47 deletions src/main/java/com/chat/yourway/controller/TopicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,59 +345,15 @@ public List<TopicResponseDto> findAllFavouriteTopics(
return topicService.findAllFavouriteTopics(userDetails);
}

@Operation(
summary = "Prohibit sending private message",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_FOUND_TOPIC),
@ApiResponse(
responseCode = "403",
description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(
responseCode = "404",
description = TOPIC_NOT_FOUND,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(
responseCode = "409",
description = USER_DID_NOT_SUBSCRIBED_TO_TOPIC,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@GetMapping(path = "/{topic-id}/message/send/prohibit")
public void prohibitSendingPrivateMessages(
@PathVariable("topic-id") int topicId, @AuthenticationPrincipal UserDetails userDetails) {
topicSubscriberService.prohibitSendingPrivateMessages(topicId, userDetails);
}

@Operation(
summary = "Permit sending private message",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_FOUND_TOPIC),
@ApiResponse(
responseCode = "403",
description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(
responseCode = "404",
description = TOPIC_NOT_FOUND,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(
responseCode = "409",
description = USER_DID_NOT_SUBSCRIBED_TO_TOPIC,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@GetMapping(path = "/{topic-id}/message/send/permit")
public void permitSendingPrivateMessages(
@PathVariable("topic-id") int topicId, @AuthenticationPrincipal UserDetails userDetails) {
topicSubscriberService.permitSendingPrivateMessages(topicId, userDetails);
}

@Operation(
summary = "List of popular topics",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_FOUND_TOPIC)
})
@GetMapping(path = "/popular/public")
public List<TopicResponseDto>findAllPopularPublicTopics() {
public List<TopicResponseDto> findAllPopularPublicTopics() {
return topicService.findPopularPublicTopics();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.chat.yourway.dto.response;

import lombok.*;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class ContactProfileResponseDto {
private String nickname;
private String email;
private Byte avatarId;
private Boolean hasPermissionSendingPrivateMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public class ContactResponseDto {

private Boolean isPrivate;

private boolean isPermittedSendingPrivateMessage;
}
3 changes: 3 additions & 0 deletions src/main/java/com/chat/yourway/model/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class Contact implements UserDetails {
@Column(name = "role", nullable = false, length = 32)
private Role role;

@Column(name = "is_permitted_sending_private_message")
private boolean isPermittedSendingPrivateMessage;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority(role.name()));
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/chat/yourway/model/TopicSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,4 @@ public class TopicSubscriber {
@Column(name = "is_favourite_topic")
private boolean isFavouriteTopic;

@Column(name = "is_permitted_sending_message")
private boolean isPermittedSendingMessage;
}
12 changes: 12 additions & 0 deletions src/main/java/com/chat/yourway/repository/ContactRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand All @@ -20,4 +22,14 @@ public interface ContactRepository extends JpaRepository<Contact, Integer> {

boolean existsByEmailIgnoreCase(String email);

@Modifying
@Query(
nativeQuery = true,
value =
"UPDATE chat.contact "
+ "SET is_permitted_sending_private_message = :isPermittedSendingPrivateMessage "
+ "WHERE email = :contactEmail")
void updatePermissionSendingPrivateMessageByContactEmail(
@Param("contactEmail") String contactEmail,
@Param("isPermittedSendingPrivateMessage") boolean isPermittedSendingPrivateMessage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ WHERE to_tsvector('english', t.topic_name) @@ to_tsquery('english', :query)

@Query(nativeQuery = true, value =
"SELECT t.*, COUNT(ts.id) AS ts_count, COUNT(m.id) AS m_count " +
"FROM topic t " +
"JOIN topic_subscriber ts ON t.id = ts.topic_id " +
"JOIN message m ON t.id = m.topic_id " +
"FROM chat.topic t " +
"JOIN chat.topic_subscriber ts ON t.id = ts.topic_id " +
"JOIN chat.message m ON t.id = m.topic_id " +
"WHERE t.is_public = true " +
"GROUP BY t.id " +
"ORDER BY ts_count DESC, m_count DESC")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,8 @@ void updateFavouriteTopicStatusByTopicIdAndContactEmail(

boolean existsByTopicIdAndTopicCreatedBy(Integer topicId, String topicCreator);

@Modifying
@Query(
nativeQuery = true,
value =
"UPDATE chat.topic_subscriber "
+ "SET is_permitted_sending_message = :isPermittedSendingPrivateMessage "
+ "FROM chat.contact c "
+ "WHERE contact_id = c.id "
+ "AND topic_id = :topicId "
+ "AND c.email = :contactEmail")
void updatePermissionSendingPrivateMessageByTopicIdAndContactEmail(
@Param("topicId") int topicId,
@Param("contactEmail") String contactEmail,
@Param("isPermittedSendingPrivateMessage") boolean isPermittedSendingPrivateMessage);

@Query(
"SELECT CASE WHEN COUNT(ts) > 0 then true else false end from TopicSubscriber ts " +
"where ts.topic.id = :topicId and ts.isPermittedSendingMessage = false")
"where ts.topic.id = :topicId and ts.contact.isPermittedSendingPrivateMessage = false")
boolean checkIfExistProhibitionSendingPrivateMessage(@Param("topicId") Integer topicId);
}
51 changes: 48 additions & 3 deletions src/main/java/com/chat/yourway/service/ContactServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.chat.yourway.dto.request.ContactRequestDto;
import com.chat.yourway.dto.request.EditContactProfileRequestDto;
import com.chat.yourway.exception.ContactNotFoundException;
import com.chat.yourway.exception.PasswordsAreNotEqualException;
import com.chat.yourway.exception.ValueNotUniqException;
import com.chat.yourway.dto.response.ContactProfileResponseDto;
import com.chat.yourway.exception.*;
import com.chat.yourway.model.Contact;
import com.chat.yourway.model.Role;
import com.chat.yourway.repository.ContactRepository;
Expand Down Expand Up @@ -90,4 +89,50 @@ public boolean isEmailExists(String email) {
return contactRepository.existsByEmailIgnoreCase(email);
}

@Override
public ContactProfileResponseDto getContactProfile(UserDetails userDetails) {
String email = userDetails.getUsername();
Contact contact =
contactRepository
.findByEmailIgnoreCase(email)
.orElseThrow(
() -> new ContactNotFoundException(String.format("Email %s wasn't found", email)));
ContactProfileResponseDto responseDto = new ContactProfileResponseDto();

responseDto.setNickname(contact.getNickname());
responseDto.setAvatarId(contact.getAvatarId());
responseDto.setEmail(email);
responseDto.setHasPermissionSendingPrivateMessage(contact.isPermittedSendingPrivateMessage());

return responseDto;
}

@Override
@Transactional
public void permitSendingPrivateMessages(UserDetails userDetails) {
boolean isPermittedSendingPrivateMessage = true;

changePermissionSendingPrivateMessages(userDetails, isPermittedSendingPrivateMessage);
}

@Override
@Transactional
public void prohibitSendingPrivateMessages(UserDetails userDetails) {
boolean isPermittedSendingPrivateMessage = false;

changePermissionSendingPrivateMessages(userDetails, isPermittedSendingPrivateMessage);
}

private void changePermissionSendingPrivateMessages(
UserDetails userDetails, boolean isPermittedSendingPrivateMessage) {
String contactEmail = userDetails.getUsername();

if (!contactRepository.existsByEmailIgnoreCase(contactEmail)) {
throw new ContactNotFoundException(
String.format("Contact with email [%s] is not found.", contactEmail));
}

contactRepository.updatePermissionSendingPrivateMessageByContactEmail(
contactEmail, isPermittedSendingPrivateMessage);
}
}
Loading

0 comments on commit 5a38be2

Please sign in to comment.