org.homiefund.web.controllers.TransactionController.java Source code

Java tutorial

Introduction

Here is the source code for org.homiefund.web.controllers.TransactionController.java

Source

/**
 * Copyright  2016 REPLACE ME OWNER (REPLACE ME YEAR)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.homiefund.web.controllers;

import lombok.extern.log4j.Log4j2;
import org.dozer.Mapper;
import org.homiefund.api.dto.DebtPreviewDTO;
import org.homiefund.api.dto.TransactionDTO;
import org.homiefund.api.dto.TransactionParticipantDTO;
import org.homiefund.api.dto.TransactionTypeDTO;
import org.homiefund.api.service.DebtPreviewService;
import org.homiefund.api.service.TransactionService;
import org.homiefund.web.forms.PurchaseForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.stream.Collectors;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 25.9.2016.
 */
@Log4j2
@Controller
@RequestMapping("/auth/transaction")
public class TransactionController {
    @Autowired
    private TransactionService transactionService;
    @Autowired
    private Mapper mapper;
    @Autowired
    private DebtPreviewService debtPreviewService;

    @PostMapping("/purchase/")
    public String submitPurchase(@ModelAttribute PurchaseForm purchaseForm) {
        log.info(purchaseForm);
        TransactionDTO transactionDTO = new TransactionDTO();
        transactionDTO.setDescription(purchaseForm.getDescription());
        TransactionTypeDTO tt = new TransactionTypeDTO();
        tt.setId(Long.valueOf(1L));
        transactionDTO.setTransactionType(tt);
        transactionDTO.setAmount(purchaseForm.getAmount());
        transactionDTO
                .setParticipants(purchaseForm.getParticipants().stream().filter(p -> p != null && p.getInclude()) //null check just in case
                        .map(p -> mapper.map(p, TransactionParticipantDTO.class)).collect(Collectors.toList()));
        transactionDTO.setRevoked(false);

        transactionService.create(transactionDTO);

        return "redirect:/auth/";
    }

    @GetMapping("/ajax/debts/")
    public @ResponseBody DebtPreviewDTO debtPreviewDTO() {
        return debtPreviewService.getOverview();
    }

}