контроллеры

This commit is contained in:
parap 2023-05-22 03:43:31 +04:00
parent d8b4905639
commit 856b38ace7
4 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package org.dbms.controllers;
import org.dbms.dto.ClientLoginDto;
import org.dbms.dto.ClientSignupDto;
import org.dbms.dto.DriverDTO;
import org.dbms.exceptions.UserExistsException;
import org.dbms.searchModels.DriverSearch;
import org.dbms.service.ClientService;
import org.dbms.storageImpl.ClientStorage;
import org.dbms.storageImpl.DriverStorage;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
@RestController
public class ClientController {
private final ClientService clientService;
public ClientController(ClientService clientService) {
this.clientService = clientService;
}
@PostMapping("/client")
public String createUser(@RequestBody @Valid ClientSignupDto client, HttpServletResponse response) {
String res = "";
try {
res = clientService.signup(client);
} catch(UserExistsException err) {
response.setStatus(403);
return "Пользователь уже существует";
}
return res;
}
@PostMapping("/login")
public String login(@RequestBody @Valid ClientLoginDto client, HttpServletResponse response) {
if(clientService.login(client)) {
return "success";
} else {
response.setStatus(403);
return "Неверный логин или пароль";
}
}
}
;

View File

@ -0,0 +1,50 @@
package org.dbms.controllers;
import org.dbms.data.DataGenerator;
import org.dbms.data.DataPerform;
import org.dbms.dto.DriverDTO;
import org.dbms.searchModels.DriverSearch;
import org.dbms.service.DriverService;
import org.dbms.storageImpl.DriverStorage;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
public class DataController {
private DataGenerator dataGenerator;
private DataPerform dataPerform;
public DataController(DataGenerator dataGenerator, DataPerform dataPerform) {
this.dataGenerator = dataGenerator;
this.dataPerform = dataPerform;
}
@GetMapping("/generate-data")
public String getDriversCount() {
dataGenerator.generate();
return "success";
}
@GetMapping("/test-select")
public String testSelect() {
return dataPerform.select() + "";
}
@GetMapping("/test-insert")
public String testInsert() {
return dataPerform.insert(1000) + "";
}
@GetMapping("/test-update")
public String testUpdate() {
return dataPerform.update(1000) + "";
}
@GetMapping("/test-delete")
public String testDelete() {
return dataPerform.delete() + "";
}
}

View File

@ -0,0 +1,36 @@
package org.dbms.controllers;
import org.dbms.dto.DriverDTO;
import org.dbms.searchModels.DriverSearch;
import org.dbms.service.DriverService;
import org.dbms.storageImpl.DriverStorage;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
public class DriverController {
private final DriverService driverService;
public DriverController(DriverService driverService) {
this.driverService = driverService;
}
@GetMapping("/drivers")
public List<DriverDTO> getDrivers(@RequestParam(defaultValue = "-1") int expFrom,
@RequestParam(defaultValue = "-1") int expTo,
@RequestParam(defaultValue = "-1") int size,
@RequestParam(defaultValue = "-1") int page) {
return driverService.getFilteredPage(expFrom, expTo, size, page).stream().map(DriverDTO::new).toList();
}
@GetMapping("/drivers/pages-count")
public int getDriversCount(@RequestParam(defaultValue = "-1") int expFrom,
@RequestParam(defaultValue = "-1") int expTo,
@RequestParam(defaultValue = "-1") int size) {
return driverService.getFilteredCount(expFrom, expTo, size);
}
}

View File

@ -0,0 +1,41 @@
package org.dbms.controllers;
import org.dbms.dto.ClientLoginDto;
import org.dbms.dto.ClientSignupDto;
import org.dbms.dto.CreateOrderDTO;
import org.dbms.dto.OrderDTO;
import org.dbms.exceptions.UserExistsException;
import org.dbms.service.ClientService;
import org.dbms.service.OrderService;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
@RestController
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
@GetMapping("/orders")
public List<OrderDTO> getOrders(@RequestParam String login, @RequestParam int size, @RequestParam int page) {
return orderService.getOrdersPageByClient(login, size, page).stream().map(OrderDTO::new).toList();
}
@PostMapping("/orders")
public String makeOrder(@RequestBody @Valid CreateOrderDTO order) {
orderService.createOrder(order);
return "success";
}
@GetMapping("/orders/pages-count")
public int getPagesCount(@RequestParam String login, @RequestParam int size) {
return orderService.getPagesCount(login, size);
}
}
;