-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Chat-Your-Way/CHAT-57--API-topic
Chat 57 api topic
- Loading branch information
Showing
37 changed files
with
1,632 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"] | ||
}"""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
src/main/java/com/chat/yourway/controller/TopicController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/main/java/com/chat/yourway/dto/request/TopicRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/chat/yourway/dto/response/ContactResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/main/java/com/chat/yourway/dto/response/TopicResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
Oops, something went wrong.