post command & query
This commit is contained in:
@@ -23,6 +23,7 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-security'
|
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
|
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.example.oneul.domain.post.api;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
|
import com.example.oneul.domain.post.dto.PostDTO;
|
||||||
|
import com.example.oneul.domain.post.service.command.PostCommandService;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value = "/post")
|
||||||
|
public class PostCommandApi {
|
||||||
|
private final PostCommandService postCommandService;
|
||||||
|
|
||||||
|
public PostCommandApi(PostCommandService postCommandService){
|
||||||
|
this.postCommandService = postCommandService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value="/", method=RequestMethod.POST)
|
||||||
|
public Post createPost(HttpSession httpSession, @RequestBody PostDTO postDTO) {
|
||||||
|
Post post = postCommandService.createPost(postDTO.toEntity(), httpSession);
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value="/{postId}/", method=RequestMethod.PUT)
|
||||||
|
public Post updatePost(HttpSession httpSession, @RequestBody PostDTO postDTO, @PathVariable Long postId) {
|
||||||
|
Post post = postCommandService.updatePost(postId, postDTO.toEntity(), httpSession);
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.example.oneul.domain.post.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
|
import com.example.oneul.domain.post.service.query.PostQueryService;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value = "/post")
|
||||||
|
public class PostQueryApi {
|
||||||
|
private final PostQueryService postQueryService;
|
||||||
|
|
||||||
|
public PostQueryApi(PostQueryService postQueryService){
|
||||||
|
this.postQueryService = postQueryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value="", method=RequestMethod.GET)
|
||||||
|
public Page<Post> findAll(@RequestParam("page") Integer page) {
|
||||||
|
Page<Post> posts = postQueryService.findAll(PageRequest.of(page, 10));
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value="/writer/{writerId}", method=RequestMethod.GET)
|
||||||
|
public Page<Post> findAllByWriter(@PathVariable Long writerId, @RequestParam("page") Integer page) {
|
||||||
|
Page<Post> posts = postQueryService.findByWriter(writerId, PageRequest.of(page, 10));
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(value="/test")
|
||||||
|
public List<Post> test() {
|
||||||
|
return postQueryService.test();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import org.springframework.data.repository.CrudRepository;
|
|||||||
public interface PostCommandRepository extends CrudRepository<Post, Long> {
|
public interface PostCommandRepository extends CrudRepository<Post, Long> {
|
||||||
Post save(Post post);
|
Post save(Post post);
|
||||||
Optional<Post> findByIdAndWriter(Long id, UserEntity writer);
|
Optional<Post> findByIdAndWriter(Long id, UserEntity writer);
|
||||||
|
void deleteByIdAndWriter(Long id, UserEntity writer);
|
||||||
void deleteById(Long id);
|
void deleteById(Long id);
|
||||||
void delete(Post post);
|
void delete(Post post);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.example.oneul.domain.post.dao;
|
package com.example.oneul.domain.post.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.example.oneul.domain.post.domain.Post;
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
@@ -12,5 +13,7 @@ import org.springframework.stereotype.Repository;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface PostQueryRepository extends JpaRepository<Post, Long>{
|
public interface PostQueryRepository extends JpaRepository<Post, Long>{
|
||||||
Optional<Post> findById(Long id);
|
Optional<Post> findById(Long id);
|
||||||
|
List<Post> findAll();
|
||||||
Page<Post> findAll(Pageable pageable);
|
Page<Post> findAll(Pageable pageable);
|
||||||
|
Page<Post> findAllByWriter_Id(Long writerId, Pageable pageable);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.example.oneul.domain.post.domain;
|
package com.example.oneul.domain.post.domain;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
|||||||
@Entity
|
@Entity
|
||||||
@EntityListeners(AuditingEntityListener.class)
|
@EntityListeners(AuditingEntityListener.class)
|
||||||
@Table(indexes = @Index(name = "i_post", columnList="createdAt"))
|
@Table(indexes = @Index(name = "i_post", columnList="createdAt"))
|
||||||
public class Post {
|
public class Post implements Serializable {
|
||||||
@Id @GeneratedValue(strategy = GenerationType.AUTO)
|
@Id @GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private Long id;
|
private Long id;
|
||||||
@CreatedDate
|
@CreatedDate
|
||||||
@@ -32,7 +33,7 @@ public class Post {
|
|||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String content;
|
private String content;
|
||||||
@Access(AccessType.PROPERTY)
|
@Access(AccessType.PROPERTY)
|
||||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
@ManyToOne(fetch = FetchType.EAGER, optional = false)
|
||||||
private UserEntity writer;
|
private UserEntity writer;
|
||||||
|
|
||||||
public Post() {}
|
public Post() {}
|
||||||
|
|||||||
30
src/main/java/com/example/oneul/domain/post/dto/PostDTO.java
Normal file
30
src/main/java/com/example/oneul/domain/post/dto/PostDTO.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.example.oneul.domain.post.dto;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
|
|
||||||
|
public class PostDTO {
|
||||||
|
@NotBlank
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
public PostDTO() {}
|
||||||
|
|
||||||
|
public PostDTO(String content){
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content){
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent(){
|
||||||
|
return this.content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post toEntity() {
|
||||||
|
return Post.builder()
|
||||||
|
.content(content)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,12 +33,12 @@ public class PostCommnadServiceImpl implements PostCommandService{
|
|||||||
.writer(userEntity)
|
.writer(userEntity)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
log.info("user: " + userEntity.toString() + " create " + post.toString());
|
log.info("user: " + userEntity.toString() + " create " + postEntity.toString());
|
||||||
return postEntity;
|
return postEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Post updatePost(Long id, Post post, HttpSession httpSession){
|
public Post updatePost(Long id, Post post, HttpSession httpSession){
|
||||||
UserEntity userEntity = (UserEntity) httpSession.getAttribute("user");
|
UserEntity userEntity = (UserEntity) httpSession.getAttribute("user");
|
||||||
// TODO: 적절한 방법인지 확인하기
|
// TODO: 적절한 방법인지 확인하기
|
||||||
Post postEntity = postCommandRepository.findByIdAndWriter(id, userEntity).orElseThrow(() -> new NotFoundException(id + " post not found"));
|
Post postEntity = postCommandRepository.findByIdAndWriter(id, userEntity).orElseThrow(() -> new NotFoundException(id + " post not found"));
|
||||||
@@ -51,7 +51,9 @@ public class PostCommnadServiceImpl implements PostCommandService{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deletePost(Long id, HttpSession httpSession){
|
public void deletePost(Long id, HttpSession httpSession){
|
||||||
postCommandRepository.deleteById(id);
|
// TODO: 이 때 세션이 만기되면 어떡함
|
||||||
|
UserEntity userEntity = (UserEntity)httpSession.getAttribute("user");
|
||||||
|
postCommandRepository.deleteByIdAndWriter(id, userEntity);
|
||||||
log.info("post " + id + " is deleted");
|
log.info("post " + id + " is deleted");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
package com.example.oneul.domain.post.service.query;
|
package com.example.oneul.domain.post.service.query;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.example.oneul.domain.post.domain.Post;
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
|
||||||
public interface PostQueryService {
|
public interface PostQueryService {
|
||||||
|
List<Post> test();
|
||||||
Page<Post> findAll(PageRequest pageRequest);
|
Page<Post> findAll(PageRequest pageRequest);
|
||||||
|
Page<Post> findByWriter(Long writerId, PageRequest pageRequest);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.example.oneul.domain.post.service.query;
|
package com.example.oneul.domain.post.service.query;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.example.oneul.domain.post.dao.PostQueryRepository;
|
import com.example.oneul.domain.post.dao.PostQueryRepository;
|
||||||
import com.example.oneul.domain.post.domain.Post;
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
|
|
||||||
@@ -21,4 +23,14 @@ public class PostQueryServiceImpl implements PostQueryService {
|
|||||||
public Page<Post> findAll(PageRequest pageRequest){
|
public Page<Post> findAll(PageRequest pageRequest){
|
||||||
return postQueryRepository.findAll(pageRequest);
|
return postQueryRepository.findAll(pageRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<Post> findByWriter(Long writerId, PageRequest pageRequest){
|
||||||
|
return postQueryRepository.findAllByWriter_Id(writerId, pageRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Post> test(){
|
||||||
|
return postQueryRepository.findAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import javax.persistence.Id;
|
|||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
|
|
||||||
import com.example.oneul.domain.post.domain.Post;
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
|
||||||
import org.springframework.data.annotation.CreatedDate;
|
import org.springframework.data.annotation.CreatedDate;
|
||||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||||
@@ -25,9 +26,9 @@ public class UserEntity implements Serializable {
|
|||||||
private Long id;
|
private Long id;
|
||||||
@Column(nullable = false, unique = true)
|
@Column(nullable = false, unique = true)
|
||||||
private String username;
|
private String username;
|
||||||
@Column(nullable = false)
|
@JsonIgnore @Column(nullable = false)
|
||||||
private String password;
|
private String password;
|
||||||
@CreatedDate
|
@JsonIgnore @CreatedDate
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
@OneToMany(orphanRemoval = true, cascade = CascadeType.REMOVE)
|
@OneToMany(orphanRemoval = true, cascade = CascadeType.REMOVE)
|
||||||
private Set<Post> posts;
|
private Set<Post> posts;
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ public class UserServiceImpl implements UserService {
|
|||||||
.password(
|
.password(
|
||||||
passwordEncoder.encode(userEntity.getPassword()))
|
passwordEncoder.encode(userEntity.getPassword()))
|
||||||
.build();
|
.build();
|
||||||
|
userRepository.save(user);
|
||||||
|
log.info("user is created: " + user.toString());
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,14 +47,16 @@ public class UserServiceImpl implements UserService {
|
|||||||
UserEntity user = userRepository.findByUsername(userEntity.getUsername())
|
UserEntity user = userRepository.findByUsername(userEntity.getUsername())
|
||||||
.orElseThrow(() -> new WrongUsernameAndPasswordException("wrong username"));
|
.orElseThrow(() -> new WrongUsernameAndPasswordException("wrong username"));
|
||||||
|
|
||||||
if(passwordEncoder.matches(userEntity.getPassword(), user.getPassword())){
|
if(!passwordEncoder.matches(userEntity.getPassword(), user.getPassword())){
|
||||||
log.info("login user: " + userEntity.toString());
|
|
||||||
httpSession.setAttribute("user",user);
|
|
||||||
log.info("session id: " + httpSession.getId());
|
|
||||||
log.info("session value: " + httpSession.getAttribute("user"));
|
|
||||||
} else {
|
|
||||||
throw new WrongUsernameAndPasswordException("wrong passowrd");
|
throw new WrongUsernameAndPasswordException("wrong passowrd");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.info("login user: " + userEntity.toString());
|
||||||
|
httpSession.setAttribute("user",user);
|
||||||
|
|
||||||
|
log.info("session id: " + httpSession.getId());
|
||||||
|
log.info("session value: " + httpSession.getAttribute("user"));
|
||||||
|
log.info("get session: " + httpSession.getAttribute("user").toString());
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler(WrongUsernameAndPasswordException.class)
|
@ExceptionHandler(WrongUsernameAndPasswordException.class)
|
||||||
protected ResponseEntity<String> handleWrongUsernameAndPasswordException(WrongUsernameAndPasswordException e){
|
protected ResponseEntity<String> handleWrongUsernameAndPasswordException(WrongUsernameAndPasswordException e){
|
||||||
log.info("wrong username and password");
|
log.info(e.getMessage());
|
||||||
return new ResponseEntity<>("wrong username and password", HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.example.oneul.service;
|
||||||
|
|
||||||
|
public class PostCommandSerivceTest {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -19,7 +19,7 @@ import org.testcontainers.junit.jupiter.Testcontainers;
|
|||||||
@Testcontainers
|
@Testcontainers
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
public class UserCommandServiceTest {
|
public class UserServiceTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userCommandService;
|
private UserService userCommandService;
|
||||||
@Autowired
|
@Autowired
|
||||||
Reference in New Issue
Block a user