-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/#33 Project API #37
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
27030c1
chore: λ©μλ νλΌλ―Έν° λ³κ²½
tidavid1 431d4e8
feat: κ³΅ν΅ μλ΅ κ·κ²© ꡬν
tidavid1 ab594cd
feat: Project Controller ꡬν
tidavid1 f3c96bf
Merge branch 'dev' into feat/#33
tidavid1 48165ff
test: Project Controller ν
μ€νΈ
tidavid1 b3df42f
chore: ControllerTestSupport μμ
tidavid1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
logbat/src/main/java/info/logbat/common/payload/ApiCommonResponse.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,48 @@ | ||
package info.logbat.common.payload; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ApiCommonResponse<T> { | ||
|
||
private int statusCode; | ||
private String message; | ||
private T data; | ||
|
||
private ApiCommonResponse(int status, String message, T data) { | ||
this.statusCode = status; | ||
this.message = message; | ||
this.data = data; | ||
} | ||
|
||
private ApiCommonResponse(int statusCode, String message) { | ||
this.statusCode = statusCode; | ||
this.message = message; | ||
} | ||
|
||
private ApiCommonResponse(int statusCode) { | ||
this.statusCode = statusCode; | ||
} | ||
|
||
public static <T> ApiCommonResponse<T> createApiResponse(HttpStatus httpStatus, String message, | ||
T data) { | ||
return new ApiCommonResponse<>(httpStatus.value(), message, data); | ||
} | ||
|
||
public static ApiCommonResponse<Void> createFailResponse(HttpStatus httpStatus, | ||
String message) { | ||
return new ApiCommonResponse<>(httpStatus.value(), message); | ||
} | ||
|
||
public static ApiCommonResponse<Void> createSuccessResponse() { | ||
return new ApiCommonResponse<>(HttpStatus.OK.value()); | ||
} | ||
|
||
public static <T> ApiCommonResponse<T> createSuccessResponse(T data) { | ||
return new ApiCommonResponse<>(HttpStatus.OK.value(), "Success", data); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
logbat/src/main/java/info/logbat/domain/project/presentation/ProjectController.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,53 @@ | ||
package info.logbat.domain.project.presentation; | ||
|
||
import info.logbat.common.payload.ApiCommonResponse; | ||
import info.logbat.domain.project.application.ProjectService; | ||
import info.logbat.domain.project.presentation.payload.request.ProjectCreateRequest; | ||
import info.logbat.domain.project.presentation.payload.request.ProjectUpdateRequest; | ||
import info.logbat.domain.project.presentation.payload.response.ProjectCommonResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/v1/projects") | ||
@RequiredArgsConstructor | ||
public class ProjectController { | ||
|
||
private final ProjectService projectService; | ||
|
||
@GetMapping("/{name}") | ||
public ApiCommonResponse<ProjectCommonResponse> get(@PathVariable String name) { | ||
return ApiCommonResponse.createSuccessResponse(projectService.getProjectByName(name)); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<ApiCommonResponse<ProjectCommonResponse>> create( | ||
@RequestBody ProjectCreateRequest request) { | ||
ApiCommonResponse<ProjectCommonResponse> response = ApiCommonResponse.createApiResponse( | ||
HttpStatus.CREATED, "Success", projectService.createProject(request.name())); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(response); | ||
} | ||
Comment on lines
+32
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ¨μν statusλ₯Ό CREATEDλ‘ μ€μ νλ μμ μ΄λΌλ©΄ @ResponseStatus(HttpStatus.CREATED) λ₯Ό μΆκ°νλ λ°©λ²μ μ΄λ¨κΉμ?!! |
||
|
||
@PutMapping("/{id}") | ||
public ApiCommonResponse<ProjectCommonResponse> update(@PathVariable Long id, | ||
@RequestBody ProjectUpdateRequest request) { | ||
return ApiCommonResponse.createSuccessResponse( | ||
projectService.updateProjectValues(id, request.name())); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ApiCommonResponse<Long> delete(@PathVariable Long id) { | ||
Long expectedId = projectService.deleteProject(id); | ||
return ApiCommonResponse.createSuccessResponse(expectedId); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...in/java/info/logbat/domain/project/presentation/payload/request/ProjectCreateRequest.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,5 @@ | ||
package info.logbat.domain.project.presentation.payload.request; | ||
|
||
public record ProjectCreateRequest(String name) { | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...in/java/info/logbat/domain/project/presentation/payload/request/ProjectUpdateRequest.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,5 +1,5 @@ | ||
package info.logbat.domain.project.presentation.payload.request; | ||
|
||
public record ProjectUpdateRequest(Long id, String name) { | ||
public record ProjectUpdateRequest(String name) { | ||
|
||
} |
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
141 changes: 141 additions & 0 deletions
141
logbat/src/test/java/info/logbat/domain/project/presentation/ProjectControllerTest.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,141 @@ | ||
package info.logbat.domain.project.presentation; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import info.logbat.domain.common.ControllerTestSupport; | ||
import info.logbat.domain.project.presentation.payload.response.ProjectCommonResponse; | ||
import java.time.LocalDateTime; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@DisplayName("ProjectControllerλ") | ||
class ProjectControllerTest extends ControllerTestSupport { | ||
|
||
private final Long expectedId = 1L; | ||
private final String expectedName = "projectName"; | ||
private final ProjectCommonResponse projectCommonResponse = mock(ProjectCommonResponse.class); | ||
|
||
@Nested | ||
@DisplayName("GET /v1/projects/{name}μ λν΄") | ||
class describeGet { | ||
|
||
@Test | ||
@DisplayName("νλ‘μ νΈ μ΄λ¦μΌλ‘ νλ‘μ νΈλ₯Ό μ‘°νν μ μλ€.") | ||
void willReturnProjectInformation() throws Exception { | ||
// Arrange | ||
LocalDateTime expectedCreatedAt = LocalDateTime.now(); | ||
given(projectService.getProjectByName("projectName")).willReturn(projectCommonResponse); | ||
given(projectCommonResponse.id()).willReturn(expectedId); | ||
given(projectCommonResponse.name()).willReturn(expectedName); | ||
given(projectCommonResponse.createdAt()).willReturn(expectedCreatedAt); | ||
// Act | ||
MockHttpServletRequestBuilder get = get("/v1/projects/{name}", "projectName"); | ||
// Assert | ||
mockMvc.perform(get) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.statusCode").value(200)) | ||
.andExpect(jsonPath("$.message").value("Success")) | ||
.andExpect(jsonPath("$.data.id").value(expectedId)) | ||
.andExpect(jsonPath("$.data.name").value(expectedName)) | ||
.andExpect(jsonPath("$.data.createdAt").value(expectedCreatedAt.toString())) | ||
.andExpect(jsonPath("$.data.updatedAt").doesNotExist()); | ||
} | ||
|
||
} | ||
|
||
@Nested | ||
@DisplayName("POST /v1/projectsμ λν΄") | ||
class describePost { | ||
|
||
@Test | ||
@DisplayName("νλ‘μ νΈλ₯Ό μμ±ν μ μλ€.") | ||
void willCreateProject() throws Exception { | ||
// Arrange | ||
LocalDateTime expectedCreatedAt = LocalDateTime.now(); | ||
given(projectService.createProject(expectedName)).willReturn(projectCommonResponse); | ||
given(projectCommonResponse.id()).willReturn(expectedId); | ||
given(projectCommonResponse.name()).willReturn(expectedName); | ||
given(projectCommonResponse.createdAt()).willReturn(expectedCreatedAt); | ||
// Act | ||
MockHttpServletRequestBuilder post = post("/v1/projects") | ||
.contentType("application/json") | ||
.content("{\"name\":\"" + expectedName + "\"}"); | ||
// Assert | ||
mockMvc.perform(post) | ||
.andExpect(status().isCreated()) | ||
.andExpect(jsonPath("$.statusCode").value(201)) | ||
.andExpect(jsonPath("$.message").value("Success")) | ||
.andExpect(jsonPath("$.data.id").value(expectedId)) | ||
.andExpect(jsonPath("$.data.name").value(expectedName)) | ||
.andExpect(jsonPath("$.data.createdAt").value(expectedCreatedAt.toString())) | ||
.andExpect(jsonPath("$.data.updatedAt").doesNotExist()); | ||
} | ||
|
||
} | ||
|
||
@Nested | ||
@DisplayName("PUT /v1/projects/{id}μ λν΄") | ||
class describePut { | ||
|
||
@Test | ||
@DisplayName("νλ‘μ νΈλ₯Ό μμ ν μ μλ€.") | ||
void willUpdateProject() throws Exception { | ||
// Arrange | ||
LocalDateTime expectedCreatedAt = LocalDateTime.now(); | ||
given(projectService.updateProjectValues(expectedId, expectedName)).willReturn( | ||
projectCommonResponse); | ||
given(projectCommonResponse.id()).willReturn(expectedId); | ||
given(projectCommonResponse.name()).willReturn(expectedName); | ||
given(projectCommonResponse.createdAt()).willReturn(expectedCreatedAt); | ||
// Act | ||
MockHttpServletRequestBuilder put = put("/v1/projects/{id}", expectedId) | ||
.contentType("application/json") | ||
.content("{\"name\":\"" + expectedName + "\"}"); | ||
// Assert | ||
mockMvc.perform(put) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.statusCode").value(200)) | ||
.andExpect(jsonPath("$.message").value("Success")) | ||
.andExpect(jsonPath("$.data.id").value(expectedId)) | ||
.andExpect(jsonPath("$.data.name").value(expectedName)) | ||
.andExpect(jsonPath("$.data.createdAt").value(expectedCreatedAt.toString())) | ||
.andExpect(jsonPath("$.data.updatedAt").doesNotExist()); | ||
} | ||
|
||
} | ||
|
||
@Nested | ||
@DisplayName("DELETE /v1/projects/{id}μ λν΄") | ||
class describeDelete { | ||
|
||
@Test | ||
@DisplayName("νλ‘μ νΈλ₯Ό μμ ν μ μλ€.") | ||
void willDeleteProject() throws Exception { | ||
// Arrange | ||
given(projectService.deleteProject(any(Long.class))).willReturn(expectedId); | ||
// Act | ||
MockHttpServletRequestBuilder delete = delete("/v1/projects/{id}", expectedId); | ||
// Assert | ||
mockMvc.perform(delete) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.statusCode").value(200)) | ||
.andExpect(jsonPath("$.message").value("Success")) | ||
.andExpect(jsonPath("$.data").value(expectedId)); | ||
} | ||
|
||
} | ||
|
||
} | ||
miiiinju1 marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ³΅ν΅ μλ¬ νΈλ€λ¬λ λμ€μ λ§λ€μ΄μ