AccountingController.java

package com.tdmconsult.ete.api;

import com.tdmconsult.ete.accounting.AccountingService;
import com.tdmconsult.ete.accounting.SKR03Account;
import com.tdmconsult.ete.utils.JsonCurrencyAmountCents;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "NAme", description = "Description")
@RestController
@RequestMapping(value = "/api/accounting", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
@PreAuthorize("hasRole('ete_admin')")
public class AccountingController {

    private final AccountingService accountingService;

    @Data
    @Accessors(chain = true)
    public static class RecordsDto {

        private List<RecordDto> records = new ArrayList<>();

        @Builder(setterPrefix = "set")
        @EqualsAndHashCode
        @Getter
        public static class RecordDto {

            private String description;
            private LocalDate tradingDate;
            private SKR03Account debitAccount;
            private SKR03Account creditAccount;
            @JsonCurrencyAmountCents
            private BigDecimal amount;
        }

    }

    @GetMapping("{accountNumber}")
    public ResponseEntity<RecordsDto> getSecurityAccount(
            final @PathVariable int accountNumber,
            final @RequestParam int fiscalYear) {

        final var accountingRecords = accountingService.getAccountingRecords(fiscalYear, accountNumber);

        final var accountingRecordsDtos = accountingRecords.stream()
                .filter(ar -> ar.getTradingDate().getYear() == fiscalYear)
                .map(ar -> RecordsDto.RecordDto.builder()
                .setDescription(ar.getDescription())
                .setTradingDate(ar.getTradingDate())
                .setCreditAccount(ar.getCreditAccount())
                .setDebitAccount(ar.getDebitAccount())
                .setAmount(ar.getAmount()).build()).collect(Collectors.toList());

        final var answer = new RecordsDto();
        answer.setRecords(accountingRecordsDtos);
        return ResponseEntity.ok(answer);
    }

}