Skip to content

Commit

Permalink
Merge pull request #133 from dd-jiyun/main
Browse files Browse the repository at this point in the history
Swagger ์„œ๋ฒ„ ๋ชฉ๋ก ์ถ”๊ฐ€
  • Loading branch information
dd-jiyun authored Mar 1, 2024
2 parents 12f6a97 + 38df31d commit 4923aee
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {

@Value("${server}")
private String serverUrl;

@Bean
public OpenAPI openAPI() {
Info info = new Info()
Expand All @@ -33,6 +38,8 @@ public OpenAPI openAPI() {
);

return new OpenAPI()
.addServersItem(new Server().url("http://localhost:8080").description("Local Server"))
.addServersItem(new Server().url(serverUrl).description("Production Server"))
.addSecurityItem(securityRequirement)
.components(components)
.info(info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
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;
Expand All @@ -36,8 +37,40 @@ public class MessageController {
@ApiResponse(responseCode = "500", description = "๊ด€๋ฆฌ์ž ๋ฌธ์˜")
})
public ResponseEntity<String> writeMessage(@RequestBody MessageDTO.Request request,
@AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.status(HttpStatus.CREATED).body(messageService.writeMessage(request, userDetails.getUsername()));
@AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.status(HttpStatus.CREATED)
.body(messageService.writeMessage(request, userDetails.getUsername()));
}

//์ชฝ์ง€ํ•จ ์ „์ฒด ์กฐํšŒ
@GetMapping("/all")
@Operation(
summary = "์ „์ฒด ๋ฉ”์‹œ์ง€ ์กฐํšŒ",
description = "์ „์ฒด ๋ฉ”์‹œ์ง€๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.",
responses = {
@ApiResponse(responseCode = "200", description = "์š”์ฒญ ์„ฑ๊ณต"),
@ApiResponse(responseCode = "400", description = "์ž˜๋ชป๋œ ์š”์ฒญ"),
@ApiResponse(responseCode = "500", description = "๊ด€๋ฆฌ์ž ๋ฌธ์˜")
})
public ResponseEntity<List<MessageDTO.Preview>> getAllMessagePreview(
@AuthenticationPrincipal UserDetails userDetails) {
return new ResponseEntity<>(messageService.getAllMessagePreview(userDetails.getUsername()), HttpStatus.OK);
}

//์ชฝ์ง€ํ•จ ์„ธ๋ถ€ ์กฐํšŒ ( ์ฃผ๊ณ  ๋ฐ›์€ ์‚ฌ์šฉ์ž์™€์˜ ๋Œ€ํ™” ๋‚ด์šฉ์„ ๋ฟŒ๋ ค์ค„ ์ˆ˜ ์žˆ๋Š” api)
@GetMapping("/{selectedUserId}")
@Operation(
summary = "๋ฉ”์‹œ์ง€ ์„ธ๋ถ€ ์กฐํšŒ",
description = "๋ฉ”์‹œ์ง€ ์„ธ๋ถ€๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.",
responses = {
@ApiResponse(responseCode = "200", description = "์š”์ฒญ ์„ฑ๊ณต"),
@ApiResponse(responseCode = "400", description = "์ž˜๋ชป๋œ ์š”์ฒญ"),
@ApiResponse(responseCode = "500", description = "๊ด€๋ฆฌ์ž ๋ฌธ์˜")
})
public ResponseEntity<List<MessageDTO.Response>> getBothMessages (
@PathVariable Long selectedUserId,
@AuthenticationPrincipal UserDetails userDetails) {
return new ResponseEntity<>(messageService.getBothMessages(userDetails.getUsername(),selectedUserId), HttpStatus.OK);
}

@GetMapping("/receiver")
Expand All @@ -49,7 +82,8 @@ public ResponseEntity<String> writeMessage(@RequestBody MessageDTO.Request reque
@ApiResponse(responseCode = "400", description = "์ž˜๋ชป๋œ ์š”์ฒญ"),
@ApiResponse(responseCode = "500", description = "๊ด€๋ฆฌ์ž ๋ฌธ์˜")
})
public ResponseEntity<List<MessageDTO.Response>> getMessagesByReceiver(@AuthenticationPrincipal UserDetails userDetails) {
public ResponseEntity<List<MessageDTO.Response>> getMessagesByReceiver(
@AuthenticationPrincipal UserDetails userDetails) {
return new ResponseEntity<>(messageService.getMessagesByReceiver(userDetails.getUsername()), HttpStatus.OK);
}

Expand All @@ -62,13 +96,26 @@ public ResponseEntity<List<MessageDTO.Response>> getMessagesByReceiver(@Authenti
@ApiResponse(responseCode = "400", description = "์ž˜๋ชป๋œ ์š”์ฒญ"),
@ApiResponse(responseCode = "500", description = "๊ด€๋ฆฌ์ž ๋ฌธ์˜")
})
public ResponseEntity<List<MessageDTO.Response>> getMessagesBySender(@AuthenticationPrincipal UserDetails userDetails) {
public ResponseEntity<List<MessageDTO.Response>> getMessagesBySender(
@AuthenticationPrincipal UserDetails userDetails) {
return new ResponseEntity<>(messageService.getMessagesBySender(userDetails.getUsername()), HttpStatus.OK);
}

//๋ฉ”์„ธ์ง€ ์‚ญ์ œ (sender์ธ ์œ ์ €๊ฐ€ receiver์œ ์ €์™€ ํ•œ ๋Œ€ํ™”๋‚ด์šฉ์„ ์‚ญ์ œ)
//์ชฝ์ง€ํ•จ ์ „์ฒด ์กฐํšŒ
//์ชฝ์ง€ํ•จ ์„ธ๋ถ€ ์กฐํšŒ ( ์ฃผ๊ณ  ๋ฐ›์€ ์‚ฌ์šฉ์ž์™€์˜ ๋Œ€ํ™” ๋‚ด์šฉ์„ ๋ฟŒ๋ ค์ค„ ์ˆ˜ ์žˆ๋Š” api)
//TODO : ๋ฉ”์„ธ์ง€ ์‚ญ์ œ (sender์ธ ์œ ์ €๊ฐ€ receiver์œ ์ €์™€ ํ•œ ๋Œ€ํ™”๋‚ด์šฉ์„ ์‚ญ์ œ)
// @DeleteMapping("/{messageId}")
// @Operation(
// summary = "๋ฉ”์‹œ์ง€ ์‚ญ์ œ",
// description = "๋ฉ”์‹œ์ง€๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.",
// responses = {
// @ApiResponse(responseCode = "200", description = "์š”์ฒญ ์„ฑ๊ณต"),
// @ApiResponse(responseCode = "400", description = "์ž˜๋ชป๋œ ์š”์ฒญ"),
// @ApiResponse(responseCode = "500", description = "๊ด€๋ฆฌ์ž ๋ฌธ์˜")
// })
// public ResponseEntity<String> deleteMessage(@PathVariable Long messageId,
// @AuthenticationPrincipal UserDetails userDetails) {
// messageService.deleteMessage(messageId, userDetails.getUsername());
// return ResponseEntity.ok("๋ฉ”์‹œ์ง€ ์‚ญ์ œ ์„ฑ๊ณต");
// }

}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.example.titto_backend.auth.domain.User;
import com.example.titto_backend.common.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand Down Expand Up @@ -32,11 +31,11 @@ public class Message extends BaseEntity {
@Column(name = "message_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sender_id")
private User sender;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "receiver_id")
private User receiver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,31 @@ public Response(Message message) {
this.senderNickname = message.getSenderNickname();
}
}

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(description = "๋ฉ”์„ธ์ง€ํ•จ ์กฐํšŒ")
public static class Preview {
@Schema(description = "๋ฉ”์‹œ์ง€ ID")
private Long id;

@Schema(description = "๋‚ด์šฉ")
private String content;

@Schema(description = "๋ณด๋‚ธ ์‚ฌ๋žŒ ๋‹‰๋„ค์ž„")
private String senderNickname;

@Schema(description = "๋ณด๋‚ธ ์‹œ๊ฐ„")
private String sentAt;

public Preview(Message message) {
this.id = message.getId();
this.content = message.getContent();
this.senderNickname = message.getSenderNickname();
this.sentAt = message.getSentAt().toString();
}
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ public interface MessageRepository extends JpaRepository<Message, Long> {
List<Message> findAllByReceiver(User user);

List<Message> findAllBySender(User user);

// user id๋ฅผ ๋ฐ›๊ณ  receiver์ด๊ฑฐ๋‚˜ sender์ธ ๋ฉ”์‹œ์ง€๋ฅผ ์กฐํšŒ
List<Message> findAllByReceiverOrSender(User receiver, User sender);

// selectedUserId๋ฅผ ๋ฐ›๊ณ  ์ž์‹ ๊ณผ ์„œ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›์€ ๋ฉ”์‹œ์ง€๋ฅผ ์กฐํšŒ
List<Message> findAllBySenderAndReceiverOrReceiverAndSender(User sender, User receiver, User receiver2,
User sender2);
// isDeleted๊ฐ€ false์ธ ๋ฉ”์‹œ์ง€๋งŒ ์กฐํšŒ
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,36 @@ public String writeMessage(MessageDTO.Request request, String email) throws Cust
}

@Transactional
public List<Response> getMessagesByReceiver(String email) {
public List<Response> getBothMessages(String email, Long selectedUserId) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

User selectedUser = userRepository.findById(selectedUserId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Message> messages = messageRepository.findAllBySenderAndReceiverOrReceiverAndSender(user, selectedUser, user, selectedUser);
return convertMessagesToDTO(messages);
}

@Transactional
public List<MessageDTO.Preview> getAllMessagePreview(String email) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Message> messages = messageRepository.findAllByReceiver(user);
return convertMessagesToPreviewDTO(messages);
}

@Transactional
public List<MessageDTO.Response> getMessagesByReceiver(String email) {
User receiver = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Message> messages = messageRepository.findAllByReceiver(receiver);
return convertMessagesToDTO(messages);
}


@Transactional
public List<Response> getMessagesBySender(String email) {
User sender = userRepository.findByEmail(email)
Expand All @@ -57,10 +79,27 @@ public List<Response> getMessagesBySender(String email) {
return convertMessagesToDTO(messages);
}

/**1 : ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž์™€ ๊ด€๋ จ๋œ ๋ฉ”์„ธ์ง€์ธ์ง€
2 : sender ์ธ์ง€ receiver ์ธ์ง€
3 : isDeleted ๊ฐ€ false ์ธ์ง€ true ์ธ์ง€ **/
@Transactional
public void deleteMessage(Long messageId, String email) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
messageRepository.deleteById(messageId);
}

private List<MessageDTO.Response> convertMessagesToDTO(List<Message> messages) {
return messages.stream()
.map(MessageDTO.Response::new)
.toList();
}

private List<MessageDTO.Preview> convertMessagesToPreviewDTO(List<Message> messages) {
return messages.stream()
.map(MessageDTO.Preview::new)
.toList();
}

}

2 changes: 2 additions & 0 deletions Titto_Backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ spring:
port: 6379
host: redis

server: ${PRODUCTION_SERVER_URL}

# JWT
jwt:
secret: ${JWT_SECRET}
Expand Down

0 comments on commit 4923aee

Please sign in to comment.