BE Study 9th
@Controller
public class CreatePostController {
@Controller and CreatePostController Definition
private static final Logger log = LoggerFactory.getLogger(CreatePostController.class);
Logger for logging
private static final String MODEL_ATTRIBUTE_POST = "post";
private final CreatePostHandler createPostHandler;
CreatePostHandler may be a class that does some kind of operation for the controller(delegation pattern?)
public CreatePostController(final CreatePostHandler createPostHandler) {
this.createPostHandler = createPostHandler;
}
consturctor. Gives createPostHandler.
@GetMapping("/posts/new")
@AnyAuthenticatedUser
public String newPostForm(Model model) {
model.addAttribute(MODEL_ATTRIBUTE_POST, new CreatePostRequest("", "", "", null, null));
return "posts/add-post";
}
adds attribute((MODEL_ATTRIBUTE_POST, new CreatePostRequest("", "", "", null, null)) to the model.
@PostMapping("/posts")
@AnyAuthenticatedUser
public String createPost(
@Valid @ModelAttribute(MODEL_ATTRIBUTE_POST) CreatePostRequest request,
BindingResult bindingResult,
@CurrentUser User loginUser,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
return "posts/add-post";
}
var createPostRequest = new CreatePostRequest(
request.title(), request.url(), request.content(), request.categoryId(), loginUser.getId());
PostDTO post = createPostHandler.createPost(createPostRequest);
log.info("Post saved successfully with id: {}", post.id());
redirectAttributes.addFlashAttribute("message", "Post saved successfully");
return "redirect:/c/" + post.category().slug();
}
}
checks if bindingResult has errors, and makes a createPostRequest with the given request, and calls createPostHandler.
public PostDTO createPost(CreatePostRequest createPostRequest) {
String title = createPostRequest.title();
log.info("process=create_post, title={}", title);
Category category = categoryRepository.getReferenceById(createPostRequest.categoryId());
User user = userRepository.getReferenceById(createPostRequest.createdUserId());
Post post = new Post(
null,
title,
createPostRequest.url(),
createPostRequest.content(),
category,
user,
Set.of(),
LocalDateTime.now(),
null);
return postDTOMapper.toDTO(postRepository.save(post));
}
}
To briefly explain it, it creates a 'post' using the request, and calls postRepository.save(post) which saves the post to the DB, and then returns postDTO. Then why we use the postDTO? Why don't we just use the postRepository.save(post)?
1. you might have the need to hide some data to the client. Entity itself does have the whole data of the record in the DB. For example, 'Member' entity would have all data of the member like ID, password, DateOfSigningIn, and all the other privacy. Thus, you cannot return the whole thing to your client.
2. Eliminating the dependency. Guess there is a modification in the DB's schema. If you just use the entity in all the other layers, you should modify all of them if the schema changes. However, if you use DTO, you can just modify only the DTOMapper method. Just like an adapter.
3. You can modify the data format or combine fields as you want.
댓글
댓글 쓰기