Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dao-api/src/main/java/com/epam/brest/dao_api/DriverDtoDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.epam.brest.model.dto.DriverDto;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

public interface DriverDtoDao {
Expand All @@ -13,4 +15,12 @@ public interface DriverDtoDao {
*/

List<DriverDto> findAllDriversWithCountCars();

/**
* Get list of driver from date to date Dto.
*
* @return list of driver from date to date Dto.
*/

List<DriverDto> chooseDriverOnDateRange(String fromDate, String toDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void deleteDriverById(Integer id) {

@Override
public Integer count() {
log.info("Method count() of class {} started", getClass().getName());
return namedParameterJdbcTemplate.queryForObject(DRIVER_COUNT, new MapSqlParameterSource(), Integer.class);
}

Expand Down
14 changes: 10 additions & 4 deletions dao-jdbc/src/main/java/com/epam/brest/dao/Queries.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ public class Queries {

public static final String DRIVER_FIND_ALL = "SELECT * FROM driver";
public static final String DRIVER_FIND_BY_ID = "SELECT * FROM driver WHERE driver_id=:driverId";
public static final String DRIVER_SAVE = "INSERT INTO driver (name, dateStartWork, salary) VALUES (:driverName, :driverDateStartWork, :driverSalary)";
public static final String DRIVER_UPDATE_BY_ID = "UPDATE driver SET name=:driverName, dateStartWork=:driverDateStartWork, salary=:driverSalary WHERE driver_id=:driverId";
public static final String DRIVER_SAVE =
"INSERT INTO driver (name, dateStartWork, salary) VALUES (:driverName, :driverDateStartWork, :driverSalary)";
public static final String DRIVER_UPDATE_BY_ID =
"UPDATE driver SET name=:driverName, dateStartWork=:driverDateStartWork, salary=:driverSalary WHERE driver_id=:driverId";
public static final String DRIVER_DELETE_BY_ID = "DELETE FROM driver WHERE driver_id=:driverId";
public static final String DRIVER_FIND_ALL_NAME = "SELECT name FROM driver";
public static final String DRIVER_COUNT = "SELECT COUNT(*) FROM driver";
public static final String DRIVER_COUNT_CAR = "SELECT d.driver_id as driverId, d.name as driverName, d.dateStartWork as driverDateStartWork, d.salary as driverSalary, COUNT(c.car_id) as countOfCarsAssignedToDriver FROM driver d LEFT JOIN car c ON d.driver_id=c.driver_id GROUP BY d.driver_id";

public static final String DRIVER_COUNT_CAR =
"SELECT d.driver_id as driverId, d.name as driverName, d.dateStartWork as driverDateStartWork, d.salary as driverSalary, COUNT(c.car_id) as countOfCarsAssignedToDriver FROM driver d LEFT JOIN car c ON d.driver_id=c.driver_id GROUP BY d.driver_id";
public static final String DRIVER_FIND_DRIVERS_ON_RANGE_DATE =
"SELECT d.driver_id as driverId, d.name as driverName, d.dateStartWork as driverDateStartWork, d.salary as driverSalary FROM driver d WHERE dateStartWork BETWEEN :fromDateChoose AND :toDateChoose";

public static final String CAR_FIND_ALL = "SELECT * FROM car";
public static final String CAR_FIND_BY_ID = "SELECT * FROM car WHERE car_id=:carId";
public static final String CAR_SAVE ="INSERT INTO car (model, driver_id) VALUES (:carModel, :driverId)";
public static final String CAR_UPDATE_BY_ID = "UPDATE car SET model=:carModel, driver_id=:driverId WHERE car_id=:carId";
public static final String CAR_DELETE_BY_ID = "DELETE FROM car WHERE car_id=:carId";
public static final String CAR_COUNT = "SELECT count(*) FROM car";
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.epam.brest.dao.dto;

import com.epam.brest.dao_api.DriverDtoDao;
import com.epam.brest.model.Driver;
import com.epam.brest.model.dto.DriverDto;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.epam.brest.dao.Queries.DRIVER_COUNT_CAR;
import static com.epam.brest.dao.Queries.DRIVER_FIND_DRIVERS_ON_RANGE_DATE;
import static com.epam.brest.logger.ProjectLogger.log;

public class DriverDtoDaoJdbcImpl implements DriverDtoDao {
Expand All @@ -23,4 +29,13 @@ public List<DriverDto> findAllDriversWithCountCars() {
log.info("Method findAllDriversWithCountCars() of class {} started", getClass().getName());
return namedParameterJdbcTemplate.query(DRIVER_COUNT_CAR, BeanPropertyRowMapper.newInstance(DriverDto.class));
}

@Override
public List<DriverDto> chooseDriverOnDateRange(String fromDate, String toDate) {
log.info("Method chooseDriverOnDateRange() of class {} started", getClass().getName());
Map<String, Object> params = new HashMap<>();
params.put("fromDateChoose", fromDate);
params.put("toDateChoose", toDate);
return namedParameterJdbcTemplate.query(DRIVER_FIND_DRIVERS_ON_RANGE_DATE, params, BeanPropertyRowMapper.newInstance(DriverDto.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

import static com.epam.brest.logger.ProjectLogger.log;
Expand All @@ -34,4 +36,18 @@ void findWithCountCars() {
log.info("List of driver Dto was created {}", drivers);
assertTrue(drivers.get(0).getCountOfCarsAssignedToDriver() > 0);
}

@Test
void chooseDriverOnDateRange() {
log.info("Method started: chooseDriverOnDateRange() of {}", getClass().getName());
// Instant fromDate = Instant.parse("1990-01-02T10:10:10.002Z");
// Instant toDate = Instant.parse("2021-01-02T10:10:10.002Z");
String fromDate = "1990-01-02T10:10:10.002Z";
String toDate = "2021-01-02T10:10:10.002Z";
// Timestamp fromDate = Timestamp.valueOf("1990-01-02 10:10:10.002");
// Timestamp toDate = Timestamp.valueOf("2021-01-02 10:10:10.002");
List<DriverDto> drivers = driverDtoDaoJdbc.chooseDriverOnDateRange(fromDate, toDate);
assertNotNull(drivers);
log.info("List of driver Dto was created {}", drivers);
}
}
21 changes: 21 additions & 0 deletions model/src/main/java/com/epam/brest/model/dto/DriverDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.epam.brest.model.dto;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;

public class DriverDto {
Expand All @@ -10,6 +11,8 @@ public class DriverDto {
private Instant driverDateStartWork;
private BigDecimal driverSalary;
private Integer countOfCarsAssignedToDriver;
private String fromDateChoose;
private String toDateChoose;

public DriverDto() {
}
Expand Down Expand Up @@ -69,6 +72,22 @@ public void setCountOfCarsAssignedToDriver(Integer countOfCarsAssignedToDriver)
this.countOfCarsAssignedToDriver = countOfCarsAssignedToDriver;
}

public String getFromDateChoose() {
return fromDateChoose;
}

public void setFromDateChoose(String fromDateChoose) {
this.fromDateChoose = fromDateChoose;
}

public String getToDateChoose() {
return toDateChoose;
}

public void setToDateChoose(String toDateChoose) {
this.toDateChoose = toDateChoose;
}

@Override
public String toString() {
return "DriverDto{" +
Expand All @@ -77,6 +96,8 @@ public String toString() {
", driverDateStartWork=" + driverDateStartWork +
", driverSalary=" + driverSalary +
", countOfCarsAssignedToDriver=" + countOfCarsAssignedToDriver +
", fromDateChoose=" + fromDateChoose +
", toDateChoose=" + toDateChoose +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.epam.brest.service_api.dto;

import com.epam.brest.model.Driver;
import com.epam.brest.model.dto.DriverDto;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

public interface DriverDtoService {
Expand All @@ -13,4 +16,14 @@ public interface DriverDtoService {
*/

List<DriverDto> findAllDriverWithCountCars();

/**
* Get list of driver from date to date Dto.
*
* @return list of driver from date to date Dto.
*/

List<DriverDto> chooseDriverOnDateRange(String fromDate, String toDate);


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.epam.brest.service.impl.dto;

import com.epam.brest.dao_api.DriverDtoDao;
import com.epam.brest.model.Driver;
import com.epam.brest.model.dto.DriverDto;
import com.epam.brest.service_api.dto.DriverDtoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

@Service
Expand All @@ -22,4 +25,11 @@ public DriverDtoServiceImpl(DriverDtoDao driverDtoDao) {
public List<DriverDto> findAllDriverWithCountCars() {
return driverDtoDao.findAllDriversWithCountCars();
}

@Override
public List<DriverDto> chooseDriverOnDateRange(String fromDate, String toDate) {
return driverDtoDao.chooseDriverOnDateRange(fromDate, toDate);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ void findAllDriverWithCountCars() {
log.info("Test passed, list driver Dto equals {}", drivers);
assertTrue(drivers.get(0).getCountOfCarsAssignedToDriver() > 0);
}

@Test
void chooseDriverOnDateRange() {
log.info("Method started: chooseDriverOnDateRange() of {}", getClass().getName());
String fromDate = "1990-01-02T10:10:10.002Z";
String toDate = "2021-01-02T10:10:10.002Z";
List<DriverDto> drivers = driverDtoService.chooseDriverOnDateRange(fromDate, toDate);
assertNotNull(drivers);
log.info("List of driver Dto was created {}", drivers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import com.epam.brest.controller.validator.DriverValidator;
import com.epam.brest.model.Driver;
import com.epam.brest.model.dto.DriverDto;
import com.epam.brest.service_api.DriverService;
import com.epam.brest.service_api.dto.DriverDtoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.Instant;

import static com.epam.brest.logger.ProjectLogger.log;

@Controller
Expand Down Expand Up @@ -78,4 +83,40 @@ public String deleteDriver(@PathVariable("id") Integer id) {
driverService.deleteDriverById(id);
return "redirect:/drivers";
}

@GetMapping("/choose-date-range")
public String showFormForChoseDateRange(@ModelAttribute("driver") DriverDto driverDto) {
log.info("Method showFormForChoseDateRange() started of class {}", getClass().getName());
return "drivers/choose-date-range";
}

@GetMapping("/drivers-range")
public String showDriversListOnRange(@ModelAttribute("driver") DriverDto driverDto, Model model) {
log.info("Method showDriversListOnRange() started of class {}", getClass().getName());
// Timestamp fromDateTimestamp = Timestamp.valueOf(fromDate);
// Timestamp toDateTimestamp = Timestamp.valueOf(toDate);
// Instant fromDateTime = Instant.parse(fromDate);
// Instant toDateTime = Instant.parse(toDate);
model.addAttribute("driverList", driverDtoService.chooseDriverOnDateRange(driverDto.getFromDateChoose(), driverDto.getToDateChoose()));
return "drivers/drivers-range";
}

// @GetMapping("/choose-date-range")
// public String showFormForChoseDateRange() {
// log.info("Method showFormForChoseDateRange() started of class {}", getClass().getName());
// return "drivers/choose-date-range1";
// }
//
// @GetMapping("/drivers-range?fromDate={from}&toDate={to}")
// public String showDriversListOnRange(@ModelAttribute("driver") DriverDto driver, HttpServletRequest request, @RequestParam String from, @RequestParam String to, Model model) {
// log.info("Method showDriversListOnRange() started of class {}", getClass().getName());
//// Timestamp fromDateTimestamp = Timestamp.valueOf(fromDate);
//// Timestamp toDateTimestamp = Timestamp.valueOf(toDate);
//// Instant fromDateTime = Instant.parse(fromDate);
//// Instant toDateTime = Instant.parse(toDate);
// String fromDate = request.getParameter(from);
// String toDate = request.getParameter(to);
// model.addAttribute("driverList", driverDtoService.chooseDriverOnDateRange("1991-01-01T00:00:00.00Z", "2021-10-01T00:00:00.001Z"));
// return "drivers/drivers-range";
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Choose driver from date to date</title>
</head>
<body>
<h1>Form of choose driver from date to date</h1>
<div>
<form th:method="GET" th:action="@{/drivers/drivers-range}" th:object="${driver}"> <!--TODO -->
<!-- <input type="hidden" name="_method" value="GET"/>-->
<label for="fromDate">From </label>
<input type="datetime-local" th:field="*{fromDateChoose}" id="fromDate"/>
<br/>
<label for="toDate"> to </label>
<input type="datetime-local" th:field="*{toDateChoose}" id="toDate">
<br/>
<input type="submit" value="Choose!">
<br/>
<button><a th:href="@{/drivers}">Cancel</a></button>
</form>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Choose driver from date to date</title>
</head>
<body>
<h1>Form of choose driver from date to date</h1>
<div>
<form th:method="GET" th:action="@{/drivers/drivers-range?fromDate=from&toDate=to" th:object="${driver}"> <!--TODO -->
<!-- <input type="hidden" name="_method" value="POST"/>-->
<label for="fromDate">From </label>
<input type="datetime-local" value="from" name="fromDate" class="datepicker" id="fromDate"/>
<br/>
<label for="toDate"> to </label>
<input type="datetime-local" value="to" name="toDate" class="datepicker" id="toDate">
<br/>
<input type="submit" value="Choose!">
<br/>
<button><a th:href="@{/drivers}">Cancel</a></button>
</form>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Drivers from date to date</title>
</head>
<body>
<center>
<h1>Driver's list from date to date</h1>
<br/>
<div>
<table border="1">
<thead>
<tr>
<th scope="col">Unique driver's number</th>
<th scope="col">Driver name</th>
<th scope="col">Date when got job</th>
<th scope="col">Salary</th>
<th scope="col">Count cars assigned to the driver</th>
</tr>
</thead>
<tbody>
<tr th:each="driver : ${driverList}">
<td th:text="${driver.getDriverId()}"></td>
<td th:text="${driver.getDriverName()}"></td>
<td th:text="${driver.getDriverDateStartWork()}"></td>
<td th:text="${driver.getDriverSalary()}"></td>
<td th:text="${driver.getCountOfCarsAssignedToDriver()}"></td>
</tr>
</tbody>
</table>
</div>
</center>
</body>
<hr>
<a th:href="@{/drivers}">Back to menu of drivers</a>
</html>
Loading