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

Chat 57 api topic #17

Merged
merged 20 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
28 changes: 18 additions & 10 deletions src/main/java/com/chat/yourway/config/openapi/OpenApiExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
public class OpenApiExamples {

public static final String NEW_CONTACT = """
{ "nickname": "newNickname",
"email": "newEmail",
"avatarId" 1,
"password": "newPassword"}""";
{
"nickname": "newNickname",
"email": "[email protected]",
"avatarId": 1,
"password": "Password-123"
}""";

public static final String LOGIN = """
{"email": "[email protected]",
"password": "user"}""";
{
"email": "[email protected]",
"password": "Password-123"
}""";

public static final String CHANGE_PASSWORD = """
{"oldPassword": "12345",
"newPassword": "qwerty"}""";
{
"oldPassword": "Password-123",
"newPassword": "Password-321"
}""";

public static final String EDIT_CONTACT_PROFILE = """
{ "nickname": "editNickname",
"avatarId" 1}""";
{
"nickname": "editNickname",
"avatarId": 2
}""";

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@ public class OpenApiMessages {
public static final String SUCCESSFULLY_SEND_REQUEST_RESTORE_PASSWORD = "Contact successfully sent request to restored password";
public static final String SUCCESSFULLY_RESTORED_PASSWORD = "Contact successfully restored password";
public static final String SUCCESSFULLY_UPDATED_CONTACT_PROFILE = "Contact profile successfully updated";
public static final String SUCCESSFULLY_SUBSCRIBED = "Contact successfully subscribed to the topic";
public static final String TOPIC_NOT_FOUND = "Topic wasn't found in repository";
public static final String ALREADY_SUBSCRIBED = "Contact already subscribed to the topic";
public static final String SUCCESSFULLY_UNSUBSCRIBED = "Contact successfully unsubscribed from the topic";
public static final String CONTACT_WASNT_SUBSCRIBED = "Contact wasn't subscribed to the topic";
public static final String SUCCESSFULLY_CREATED_TOPIC = "Topic successfully created";
public static final String SUCCESSFULLY_FOUND_TOPIC = "Topic successfully found";
public static final String SUCCESSFULLY_DELETE_TOPIC = "Topic successfully deleted";
public static final String TOPIC_NOT_ACCESS = "Topic operation not access";

}
8 changes: 4 additions & 4 deletions src/main/java/com/chat/yourway/controller/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class ChatController {
@MessageMapping("/application")
@SendTo("/topic")
public MessageResponseDto sendToTopic(ReceivedMessageDto message, Principal principal) {
String username = principal.getName();
return chatMessageService.sendToTopic(message, username);
String email = principal.getName();
return chatMessageService.sendToTopic(message, email);
}

@MessageMapping("/private")
public MessageResponseDto sendToUser(@Payload ReceivedMessageDto message, Principal principal) {
String username = principal.getName();
return chatMessageService.sendToUser(message, username);
String email = principal.getName();
return chatMessageService.sendToUser(message, email);
}

}
50 changes: 30 additions & 20 deletions src/main/java/com/chat/yourway/controller/ContactController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
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 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;
Expand All @@ -14,31 +19,36 @@
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import static com.chat.yourway.config.openapi.OpenApiMessages.*;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
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;

@Tag(name = "Contact")
@RestController
@RequestMapping("/contacts")
@RequiredArgsConstructor
public class ContactController {
private final ContactService contactService;

@Operation(summary = "Registration a new contact",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_UPDATED_CONTACT_PROFILE),
@ApiResponse(responseCode = "404", description = CONTACT_NOT_FOUND,
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)
public void editContactProfile(@Valid @RequestBody EditContactProfileRequestDto editContactProfileRequestDto,
@AuthenticationPrincipal UserDetails userDetails) {
contactService.updateContactProfile(editContactProfileRequestDto, userDetails);
}
private final ContactService contactService;

@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)))
},
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);
}

}
134 changes: 134 additions & 0 deletions src/main/java/com/chat/yourway/controller/TopicController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.chat.yourway.controller;

