定義實體類,該類包含資產的相關資訊,用於映射到assets資料庫表。
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "_assets")
public class AssetsEntity {
@CreatedBy
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
private AppUserEntity appUser;
@Id
@GeneratedValue
private Long id;
private String assetsType;
private String assetsName;
private Integer assetsValue;
@LastModifiedDate
@JsonIgnore
private LocalDate recorded_at;
}
Assets Repository 接口,通過自定義的JPQL查詢對AssetsEntity進行資料庫操作。
用於查詢資料庫的自訂SQL查詢語句,根據日期和使用者篩選 AssetsEntity 物件。
@Repository
public interface AssetsRepository extends JpaRepository<AssetsEntity, Long> {
@Query("SELECT e FROM AssetsEntity e WHERE DATE(e.recorded_at) = :date AND e.appUser = :appUser")
List<AssetsEntity> findAssetsByDateAndAppUser(@Param("date") LocalDate date, AppUserEntity appUser);
}
用於處理資產相關邏輯,返回相應的響應數據。
@Service
@AllArgsConstructor
public class AssetsService {
private final AssetsRepository assetsRepository;
public Map<String, Object> getAssets(String request) {
var response = new LinkedHashMap<String, Object>();
var appUser = (AppUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
var Data = new ArrayList<>();
LocalDate date = LocalDate.parse(request, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
var assetsData = assetsRepository.findAssetsByDateAndAppUser(date,appUser);
if (!assetsData.isEmpty()){
for(var assets:assetsData){
Data.add(assets);
}
}
response.put("status", "成功");
response.put("message","ok");
response.put("assetsDataList", Data);
return response;
}
public Map<String, Object> addAssets(AssetsRequest request){
var response = new LinkedHashMap<String, Object>();
var appUser = (AppUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
var assetsData = AssetsEntity
.builder()
.appUser(appUser)
.assetsType(request.getAssetsType())
.assetsName(request.getAssetsName())
.assetsValue(request.getAssetsValue())
.recorded_at(request.getRecorded_at())
.build();
assetsRepository.save(assetsData);
response.put("status", "成功");
response.put("message", "ok");
return response;
}
public Map<String, Object> deleteAssets(Long request){
var response = new LinkedHashMap<String, Object>();
assetsRepository.deleteById(request);
response.put("status", "成功");
response.put("message", "ok");
return response;
}
}
Spring Boot控制器,用於處理支出相關的 HTTP 請求。
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user")
@Tag(name = "資產模塊")
public class AssetsController {
private final AssetsService service;
@GetMapping("/assets")
@Operation(summary = "資產列表資料")
public ResponseEntity<?> getAssets(
@RequestParam("date") String date
) {
return ResponseEntity.ok(service.getAssets(date));
}
@PostMapping("/assets")
@Operation(summary = "新增資產資料")
public ResponseEntity<?> addAssets(
@RequestBody AssetsRequest request
) {
return ResponseEntity.ok(service.addAssets(request));
}
@DeleteMapping("/assets")
@Operation(summary = "刪除資產記錄")
public ResponseEntity<?> deleteAssets(
@RequestParam("id") Long request
) {
return ResponseEntity.ok(service.deleteAssets(request));
}
}
@Data
@Schema(name = "AssetsRequest", description = "資產新增請求")
public class AssetsRequest {
@Schema(description = "資產名稱")
private String assetsName;
@Schema(description = "資產金額")
private Integer assetsValue;
@Schema(description = "資產類型")
private String assetsType;
@Schema(description = "使用者記錄時間")
private LocalDate recorded_at;
}