-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from 42organization/feat/likeService-7
[Feat] 좋아요 기능 추가 #7
- Loading branch information
Showing
9 changed files
with
211 additions
and
18 deletions.
There are no files selected for viewing
22 changes: 21 additions & 1 deletion
22
src/main/java/com/example/demo/PersistenceDateConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
package com.example.demo;public class PersistenceDateConverter { | ||
package com.example.demo; | ||
|
||
import jakarta.persistence.AttributeConverter; | ||
import jakarta.persistence.Converter; | ||
|
||
import java.sql.Date; | ||
import java.time.*; | ||
|
||
|
||
//@Converter(autoApply = true) | ||
public class PersistenceDateConverter implements AttributeConverter<LocalDateTime, Date> { | ||
|
||
@Override | ||
public Date convertToDatabaseColumn(LocalDateTime attribute) { | ||
return Date.valueOf(attribute.toLocalDate()); | ||
} | ||
|
||
@Override | ||
public LocalDateTime convertToEntityAttribute(Date dbData) { | ||
return dbData.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/example/demo/dto/AllPostResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.demo.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
public class AllPostResponseDto { | ||
private List<PostResponseDto> dataList; | ||
private Long postCnt; | ||
|
||
public AllPostResponseDto (List<PostResponseDto> dataList) { | ||
this.dataList = dataList; | ||
this.postCnt = Long.valueOf(dataList.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ | |
public class PostModifyRequestDto { | ||
private Long postId; | ||
private String info; | ||
private String tag; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package com.example.demo.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@NoArgsConstructor | ||
@Setter | ||
@Getter | ||
@AllArgsConstructor | ||
public class PostSaveRequestDto { | ||
private String writer; | ||
private String info; | ||
private String tag; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/test/java/com/example/demo/api/PostControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.example.demo.api; | ||
|
||
import com.example.demo.domain.Post; | ||
import com.example.demo.dto.PostResponseDto; | ||
import com.example.demo.dto.PostSaveRequestDto; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import jakarta.persistence.EntityTransaction; | ||
import jakarta.persistence.PersistenceUnit; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@Transactional | ||
public class PostControllerTest { | ||
|
||
@LocalServerPort | ||
private int port; | ||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
@PersistenceUnit | ||
EntityManagerFactory emf; | ||
EntityManager em; | ||
|
||
@Test | ||
public void savePostTest() { | ||
PostSaveRequestDto postRequest = new PostSaveRequestDto("hyunkyu", "info", "tag"); | ||
|
||
String url = "http://localhost:" + port + "/post"; | ||
ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, postRequest, Long.class); | ||
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void getPostTest(){ | ||
em = emf.createEntityManager(); | ||
EntityTransaction entityTransaction = em.getTransaction(); | ||
Post post = Post.createPost("content", "hyunkyu", "tag"); | ||
em.persist(post); | ||
|
||
String url = "http://localhost:" + port + "/post?postId=" + post.getId(); | ||
ResponseEntity<PostResponseDto> responseEntity = restTemplate.getForEntity(url, PostResponseDto.class); | ||
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
assertThat(responseEntity.getBody().getInfo()).isEqualTo("content"); | ||
} | ||
} |