su.vistar.drivers.web.api.DriversController.java Source code

Java tutorial

Introduction

Here is the source code for su.vistar.drivers.web.api.DriversController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package su.vistar.drivers.web.api;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import su.vistar.drivers.model.Category;
import su.vistar.drivers.model.Driver;
import su.vistar.drivers.model.Gender;
import su.vistar.drivers.services.categories.CategoriesService;
import su.vistar.drivers.services.drivers.DriversService;
import su.vistar.drivers.web.api.dto.DriverDTO;
import su.vistar.drivers.web.api.dto.GenderDTO;

/**
 *
 * @author aleksandr-gorovoi
 */
@RestController
@RequestMapping(value = "api/drivers/")
public class DriversController {

    private Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    DriversService driversService;

    @Autowired
    CategoriesService categoriesService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public @ResponseBody List<DriverDTO> getDriversList(
            @RequestParam(value = "query", required = false) String query,
            @RequestParam(value = "limit", required = false) Integer limit) {
        logger.info("?   ?? ");
        Stream<Driver> stream = driversService.getDriversList().stream();
        if (query != null) {
            String[] parts = query.toLowerCase().split(" ");
            stream = stream.filter((_d) -> {
                String lowerName = _d.getName().toLowerCase(), lowerSurname = _d.getSurname().toLowerCase(),
                        lowerPatronimyc = _d.getPatronimyc().toLowerCase();
                int score = 0;
                for (String part : parts) {
                    if (lowerName.contains(part) || lowerSurname.contains(part) || lowerPatronimyc.contains(part))
                        score++;
                }
                return score >= parts.length;
            });
        }
        if (limit != null) {
            stream = stream.limit(limit.longValue());
        }
        return stream.map(_d -> new DriverDTO(_d)).collect(Collectors.toList());
    }

    @RequestMapping(value = "/{id}/", method = RequestMethod.GET)
    public @ResponseBody DriverDTO getDriver(@PathVariable("id") int id) {
        logger.info(String.format("?   ? ? id %d", id));
        Driver driver = driversService.getDriver(id);
        return new DriverDTO(driver);
    }

    @RequestMapping(value = "/{id}/", method = RequestMethod.PATCH)
    public @ResponseBody DriverDTO updateDriver(@PathVariable("id") int id, @RequestBody DriverDTO updatedDTO) {
        logger.info(String.format("?   ? ? id %d", id));
        Driver updated = new Driver();
        updated.setSurname(updatedDTO.surname);
        updated.setName(updatedDTO.name);
        updated.setPatronimyc(updatedDTO.patronimyc);
        updated.setBirthdate(updatedDTO.birthdate);
        Gender.Variant gender = Gender.Variant.valueOf(updatedDTO.genderId);
        if (gender != null) {
            updated.setGender(gender.getGender());
        }
        if (updatedDTO.categoryId != null) {
            Category category = categoriesService.getCategoryById(updatedDTO.categoryId);
            updated.setCategory(category);
        }
        //
        Driver returned = driversService.updateDriver(id, updated);
        return new DriverDTO(returned);
    }

    @RequestMapping(value = "/genders", method = RequestMethod.GET)
    public @ResponseBody List<GenderDTO> getGenderList() {
        List<GenderDTO> genders = Arrays.stream(Gender.Variant.values()).map(_v -> new GenderDTO(_v.getGender()))
                .collect(Collectors.toList());
        return genders;
    }

    /*@RequestMapping(value = "/{id}/", method = RequestMethod.DELETE)
    public void deleteDriver(@PathVariable("id") int id){
    logger.info(String.format("?   ? ? id %d", id));
    driversService.deleteDriver(id);
    }*/
}