import static com.chat.yourway.config.openapi.OpenApiMessages.ALREADY_SUBSCRIBED;
import static com.chat.yourway.config.openapi.OpenApiMessages.CONTACT_UNAUTHORIZED;
import static com.chat.yourway.config.openapi.OpenApiMessages.CONTACT_WASNT_SUBSCRIBED;
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_CREATED_TOPIC;
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_DELETE_TOPIC;
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_FOUND_TOPIC;
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_SUBSCRIBED;
import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_UNSUBSCRIBED;
import static com.chat.yourway.config.openapi.OpenApiMessages.TOPIC_NOT_ACCESS;
import static com.chat.yourway.config.openapi.OpenApiMessages.TOPIC_NOT_FOUND;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import com.chat.yourway.dto.response.ApiErrorResponseDto;
import com.chat.yourway.dto.response.ContactResponseDto;
import com.chat.yourway.dto.response.TopicResponseDto;
import com.chat.yourway.service.interfaces.TopicService;
import com.chat.yourway.service.interfaces.TopicSubscriberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.security.Principal;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/topics")
@RequiredArgsConstructor
@Tag(name = "Topic")
public class TopicController {

private final TopicService topicService;
private final TopicSubscriberService topicSubscriberService;

@Operation(summary = "Create new topic",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_CREATED_TOPIC),
@ApiResponse(responseCode = "403", description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@PostMapping(path = "/create", produces = APPLICATION_JSON_VALUE)
public TopicResponseDto create(String topicName, Principal principal) {
String email = principal.getName();
return topicService.create(topicName, email);
}

@Operation(summary = "Find topic by id",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_FOUND_TOPIC),
@ApiResponse(responseCode = "404", description = TOPIC_NOT_FOUND,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(responseCode = "403", description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@GetMapping(path = "/{id}", produces = APPLICATION_JSON_VALUE)
public TopicResponseDto findById(@PathVariable Integer id) {
return topicService.findById(id);
}

@Operation(summary = "Find all topics",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_FOUND_TOPIC),
@ApiResponse(responseCode = "403", description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@GetMapping(produces = APPLICATION_JSON_VALUE)
public List<TopicResponseDto> findAll() {
return topicService.findAll();
}

@Operation(summary = "Delete by creator and topic id",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_DELETE_TOPIC),
@ApiResponse(responseCode = "409", description = TOPIC_NOT_ACCESS,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(responseCode = "403", description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@DeleteMapping(path = "/{id}", produces = APPLICATION_JSON_VALUE)
public void deleteByCreator(@PathVariable Integer id, Principal principal) {
String email = principal.getName();
topicService.deleteByCreator(id, email);
}

@Operation(summary = "Subscribe to the topic",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_SUBSCRIBED),
@ApiResponse(responseCode = "409", description = ALREADY_SUBSCRIBED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(responseCode = "403", description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@PostMapping(path = "/subscribe/{topicId}", produces = APPLICATION_JSON_VALUE)
public void subscribeToTopic(@PathVariable Integer topicId, Principal principal) {
String email = principal.getName();
topicSubscriberService.subscribeToTopicById(email, topicId);
}

@Operation(summary = "Unsubscribe from the topic",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_UNSUBSCRIBED),
@ApiResponse(responseCode = "404", description = CONTACT_WASNT_SUBSCRIBED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
@ApiResponse(responseCode = "403", description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@PatchMapping(path = "/unsubscribe/{topicId}", produces = APPLICATION_JSON_VALUE)
public void unsubscribeFromTopic(@PathVariable Integer topicId, Principal principal) {
String email = principal.getName();
topicSubscriberService.unsubscribeFromTopicById(email, topicId);
}

@Operation(summary = "Find all subscribers to topic by topicId",
responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_FOUND_TOPIC),
@ApiResponse(responseCode = "403", description = CONTACT_UNAUTHORIZED,
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
})
@GetMapping(path = "/subscribers/{topicId}", produces = APPLICATION_JSON_VALUE)
public List<ContactResponseDto> findAllSubscribersByTopicId(@PathVariable Integer topicId) {
return topicSubscriberService.findAllSubscribersByTopicId(topicId);
}

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

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class ContactResponseDto {

private Integer id;

private String nickname;

private String email;

private Byte avatarId;

private Boolean isActive;

private Boolean isPrivate;

}
28 changes: 28 additions & 0 deletions src/main/java/com/chat/yourway/dto/response/TopicResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.chat.yourway.dto.response;

import java.time.LocalDateTime;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@ToString
public class TopicResponseDto {

private Integer id;

private String topicName;

private String createdBy;

private LocalDateTime createdAt;

private Set<TopicSubscriberResponseDto> topicSubscribers;

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

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@ToString
public class TopicSubscriberResponseDto {

private ContactResponseDto contact;

private LocalDateTime subscribeAt;

private LocalDateTime unsubscribeAt;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.chat.yourway.exception;

public class ContactAlreadySubscribedToTopicException extends RuntimeException {

public ContactAlreadySubscribedToTopicException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.chat.yourway.exception;

public class TopicAccessException extends RuntimeException {
public TopicAccessException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.chat.yourway.exception;

public class TopicNotFoundException extends RuntimeException {
public TopicNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.chat.yourway.exception;

public class TopicSubscriberNotFoundException extends RuntimeException {

public TopicSubscriberNotFoundException(String message) {
super(message);
}
}
Loading
Loading