Skip to content

Commit

Permalink
refactor : add attachment column
Browse files Browse the repository at this point in the history
  • Loading branch information
NameIsUser06 committed Dec 27, 2023
1 parent ccc4d4c commit 1a3fcd7
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Attachment {
private Long zIndex;

@Column
@Enumerated(EnumType.STRING)
private PostType postType;

@Column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
public class AttachmentController {

private final PostAttachmentService postAttachmentService;

@PostMapping
public ResponseEntity<String> createPost(@RequestPart("data") PostAttachmentRequest request, @RequestPart("file") MultipartFile file, HttpServletRequest httpServletRequest) {
return postAttachmentService.execute(request, file, httpServletRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
public class PostAttachmentRequest {
private String content;
private Long colorCode;
private Long zIndex;
private PostType postType;
private String xPosition;
private String yPosition;
private Long z;
private String x;
private String y;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.bssm.attachit.domain.attachment.presentation.dto.response;

import lombok.Builder;
import lombok.Getter;
import org.bssm.attachit.domain.attachment.domain.type.PostType;

@Getter
@Builder
public class AttachmentResponse {
private String content;
private String url;
private Long colorCode;
private Long zIndex;
private PostType postType;
private String xPosition;
private String yPosition;
private String name;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.bssm.attachit.domain.attachment.service;

import lombok.RequiredArgsConstructor;
import org.bssm.attachit.domain.attachment.domain.Attachment;
import org.bssm.attachit.domain.attachment.presentation.dto.response.AttachmentResponse;
import org.bssm.attachit.domain.attachment.repository.AttachmentRepository;
import org.bssm.attachit.global.properties.FileProperties;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -12,8 +13,20 @@
public class GetAttachmentListService {

private final AttachmentRepository attachmentRepository;
private final FileProperties fileProperties;

public List<Attachment> execute() {
return attachmentRepository.findAll();
public List<AttachmentResponse> execute() {
return attachmentRepository.findAll().stream().map(
attachment -> AttachmentResponse.builder()
.content(attachment.getContent())
.url(fileProperties.getUrl() + "/file?attachmentId=" + attachment.getId())
.name(attachment.getUser().getName())
.colorCode(attachment.getColorCode())
.postType(attachment.getPostType())
.xPosition(attachment.getXPosition())
.yPosition(attachment.getYPosition())
.zIndex(attachment.getZIndex())
.build()
).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.bssm.attachit.domain.attachment.domain.Attachment;
import org.bssm.attachit.domain.attachment.exception.FileNotFoundException;
import org.bssm.attachit.domain.attachment.presentation.dto.request.PostAttachmentRequest;
import org.bssm.attachit.domain.attachment.repository.AttachmentRepository;
import org.bssm.attachit.domain.user.domain.User;
Expand All @@ -24,26 +23,26 @@ public class PostAttachmentService {
private final FileSaveUtil fileSaveUtil;

public ResponseEntity<String> execute(PostAttachmentRequest request, MultipartFile file, HttpServletRequest httpServletRequest) {
if (file.isEmpty()) {
throw FileNotFoundException.EXCEPTION;
}

User user = userRepository.findByEmail(jwtUtil.extractEmail(httpServletRequest)).orElseThrow(
() -> UserNotFoundException.EXCEPTION
);

String path = fileSaveUtil.save(file);
String path = null;

if (!file.isEmpty()) {
path = fileSaveUtil.save(file);
}

attachmentRepository.save(
Attachment.builder()
.path(path)
.content(request.getContent())
.user(user)
.colorCode(request.getColorCode())
.zIndex(request.getZIndex())
.zIndex(request.getZ())
.postType(request.getPostType())
.xPosition(request.getXPosition())
.yPosition(request.getYPosition())
.xPosition(request.getX())
.yPosition(request.getY())
.build()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
@ConfigurationProperties(prefix = "root")
public class FileProperties {
private Path path;
private String url;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.bssm.attachit.domain.attachment.domain.Attachment;
import org.bssm.attachit.domain.attachment.exception.SocketIOException;
import org.bssm.attachit.domain.attachment.presentation.dto.response.AttachmentResponse;
import org.bssm.attachit.domain.attachment.service.GetAttachmentListService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -37,7 +37,7 @@ public void afterConnectionClosed(WebSocketSession session, CloseStatus status)

@Scheduled(fixedRate = 1000, initialDelay = 5000)
public void sendAttachmentListToAllClients() throws JsonProcessingException {
List<Attachment> attachmentList = getAttachmentListService.execute();
List<AttachmentResponse> attachmentList = getAttachmentListService.execute();
String jsonAttachments = objectMapper.writeValueAsString(attachmentList);
TextMessage message = new TextMessage(jsonAttachments);
CLIENTS.forEach((key, value) -> {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jwt:

root:
path: ${ROOT_PATH}
url: ${SERVER_URL}

server:
servlet:
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/bssm/attachit/AttachitApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootTest
class AttachitApplicationTests {

@Test
void contextLoads() {
}
// @Test
// void contextLoads() {
// }

}

0 comments on commit 1a3fcd7

Please sign in to comment.