Skip to content
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(KL-157): add Location header #56

Merged
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package taco.klkl.domain.product.controller;

import java.net.URI;
import java.util.Set;

import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -14,8 +14,8 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -86,11 +86,12 @@ public ProductDetailResponse findProductById(

@PostMapping
@Operation(summary = "μƒν’ˆ 등둝", description = "μƒν’ˆμ„ λ“±λ‘ν•©λ‹ˆλ‹€.")
@ResponseStatus(HttpStatus.CREATED)
public ProductDetailResponse createProduct(
public ResponseEntity<ProductDetailResponse> createProduct(
@Valid @RequestBody ProductCreateUpdateRequest createRequest
) {
return productService.createProduct(createRequest);
ProductDetailResponse createdProduct = productService.createProduct(createRequest);
URI location = createResourceLocation(createdProduct.id());
return ResponseEntity.created(location).body(createdProduct);
}

@PutMapping("/{productId}")
Expand All @@ -111,4 +112,11 @@ public ResponseEntity<Void> deleteProduct(
return ResponseEntity.noContent().build();
}

private URI createResourceLocation(final Long resourceId) {
ohhamma marked this conversation as resolved.
Show resolved Hide resolved
return ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{productId}")
.buildAndExpand(resourceId)
.toUri();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public PagedResponseDto<ProductSimpleResponse> findProductsByFilterOptionsAndSor
public ProductDetailResponse findProductById(final Long id) throws ProductNotFoundException {
final Product product = productRepository.findById(id)
.orElseThrow(ProductNotFoundException::new);
return taco.klkl.domain.product.dto.response.ProductDetailResponse.from(product);
return ProductDetailResponse.from(product);
}

@Override
Expand All @@ -105,7 +105,7 @@ public ProductDetailResponse createProduct(final ProductCreateUpdateRequest crea
Set<Tag> tags = createTagsByTagIds(createRequest.tagIds());
product.addTags(tags);
}
return taco.klkl.domain.product.dto.response.ProductDetailResponse.from(product);
return ProductDetailResponse.from(product);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ void testCreateProduct_ShouldReturnCreatedProduct() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(productCreateUpdateRequest)))
.andExpect(status().isCreated())
.andExpect(header().string("Location", containsString("/v1/products/")))
.andExpect(jsonPath("$.isSuccess", is(true)))
.andExpect(jsonPath("$.data.id", is(productDetailResponse.id().intValue())))
.andExpect(jsonPath("$.data.name", is(productDetailResponse.name())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void testCreateProduct() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(createRequest)))
.andExpect(status().isCreated())
.andExpect(header().string("Location", containsString("/v1/products/")))
.andExpect(jsonPath("$.isSuccess", is(true)))
.andExpect(jsonPath("$.data.id", notNullValue()))
.andExpect(jsonPath("$.data.name", is(createRequest.name())))
Expand Down