Skip to content

Commit

Permalink
Merge pull request #17 from Chat-Your-Way/CHAT-57--API-topic
Browse files Browse the repository at this point in the history
Chat 57  api topic
  • Loading branch information
Troha7 authored Sep 20, 2023
2 parents e42054b + 698f971 commit 52a11ac
Show file tree
Hide file tree
Showing 37 changed files with 1,632 additions and 141 deletions.
33 changes: 23 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,34 @@
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
}""";

public static final String NEW_TOPIC = """
{
"topicName": "new chat topic",
"tags": ["#tag1", "#tag2"]
}""";
}
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);
}

}
153 changes: 153 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,153 @@
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.config.openapi.OpenApiExamples;
import com.chat.yourway.dto.request.TopicRequestDto;
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.ExampleObject;
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.RequestBody;
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)))
},
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
content = @Content(schema = @Schema(implementation = TopicRequestDto.class),
examples = @ExampleObject(value = OpenApiExamples.NEW_TOPIC,
description = "New Topic"))))
@PostMapping(path = "/create", produces = APPLICATION_JSON_VALUE)
public TopicResponseDto create(@RequestBody TopicRequestDto topicRequestDto, Principal principal) {
String email = principal.getName();
return topicService.create(topicRequestDto, 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(path = "/all", 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);
}

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

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

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 TopicRequestDto {

private String topicName;

private Set<String> tags;

}
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;

}
31 changes: 31 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,31 @@
package com.chat.yourway.dto.response;

import com.chat.yourway.model.Tag;
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<Tag> tags;

private Set<TopicSubscriberResponseDto> topicSubscribers;

}
Loading

0 comments on commit 52a11ac

Please sign in to comment.