-
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 #17 from Nexters/feature/theme
[FEAT] Theme ν μ€νΈ μ½λ μμ± λ° API ꡬν (#2)
- Loading branch information
Showing
14 changed files
with
443 additions
and
17 deletions.
There are no files selected for viewing
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
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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/nextroom/oescape/controller/ThemeController.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,52 @@ | ||
package com.nextroom.oescape.controller; | ||
|
||
import static com.nextroom.oescape.exceptions.StatusCode.*; | ||
|
||
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.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; | ||
|
||
import com.nextroom.oescape.dto.BaseResponse; | ||
import com.nextroom.oescape.dto.DataResponse; | ||
import com.nextroom.oescape.dto.ThemeDto; | ||
import com.nextroom.oescape.service.ThemeService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/theme") | ||
@RequiredArgsConstructor | ||
public class ThemeController { | ||
private final ThemeService themeService; | ||
|
||
@PostMapping | ||
public ResponseEntity<BaseResponse> addTheme( | ||
@RequestBody ThemeDto.AddThemeRequest request) { | ||
themeService.addTheme(request); | ||
return ResponseEntity.ok(new BaseResponse(OK)); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<BaseResponse> getThemeList() { | ||
return ResponseEntity.ok(new DataResponse<>(OK, themeService.getThemeList())); | ||
} | ||
|
||
@PutMapping | ||
public ResponseEntity<BaseResponse> editTheme( | ||
@RequestBody ThemeDto.EditThemeRequest request) { | ||
themeService.editTheme(request); | ||
return ResponseEntity.ok(new BaseResponse(OK)); | ||
} | ||
|
||
@DeleteMapping | ||
public ResponseEntity<BaseResponse> removeTheme( | ||
@RequestBody ThemeDto.RemoveRequest request) { | ||
themeService.removeTheme(request); | ||
return ResponseEntity.ok(new BaseResponse(OK)); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.nextroom.oescape.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
public class ThemeDto { | ||
|
||
@Getter | ||
@Builder | ||
@RequiredArgsConstructor | ||
@NoArgsConstructor(force = true) | ||
public static class AddThemeRequest { | ||
private final String title; | ||
private final Integer timeLimit; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
public static class AddThemeResponse { | ||
private final Long id; | ||
private final String title; | ||
private final Integer timeLimit; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
public static class ThemeListResponse { | ||
private final Long id; | ||
private final String title; | ||
private final Integer timeLimit; | ||
} | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public static class EditThemeRequest { | ||
private final Long id; | ||
private final String title; | ||
private final Integer timeLimit; | ||
} | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public static class RemoveRequest { | ||
private final Long id; | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/nextroom/oescape/repository/ThemeRepository.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,15 @@ | ||
package com.nextroom.oescape.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.nextroom.oescape.domain.Shop; | ||
import com.nextroom.oescape.domain.Theme; | ||
|
||
public interface ThemeRepository extends JpaRepository<Theme, Long> { | ||
Optional<Theme> findByTitle(String title); | ||
|
||
List<Theme> findAllByShop(Shop shop); | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/nextroom/oescape/service/ThemeService.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,75 @@ | ||
package com.nextroom.oescape.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.nextroom.oescape.domain.Shop; | ||
import com.nextroom.oescape.domain.Theme; | ||
import com.nextroom.oescape.dto.ThemeDto; | ||
import com.nextroom.oescape.exceptions.CustomException; | ||
import com.nextroom.oescape.exceptions.StatusCode; | ||
import com.nextroom.oescape.repository.ThemeRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ThemeService { | ||
private final ThemeRepository themeRepository; | ||
|
||
public ThemeDto.AddThemeResponse addTheme(ThemeDto.AddThemeRequest request) { | ||
Theme result = themeRepository.findByTitle(request.getTitle()).orElseThrow( | ||
() -> new CustomException(StatusCode.BAD_REQUEST) | ||
); | ||
|
||
Theme theme = Theme.builder() | ||
.title(request.getTitle()) | ||
.timeLimit(request.getTimeLimit()) | ||
.build(); | ||
|
||
Theme savedTheme = themeRepository.save(theme); | ||
|
||
return ThemeDto.AddThemeResponse.builder() | ||
.id(savedTheme.getId()) | ||
.title(savedTheme.getTitle()) | ||
.timeLimit(savedTheme.getTimeLimit()) | ||
.build(); | ||
} | ||
|
||
public List<ThemeDto.ThemeListResponse> getThemeList() { | ||
//TODO νμ κ²μ¦ λ‘μ§ | ||
|
||
List<Theme> themeList = themeRepository.findAllByShop(new Shop()); | ||
List<ThemeDto.ThemeListResponse> themeListResponses = new ArrayList<>(); | ||
for (Theme theme : themeList) { | ||
themeListResponses.add(ThemeDto.ThemeListResponse | ||
.builder() | ||
.id(theme.getId()) | ||
.title(theme.getTitle()) | ||
.timeLimit(theme.getTimeLimit()) | ||
.build()); | ||
} | ||
return themeListResponses; | ||
} | ||
|
||
public void editTheme(ThemeDto.EditThemeRequest request) { | ||
//TODO ν λ§ μ¬μ©μ μ‘°ν | ||
|
||
//TODO ν λ§ μ‘°ν | ||
Theme theme = themeRepository.findById(request.getId()).orElseThrow( | ||
() -> new CustomException(StatusCode.BAD_REQUEST) | ||
); | ||
|
||
//TODO ν λ§ μμ | ||
} | ||
|
||
public void removeTheme(ThemeDto.RemoveRequest request) { | ||
//TODO ν λ§ μ¬μ©μ μ‘°ν | ||
|
||
//TODO ν λ§ μ‘°ν | ||
|
||
//TODO ν λ§ μμ | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/java/com/nextroom/oescape/controller/ThemeControllerTest.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,58 @@ | ||
package com.nextroom.oescape.controller; | ||
|
||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import com.google.gson.Gson; | ||
import com.nextroom.oescape.dto.ThemeDto; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ThemeControllerTest { | ||
|
||
@InjectMocks | ||
private ThemeController themeController; | ||
|
||
private MockMvc mockMvc; | ||
private Gson gson; | ||
|
||
@BeforeEach | ||
void init() { | ||
gson = new Gson(); | ||
mockMvc = MockMvcBuilders.standaloneSetup(themeController).build(); | ||
} | ||
|
||
@Test | ||
@DisplayName("ν λ§ λ±λ‘ μ€ν¨_μ¬μ©μ μλ³κ° ν€λμ μμ") | ||
void failAddTheme() throws Exception { | ||
//given | ||
String url = "/api/v1/theme"; | ||
|
||
//when | ||
ResultActions resultActions = mockMvc.perform( | ||
MockMvcRequestBuilders.post(url) | ||
.content(gson.toJson(addThemeRequest("ν λ§ μ΄λ¦", 70))) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
); | ||
|
||
//then | ||
resultActions.andExpect(status().isBadRequest()); | ||
} | ||
|
||
private ThemeDto.AddThemeRequest addThemeRequest(String title, int timeLimit) { | ||
return ThemeDto.AddThemeRequest.builder() | ||
.title(title) | ||
.timeLimit(timeLimit) | ||
.build(); | ||
} | ||
} |
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,16 @@ | ||
package com.nextroom.oescape.domain; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ThemeTest { | ||
|
||
@Test | ||
@DisplayName("ν λ§ μμ±") | ||
void createTheme() { | ||
Theme theme = new Theme(); | ||
assertNotNull(theme); | ||
} | ||
} |
Oops, something went wrong.