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

Java tutorial

Introduction

Here is the source code for org.homiefund.web.controllers.HomeController.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.HomeDTO;
import org.homiefund.api.service.HomeService;
import org.homiefund.api.service.InviteService;
import org.homiefund.exceptions.FieldException;
import org.homiefund.web.forms.HomeForm;
import org.homiefund.web.forms.InviteForm;
import org.homiefund.web.support.Attributed;
import org.homiefund.web.support.InviteToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 23.9.2016.
 */
@Log4j2
@Controller
@Attributed
@RequestMapping("/auth/home")
public class HomeController {
    @Autowired
    private HomeService homeService;
    @Autowired
    private InviteService inviteService;
    @Autowired
    private Mapper mapper;

    @GetMapping("/")
    public String list(Model model) {
        model.addAttribute("homeList", homeService.getHomes());
        model.addAttribute("homeForm", new HomeForm());
        return "home.list";
    }

    @PostMapping("/")
    public String submitHome(@Valid @ModelAttribute HomeForm homeForm, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "home.list";
        } else {
            try {
                homeService.create(mapper.map(homeForm, HomeDTO.class));
            } catch (FieldException e) {
                result.rejectValue(e.getField(), e.getMessage());

                return "home.list";
            }
            return "redirect:/auth/home/";
        }
    }

    @GetMapping("/{homeID}/")
    public String show(@PathVariable Long homeID, Model model) {
        model.addAttribute("home", homeService.getByID(homeID));

        return "home.entry";
    }

    @PostMapping("/invite/")
    public String invite(@ModelAttribute InviteForm inviteForm, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            return "home.entry";
        } else {
            HomeDTO home = new HomeDTO();
            home.setId(inviteForm.getHomeID());
            inviteService.sendInvite(home, inviteForm.getEmail());

            return "redirect:/auth/home/" + home.getId() + "/";
        }
    }

    @PostMapping(value = "/token/")
    public String token(@ModelAttribute InviteToken inviteToken) {
        inviteService.acceptInvite(inviteToken.getInviteToken());

        return "redirect:/auth/home/";
    }

    @ModelAttribute(name = "inviteForm")
    public InviteForm inviteForm() {
        return new InviteForm();
    }
}