114 lines
3.8 KiB
Java
114 lines
3.8 KiB
Java
package io.controller;
|
||
|
||
|
||
import io.annotation.Login;
|
||
import io.common.constant.Constant;
|
||
import io.common.page.PageData;
|
||
import io.common.utils.Result;
|
||
import io.common.validator.AssertUtils;
|
||
import io.common.validator.ValidatorUtils;
|
||
import io.common.validator.group.AddGroup;
|
||
import io.common.validator.group.DefaultGroup;
|
||
import io.common.validator.group.UpdateGroup;
|
||
import io.modules.item.dto.CartDTO;
|
||
import io.modules.item.dto.HouseDTO;
|
||
import io.modules.item.service.CartService;
|
||
import io.modules.item.service.HouseService;
|
||
import io.swagger.v3.oas.annotations.Operation;
|
||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.beans.factory.annotation.Value;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import io.swagger.v3.oas.annotations.Parameter;
|
||
import io.swagger.v3.oas.annotations.Parameters;
|
||
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.stream.Collectors;
|
||
|
||
|
||
/**
|
||
* 购物车
|
||
*
|
||
* @author Mark #
|
||
* @since 1.0.0 2025-03-12
|
||
*/
|
||
@RestController
|
||
@RequestMapping("api/cart")
|
||
@Tag(name="购物车")
|
||
@CrossOrigin
|
||
public class CartController {
|
||
@Autowired
|
||
private CartService cartService;
|
||
@Value("${upload.url}")
|
||
private String uploadUrl;
|
||
@Autowired
|
||
private HouseService itemService;
|
||
|
||
@GetMapping("page")
|
||
@Operation(summary = "分页")
|
||
@Parameters({
|
||
@Parameter(name = Constant.PAGE, description = "当前页码,从1开始", in = ParameterIn.QUERY, required = true, ref="int") ,
|
||
@Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY,required = true, ref="int") ,
|
||
@Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref="String") ,
|
||
@Parameter(name = Constant.ORDER, description = "排序方式,可选值(asc、desc)", in = ParameterIn.QUERY, ref="String")
|
||
})
|
||
@Login
|
||
public Result<PageData<CartDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params,@Parameter(hidden = true) @RequestAttribute("userId") Long userId){
|
||
params.put("userId",userId.toString());
|
||
PageData<CartDTO> page = cartService.page(params);
|
||
|
||
List<CartDTO> collect = page.getList().stream().map(e -> {
|
||
Long productId = e.getProductId();
|
||
HouseDTO itemDTO = itemService.get(productId);
|
||
if (itemDTO != null) {
|
||
itemDTO.setImage(uploadUrl + itemDTO.getImage());
|
||
e.setItem(itemDTO);
|
||
}
|
||
return e;
|
||
}).collect(Collectors.toList());
|
||
page.setList(collect);
|
||
return new Result<PageData<CartDTO>>().ok(page);
|
||
}
|
||
@GetMapping("{id}")
|
||
@Operation(summary = "信息")
|
||
public Result<CartDTO> get(@PathVariable("id") Long id){
|
||
CartDTO data = cartService.get(id);
|
||
|
||
return new Result<CartDTO>().ok(data);
|
||
}
|
||
|
||
@PostMapping
|
||
@Operation(summary = "保存")
|
||
@Login
|
||
public Result save(@RequestBody CartDTO dto,@Parameter(hidden = true) @RequestAttribute("userId") Long userId){
|
||
dto.setUserId(userId);
|
||
//效验数据
|
||
ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
|
||
cartService.save(dto);
|
||
return new Result();
|
||
}
|
||
|
||
@PutMapping
|
||
@Operation(summary = "修改")
|
||
public Result update(@RequestBody CartDTO dto){
|
||
//效验数据
|
||
ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
|
||
cartService.update(dto);
|
||
return new Result();
|
||
}
|
||
|
||
|
||
@DeleteMapping("{id}")
|
||
@Operation(summary = "删除")
|
||
public Result delete(@PathVariable Long id) {
|
||
Long[] ids = new Long[]{id};
|
||
//效验数据
|
||
AssertUtils.isArrayEmpty(ids, "id");
|
||
cartService.delete(ids);
|
||
return new Result();
|
||
}
|
||
|
||
}
|