Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
26 changes: 26 additions & 0 deletions restrequest/json-request.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GET http://localhost:8080/api/json/v1/vouchers
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

이렇게 다양한 방식으로 테스트해보는것도 좋다고 생각합니다


###

GET http://localhost:8080/api/json/v1/vouchers/search?startDate=2023-07-16T00:00:00&endDate=2023-07-19T23:59:59&
discountType=fixed

###

POST http://localhost:8080/api/json/v1/vouchers
Content-Type: application/json

{
"discountType": "fixed",
"discountStringValue": "123"
}

###

DELETE http://localhost:8080/api/json/v1/vouchers/f3ac7182-4f80-470a-99e7-478c29c41b0f

###

GET http://localhost:8080/api/json/v1/vouchers/cc295caf-cdb1-4fe6-8360-a2149a74f1b9

###
26 changes: 26 additions & 0 deletions restrequest/xml-request.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GET http://localhost:8080/api/xml/v1/vouchers

###

GET http://localhost:8080/api/xml/v1/vouchers/search?startDate=2023-07-16T00:00:00&endDate=2023-07-19T23:59:59&
discountType=fixed

###

POST http://localhost:8080/api/xml/v1/vouchers
Content-Type: application/xml

<VoucherRequest>
<discountType>fixed</discountType>
<discountStringValue>123</discountStringValue>
</VoucherRequest>

###

DELETE http://localhost:8080/api/xml/v1/vouchers/ea50d33d-d668-4dcd-b2b2-980b184ff003

###

GET http://localhost:8080/api/xml/v1/vouchers/fef87c80-ee1c-4aca-bf14-375930aff54c

###
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.prgmrs.voucher.view;
package com.prgmrs.voucher;

import com.prgmrs.voucher.controller.BlacklistController;
import com.prgmrs.voucher.controller.UserController;
import com.prgmrs.voucher.controller.VoucherController;
import com.prgmrs.voucher.controller.WalletController;
import com.prgmrs.voucher.dto.ResponseDTO;
import com.prgmrs.voucher.controller.console.BlacklistController;
import com.prgmrs.voucher.controller.console.UserController;
import com.prgmrs.voucher.controller.console.VoucherController;
import com.prgmrs.voucher.controller.console.WalletController;
import com.prgmrs.voucher.controller.console.wrapper.ResponseDTO;
import com.prgmrs.voucher.exception.WrongRangeFormatException;
import com.prgmrs.voucher.setting.BlacklistProperties;
import com.prgmrs.voucher.view.RequestBodyCreator;
import com.prgmrs.voucher.view.io.ConsoleReader;
import com.prgmrs.voucher.view.io.ConsoleWriter;
import com.prgmrs.voucher.view.io.ManagementType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prgmrs.voucher.controller;
package com.prgmrs.voucher.controller.console;

import com.prgmrs.voucher.dto.ResponseDTO;
import com.prgmrs.voucher.controller.console.wrapper.ResponseDTO;
import com.prgmrs.voucher.enums.StatusCode;
import com.prgmrs.voucher.exception.WrongRangeFormatException;
import com.prgmrs.voucher.service.BlacklistService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prgmrs.voucher.controller;
package com.prgmrs.voucher.controller.console;

import com.prgmrs.voucher.dto.ResponseDTO;
import com.prgmrs.voucher.controller.console.wrapper.ResponseDTO;
import com.prgmrs.voucher.dto.request.UserRequest;
import com.prgmrs.voucher.dto.request.VoucherIdRequest;
import com.prgmrs.voucher.enums.StatusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prgmrs.voucher.controller;
package com.prgmrs.voucher.controller.console;

import com.prgmrs.voucher.dto.ResponseDTO;
import com.prgmrs.voucher.controller.console.wrapper.ResponseDTO;
import com.prgmrs.voucher.dto.request.UsernameRequest;
import com.prgmrs.voucher.dto.request.VoucherRequest;
import com.prgmrs.voucher.enums.StatusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prgmrs.voucher.controller;
package com.prgmrs.voucher.controller.console;

import com.prgmrs.voucher.dto.ResponseDTO;
import com.prgmrs.voucher.controller.console.wrapper.ResponseDTO;
import com.prgmrs.voucher.dto.request.AssignVoucherRequest;
import com.prgmrs.voucher.dto.request.RemoveVoucherRequest;
import com.prgmrs.voucher.enums.StatusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.prgmrs.voucher.dto;
package com.prgmrs.voucher.controller.console.wrapper;

import com.prgmrs.voucher.enums.StatusCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.prgmrs.voucher.controller.json;

