GoodsController.java
Java/Baasid/src/main/java/com/baasid/controller/GoodsController.java
package com.baasid.controller;
import com.baasid.dto.GoodsRequest;
import com.baasid.dto.GoodsResponse;
import com.baasid.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
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;
import java.util.List;
import java.util.UUID;
/**
* 商品 CRUD REST API,基底路徑 {@code /goods}。
* <p>
* 需通過 Spring Security 認證;建立者/更新者由 JWT 解析出的 {@link UUID}({@link Authentication#getPrincipal()})帶入服務層。
*/
@RestController
@RequestMapping("/goods")
@Tag(name = "Goods", description = "商品管理")
public class GoodsController {
private final GoodsService goodsService;
public GoodsController(GoodsService goodsService) {
this.goodsService = goodsService;
}
/** {@code POST /goods/add}:新增商品,寫入建立者與更新者為目前登入使用者。 */
@PostMapping("/add")
@Operation(
summary = "新增商品",
responses = {
@ApiResponse(responseCode = "200", description = "成功",
content = @Content(schema = @Schema(implementation = GoodsResponse.class))),
@ApiResponse(responseCode = "403", description = "權限不足", content = @Content),
@ApiResponse(responseCode = "500", description = "系統錯誤", content = @Content)
}
)
public ResponseEntity<GoodsResponse> addGoods(
@Valid @RequestBody GoodsRequest request,
Authentication authentication) {
UUID userId = (UUID) authentication.getPrincipal();
GoodsResponse response = goodsService.addGoods(request, userId);
return ResponseEntity.ok(response);
}
/** {@code GET /goods}:列出全部商品。 */
@GetMapping
@Operation(
summary = "取得所有商品",
responses = {
@ApiResponse(responseCode = "200", description = "成功",
content = @Content(array = @ArraySchema(
schema = @Schema(implementation = GoodsResponse.class)))),
@ApiResponse(responseCode = "403", description = "權限不足", content = @Content),
@ApiResponse(responseCode = "500", description = "系統錯誤", content = @Content)
}
)
public ResponseEntity<List<GoodsResponse>> getAllGoods() {
return ResponseEntity.ok(goodsService.getAllGoods());
}
/** {@code GET /goods/{id}}:依主鍵查單筆。 */
@GetMapping("/{id}")
@Operation(
summary = "取得指定商品",
responses = {
@ApiResponse(responseCode = "200", description = "成功",
content = @Content(schema = @Schema(implementation = GoodsResponse.class))),
@ApiResponse(responseCode = "403", description = "權限不足", content = @Content),
@ApiResponse(responseCode = "500", description = "系統錯誤", content = @Content)
}
)
public ResponseEntity<GoodsResponse> getGoodsById(@PathVariable UUID id) {
return ResponseEntity.ok(goodsService.getGoodsById(id));
}
/** {@code PUT /goods/{id}}:更新名稱與最後更新者。 */
@PutMapping("/{id}")
@Operation(
summary = "更新商品",
responses = {
@ApiResponse(responseCode = "200", description = "成功",
content = @Content(schema = @Schema(implementation = GoodsResponse.class))),
@ApiResponse(responseCode = "403", description = "權限不足", content = @Content),
@ApiResponse(responseCode = "500", description = "系統錯誤", content = @Content)
}
)
public ResponseEntity<GoodsResponse> updateGoods(
@PathVariable UUID id,
@Valid @RequestBody GoodsRequest request,
Authentication authentication) {
UUID userId = (UUID) authentication.getPrincipal();
GoodsResponse response = goodsService.updateGoods(id, request, userId);
return ResponseEntity.ok(response);
}
/** {@code DELETE /goods/{id}}:刪除指定商品。 */
@DeleteMapping("/{id}")
@Operation(
summary = "刪除商品",
responses = {
@ApiResponse(responseCode = "200", description = "成功"),
@ApiResponse(responseCode = "403", description = "權限不足", content = @Content),
@ApiResponse(responseCode = "500", description = "系統錯誤", content = @Content)
}
)
public ResponseEntity<Void> deleteGoods(@PathVariable UUID id) {
goodsService.deleteGoods(id);
return ResponseEntity.ok().build();
}
}
Articoli correlati
init.sql
init.sql — sql source code from the Java learning materials (Java/Baasid/sql/init.sql).
Leggi l'articolo →BaasidApplication.java
BaasidApplication.java — java source code from the Java learning materials (Java/Baasid/src/main/java/com/baasid/BaasidApplication.java).
Leggi l'articolo →DataInitializer.java
DataInitializer.java — java source code from the Java learning materials (Java/Baasid/src/main/java/com/baasid/config/DataInitializer.java).
Leggi l'articolo →OpenApiConfig.java
OpenApiConfig.java — java source code from the Java learning materials (Java/Baasid/src/main/java/com/baasid/config/OpenApiConfig.java).
Leggi l'articolo →SecurityConfig.java
SecurityConfig.java — java source code from the Java learning materials (Java/Baasid/src/main/java/com/baasid/config/SecurityConfig.java).
Leggi l'articolo →AuthController.java
AuthController.java — java source code from the Java learning materials (Java/Baasid/src/main/java/com/baasid/controller/AuthController.java).
Leggi l'articolo →