<commit_before>package nl.kreditor.controller.api;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import javassist.NotFoundException;
import nl.kreditor.component.solver.TransferSimplifier;
import nl.kreditor.form.OperationForm;
import nl.kreditor.model.Book;
import nl.kreditor.model.Operation;
import nl.kreditor.model.PaymentType;
import nl.kreditor.repository.CategoryRepository;
import nl.kreditor.response.OperationResponse;
import nl.kreditor.response.TransactionResponse;
import nl.kreditor.service.BookService;
import nl.kreditor.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin("*")
@RequestMapping("/api/v1/books/{bookId}/operations")
public class OperationController {
    @Autowired
    private BookService bookService;

    @Autowired
    private OperationService operationService;

    @Autowired
    private CategoryRepository categoryRepository;

    @GetMapping("/")
    @io.swagger.v3.oas.annotations.Operation(tags = {"app"})
    List<OperationResponse> getOperations(@PathVariable Integer bookId) throws NotFoundException {
        return operationService
                .findByBook(bookService.findById(bookId))
                .stream()
                .map(OperationResponse::new)
                .collect(Collectors.toList());
    }

    @PostMapping("/")
    @io.swagger.v3.oas.annotations.Operation(tags = {"app"})
    OperationResponse postOperation(
            @PathVariable("bookId") @Parameter(schema = @Schema(type = "integer")) Book book,
            @Valid @RequestBody OperationForm operationRequest
    ) {
        Operation operation = new Operation(operationRequest, book);
        return new OperationResponse(operationService.persist(operation));
    }

    @PutMapping("/{operationId}")
    @io.swagger.v3.oas.annotations.Operation(tags = {"app"})
    OperationResponse putOperation(
            @PathVariable("bookId") @Parameter(schema = @Schema(type = "integer")) Book book,
            @PathVariable("operationId") @Parameter(schema = @Schema(type = "integer")) Operation operation,
            @Valid @RequestBody OperationForm operationForm
    ) {
        //TODO: set parent relation to book

        operation.setModified(LocalDateTime.now());
        operation.setName(operationForm.getName());
        operation.setCategory(categoryRepository.findById(operationForm.getCategoryId()));
        operation.setType(operationForm.getType());
        operation.setPaymentType(PaymentType.UNKNOWN);
        return null;
    }

    @GetMapping("/{operationId}")
    @io.swagger.v3.oas.annotations.Operation(tags = {"app"})
    OperationResponse getOperation(
            @PathVariable("bookId") @Parameter(schema = @Schema(type = "integer")) Book book,
            @PathVariable("operationId") @Parameter(schema = @Schema(type = "integer")) Operation operation
    ) {
        if (!operation.getBook().equals(book)) {<commit_msg>Fix parent relation to Book in OperationController<commit_after>package nl.kreditor.controller.api;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import javassist.NotFoundException;
import nl.kreditor.component.solver.TransferSimplifier;
import nl.kreditor.form.OperationForm;
import nl.kreditor.model.Book;
import nl.kreditor.model.Operation;
import nl.kreditor.model.PaymentType;
import nl.kreditor.repository.CategoryRepository;
import nl.kreditor.response.OperationResponse;
import nl.kreditor.response.TransactionResponse;
import nl.kreditor.service.BookService;
import nl.kreditor.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin("*")
@RequestMapping("/api/v1/books/{bookId}/operations")
public class OperationController {
    @Autowired
    private BookService bookService;

    @Autowired
    private OperationService operationService;

    @Autowired
    private CategoryRepository categoryRepository;

    @GetMapping("/")
    @io.swagger.v3.oas.annotations.Operation(tags = {"app"})
    List<OperationResponse> getOperations(@PathVariable Integer bookId) throws NotFoundException {
        return operationService
                .findByBook(bookService.findById(bookId))
                .stream()
                .map(OperationResponse::new)
                .collect(Collectors.toList());
    }

    @PostMapping("/")
    @io.swagger.v3.oas.annotations.Operation(tags = {"app"})
    OperationResponse postOperation(
            @PathVariable("bookId") @Parameter(schema = @Schema(type = "integer")) Book book,
            @Valid @RequestBody OperationForm operationRequest
    ) {
        Operation operation = new Operation(operationRequest, book);
        return new OperationResponse(operationService.persist(operation));
    }

    @PutMapping("/{operationId}")
    @io.swagger.v3.oas.annotations.Operation(tags = {"app"})
    OperationResponse putOperation(
            @PathVariable("bookId") @Parameter(schema = @Schema(type = "integer")) Book book,
            @PathVariable("operationId") @Parameter(schema = @Schema(type = "integer")) Operation operation,
            @Valid @RequestBody OperationForm operationForm
    ) {
        operation.setBook(book);
        operation.setModified(LocalDateTime.now());
        operation.setName(operationForm.getName());
        operation.setCategory(categoryRepository.findById(operationForm.getCategoryId()));
        operation.setType(operationForm.getType());
        operation.setPaymentType(PaymentType.UNKNOWN);

        return new OperationResponse(operationService.persist(operation));
    }

    @GetMapping("/{operationId}")
    @io.swagger.v3.oas.annotations.Operation(tags = {"app"})
    OperationResponse getOperation(
            @PathVariable("bookId") @Parameter(schema = @Schema(type = "integer")) Book book,
            @PathVariable("operationId") @Parameter(schema = @Schema(type = "integer")) Operation operation
    ) {
        if (!operation.getBook().equals(book)) {
{'fraction_non_alphanumeric': 0.11869158878504672, 'fraction_numerical': 0.0021806853582554517, 'mean_word_length': 4.059889676910953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5107222', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1745, 'n_words': 300}