post command & query

This commit is contained in:
jinho jeong
2022-05-30 20:09:15 +09:00
parent efea36e1fc
commit da318671b6
15 changed files with 164 additions and 16 deletions

View File

@@ -23,6 +23,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
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-data-jpa'

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -10,6 +10,7 @@ import org.springframework.data.repository.CrudRepository;
public interface PostCommandRepository extends CrudRepository<Post, Long> {
Post save(Post post);
Optional<Post> findByIdAndWriter(Long id, UserEntity writer);
void deleteByIdAndWriter(Long id, UserEntity writer);
void deleteById(Long id);
void delete(Post post);
}

View File

@@ -1,5 +1,6 @@
package com.example.oneul.domain.post.dao;
import java.util.List;
import java.util.Optional;
import com.example.oneul.domain.post.domain.Post;
@@ -12,5 +13,7 @@ import org.springframework.stereotype.Repository;
@Repository
public interface PostQueryRepository extends JpaRepository<Post, Long>{
Optional<Post> findById(Long id);
List<Post> findAll();
Page<Post> findAll(Pageable pageable);
Page<Post> findAllByWriter_Id(Long writerId, Pageable pageable);
}

View File

@@ -1,5 +1,6 @@
package com.example.oneul.domain.post.domain;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;
@@ -24,7 +25,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(indexes = @Index(name = "i_post", columnList="createdAt"))
public class Post {
public class Post implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@CreatedDate
@@ -32,7 +33,7 @@ public class Post {
@Column(nullable = false)
private String content;
@Access(AccessType.PROPERTY)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@ManyToOne(fetch = FetchType.EAGER, optional = false)
private UserEntity writer;
public Post() {}

View 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();
}
}

View File

@@ -33,12 +33,12 @@ public class PostCommnadServiceImpl implements PostCommandService{
.writer(userEntity)
.build());
log.info("user: " + userEntity.toString() + " create " + post.toString());
log.info("user: " + userEntity.toString() + " create " + postEntity.toString());
return postEntity;
}
@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");
// TODO: 적절한 방법인지 확인하기
Post postEntity = postCommandRepository.findByIdAndWriter(id, userEntity).orElseThrow(() -> new NotFoundException(id + " post not found"));
@@ -51,7 +51,9 @@ public class PostCommnadServiceImpl implements PostCommandService{
@Override
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");
}
}

View File

@@ -1,10 +1,14 @@
package com.example.oneul.domain.post.service.query;
import java.util.List;
import com.example.oneul.domain.post.domain.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
public interface PostQueryService {
List<Post> test();
Page<Post> findAll(PageRequest pageRequest);
Page<Post> findByWriter(Long writerId, PageRequest pageRequest);
}

View File

@@ -1,5 +1,7 @@
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.domain.Post;
@@ -21,4 +23,14 @@ public class PostQueryServiceImpl implements PostQueryService {
public Page<Post> findAll(PageRequest 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();
}
}

View File

@@ -14,6 +14,7 @@ import javax.persistence.Id;
import javax.persistence.OneToMany;
import com.example.oneul.domain.post.domain.Post;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@@ -25,9 +26,9 @@ public class UserEntity implements Serializable {
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
@JsonIgnore @Column(nullable = false)
private String password;
@CreatedDate
@JsonIgnore @CreatedDate
private LocalDateTime createdAt;
@OneToMany(orphanRemoval = true, cascade = CascadeType.REMOVE)
private Set<Post> posts;

View File

@@ -37,6 +37,8 @@ public class UserServiceImpl implements UserService {
.password(
passwordEncoder.encode(userEntity.getPassword()))
.build();
userRepository.save(user);
log.info("user is created: " + user.toString());
return user;
}
@@ -45,14 +47,16 @@ public class UserServiceImpl implements UserService {
UserEntity user = userRepository.findByUsername(userEntity.getUsername())
.orElseThrow(() -> new WrongUsernameAndPasswordException("wrong username"));
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 {
if(!passwordEncoder.matches(userEntity.getPassword(), user.getPassword())){
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;
}

View File

@@ -22,7 +22,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(WrongUsernameAndPasswordException.class)
protected ResponseEntity<String> handleWrongUsernameAndPasswordException(WrongUsernameAndPasswordException e){
log.info("wrong username and password");
return new ResponseEntity<>("wrong username and password", HttpStatus.NOT_FOUND);
log.info(e.getMessage());
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
}

View File

@@ -0,0 +1,5 @@
package com.example.oneul.service;
public class PostCommandSerivceTest {
}

View File

@@ -19,7 +19,7 @@ import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
@SpringBootTest
@ActiveProfiles("test")
public class UserCommandServiceTest {
public class UserServiceTest {
@Autowired
private UserService userCommandService;
@Autowired