import com.prgmrs.voucher.dto.request.VoucherIdRequest;
import com.prgmrs.voucher.dto.request.VoucherRequest;
import com.prgmrs.voucher.dto.request.VoucherSearchRequest;
import com.prgmrs.voucher.dto.response.RemoveResponse;
import com.prgmrs.voucher.dto.response.VoucherListResponse;
import com.prgmrs.voucher.dto.response.VoucherResponse;
import com.prgmrs.voucher.service.VoucherService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/json/v1")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rest api 아닌가요?
rest의 url 규칙에 대해 알아보세요

헤더와 mapping 어노테이션을 이용할수 있습니다

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 내용에 대해서 공부를 해보았는데 URL에는 어떠한 데이터 형식으로 주고받는지에 대한 내용이 있으면 안되는 것을 배웠습니다. 대신 mapping의 옵션에서 어떤 데이터를 받아들일지 명확히 나타내도록 하겠습니다.

public class VoucherJsonController {

private final VoucherService voucherService;

public VoucherJsonController(VoucherService voucherService) {
this.voucherService = voucherService;
}

@GetMapping("/vouchers")
public ResponseEntity<VoucherListResponse> findAll() {
return ResponseEntity.ok(voucherService.findAll());
}

@GetMapping("/vouchers/search")
public ResponseEntity<VoucherListResponse> findByCreationTimeAndDiscountType(VoucherSearchRequest voucherSearchRequest) {
return ResponseEntity.ok(voucherService.findByCreationTimeAndDiscountType(voucherSearchRequest));
}

@PostMapping("/vouchers")
public ResponseEntity<VoucherResponse> createVoucher(@RequestBody VoucherRequest voucherRequest) {
return ResponseEntity.ok(voucherService.createVoucher(voucherRequest));
}

@DeleteMapping("/vouchers/{voucherId}")
public ResponseEntity<RemoveResponse> removeVoucher(@PathVariable("voucherId") VoucherIdRequest voucherIdRequest) {
return ResponseEntity.ok(voucherService.removeVoucher(voucherIdRequest));
}

@GetMapping("/vouchers/{voucherId}")
public ResponseEntity<VoucherResponse> findById(@PathVariable("voucherId") VoucherIdRequest voucherIdRequest) {
return ResponseEntity.ok(voucherService.findById(voucherIdRequest));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.prgmrs.voucher.controller.mvc;

import com.prgmrs.voucher.controller.mvc.dto.VoucherMvcCreateRequest;
import com.prgmrs.voucher.controller.mvc.dto.VoucherMvcRemoveRequest;
import com.prgmrs.voucher.dto.request.VoucherIdRequest;
import com.prgmrs.voucher.dto.request.VoucherRequest;
import com.prgmrs.voucher.dto.response.VoucherListResponse;
import com.prgmrs.voucher.model.Voucher;
import com.prgmrs.voucher.service.VoucherService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/vouchers")
public class VoucherMvcController {
private final VoucherService voucherService;

public VoucherMvcController(VoucherService voucherService) {
this.voucherService = voucherService;
}

@GetMapping("/")
public String showMainPage() {
return "voucher-main";
}

@GetMapping("/list")
public String showListPage(Model model) {
VoucherListResponse voucherListResponse = voucherService.findAll();
List<Voucher> voucherList = voucherListResponse.voucherList();
model.addAttribute("voucherList", voucherList);
return "voucher-list";
}

@GetMapping("/create")
public String showCreatePage(Model model) {
model.addAttribute("voucherCreateRequest", new VoucherMvcCreateRequest());
return "voucher-create";
}

@PostMapping("/create")
public String createVoucher(@ModelAttribute VoucherMvcCreateRequest voucherMvcCreateRequest) {
voucherService.createVoucher(new VoucherRequest(voucherMvcCreateRequest.getDiscountType(), voucherMvcCreateRequest.getDiscountStringValue()));
return "voucher-request-success";
}

@GetMapping("/remove")
public String showRemovePage(Model model) {
VoucherListResponse voucherListResponse = voucherService.findAll();
List<Voucher> voucherList = voucherListResponse.voucherList();
model.addAttribute("voucherList", voucherList);
model.addAttribute("voucherRemoveRequest", new VoucherMvcRemoveRequest());
return "voucher-remove";
}

@PostMapping("/remove")
public String removeVoucher(@ModelAttribute VoucherMvcRemoveRequest voucherMvcRemoveRequest) {
voucherService.removeVoucher(new VoucherIdRequest(voucherMvcRemoveRequest.getVoucherId()));
return "voucher-request-success";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.prgmrs.voucher.controller.mvc.dto;

public class VoucherMvcCreateRequest {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setter를 안써도 됩니다.
ModelAttribute에 대해 알아보세요

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private String discountType;
private String discountStringValue;

public VoucherMvcCreateRequest() {
// Do nothing for thymeleaf support purpose
}

public String getDiscountType() {
return discountType;
}

public void setDiscountType(String discountType) {
this.discountType = discountType;
}

public String getDiscountStringValue() {
return discountStringValue;
}

public void setDiscountStringValue(String discountStringValue) {
this.discountStringValue = discountStringValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.prgmrs.voucher.controller.mvc.dto;

public class VoucherMvcRemoveRequest {
private String voucherId;

public VoucherMvcRemoveRequest() {
// Do nothing for thymeleaf support purpose
}

public String getVoucherId() {
return voucherId;
}

public void setVoucherId(String voucherId) {
this.voucherId = voucherId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.prgmrs.voucher.controller.xml;

import com.prgmrs.voucher.dto.request.VoucherIdRequest;
import com.prgmrs.voucher.dto.request.VoucherRequest;
import com.prgmrs.voucher.dto.request.VoucherSearchRequest;
import com.prgmrs.voucher.dto.response.RemoveResponse;
import com.prgmrs.voucher.dto.response.VoucherListResponse;
import com.prgmrs.voucher.dto.response.VoucherResponse;
import com.prgmrs.voucher.service.VoucherService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/xml/v1")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VoucherJsonController와
마찬가지에요

public class VoucherXmlController {
private final VoucherService voucherService;

public VoucherXmlController(VoucherService voucherService) {
this.voucherService = voucherService;
}

@GetMapping(value = "/vouchers", produces = "application/xml")
public ResponseEntity<VoucherListResponse> findAll() {
return ResponseEntity.ok(voucherService.findAll());
}
Comment on lines +22 to +25

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 한가지 언급안했던 부분이 있긴한데 ㅠ
produces 속성 안에 들어가보시면 알겠지만 배열입니다.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE} 이런 형상이 가능해져요 ㅎㅎ

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

형식별로 두개의 컨트롤러를 만들어야 하나 생각했는데, 이렇게 배열로 처리 할 수 있군요..!


@GetMapping(value = "/vouchers/search", produces = "application/xml")
public ResponseEntity<VoucherListResponse> findByCreationTimeAndDiscountType(VoucherSearchRequest voucherSearchRequest) {
return ResponseEntity.ok(voucherService.findByCreationTimeAndDiscountType(voucherSearchRequest));
}

@PostMapping(value = "/vouchers", consumes = "application/xml", produces = "application/xml")
public ResponseEntity<VoucherResponse> createVoucher(@RequestBody VoucherRequest voucherRequest) {
System.out.println(voucherRequest); // or use a logger
return ResponseEntity.ok(voucherService.createVoucher(voucherRequest));
}

@DeleteMapping(value = "/vouchers/{voucherId}", produces = "application/xml")
public ResponseEntity<RemoveResponse> removeVoucher(@PathVariable("voucherId") VoucherIdRequest voucherIdRequest) {
return ResponseEntity.ok(voucherService.removeVoucher(voucherIdRequest));
}

@GetMapping(value = "/vouchers/{voucherId}", produces = "application/xml")
public ResponseEntity<VoucherResponse> findById(@PathVariable("voucherId") VoucherIdRequest voucherIdRequest) {
return ResponseEntity.ok(voucherService.findById(voucherIdRequest));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.prgmrs.voucher.dto.request;

public record VoucherRequest(String discountType, String discountStringValue) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.prgmrs.voucher.dto.request;

import com.prgmrs.voucher.exception.WrongRangeFormatException;

import java.time.LocalDateTime;

public record VoucherSearchRequest(LocalDateTime startDate, LocalDateTime endDate, String discountType) {
public VoucherSearchRequest {
if (startDate == null) {
throw new WrongRangeFormatException("startDate cannot be null");
}
if (endDate == null) {
throw new WrongRangeFormatException("endDate cannot be null");
}
if (discountType == null || discountType.isBlank()) {
throw new WrongRangeFormatException("discountType cannot be null or blank");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.prgmrs.voucher.dto.response;

public record RemoveResponse(int rowsDeleted) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
import java.util.UUID;

public record VoucherResponse(UUID voucherUuid, DiscountStrategy discountStrategy) {
}
}
Loading