Create dto and Service

This commit is contained in:
Viltskaa 2023-05-05 18:43:43 +04:00
parent 1fc6b0f67c
commit 459d16bfd7
13 changed files with 316 additions and 10 deletions

View File

@ -0,0 +1,16 @@
package com.example.sybd.Dto;
import lombok.Data;
import java.util.List;
@Data
public class ClientDto {
private Long id;
private String name;
private String lastName;
private String secondName;
private String telephone;
private String address;
private List<Long> visits;
}

View File

@ -0,0 +1,9 @@
package com.example.sybd.Dto;
import lombok.Data;
@Data
public class IdNameDto {
private Long id;
private String name;
}

View File

@ -0,0 +1,10 @@
package com.example.sybd.Dto;
import lombok.Data;
@Data
public class PostDto {
private Long id;
private String name;
private IdNameDto group;
}

View File

@ -0,0 +1,11 @@
package com.example.sybd.Dto;
import lombok.Data;
@Data
public class ServiceDto {
private Long id;
private String name;
private IdNameDto group;
private Integer cost;
}

View File

@ -0,0 +1,9 @@
package com.example.sybd.Dto;
import lombok.Data;
@Data
public class ServiceGroupDto {
private Long id;
private String name;
}

View File

@ -0,0 +1,17 @@
package com.example.sybd.Dto;
import lombok.Data;
import java.sql.Date;
import java.sql.Time;
@Data
public class VisitDto {
private Long id;
private IdNameDto client;
private IdNameDto worker;
private ServiceDto service;
private Date date;
private Time time;
private boolean isEnded;
}

View File

@ -0,0 +1,17 @@
package com.example.sybd.Dto;
import lombok.Data;
import java.util.List;
@Data
public class WorkerDto {
private Long id;
private String name;
private String lastName;
private String secondName;
private String telephone;
private String address;
private PostDto post;
private List<Long> visits;
}

View File

@ -1,27 +1,66 @@
package com.example.sybd.controllers;
import com.example.sybd.models.Visit;
import com.example.sybd.services.ClientService;
import com.example.sybd.services.ServicesService;
import com.example.sybd.services.VisitService;
import com.example.sybd.services.WorkerService;
import com.example.sybd.utils.mappingUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/worker")
public class WorkerController {
private final VisitService visitService;
private final WorkerService workerService;
private final ClientService clientService;
private final ServicesService servicesService;
public WorkerController(VisitService visitService, WorkerService workerService) {
public WorkerController(VisitService visitService,
WorkerService workerService,
ClientService clientService,
ServicesService servicesService) {
this.visitService = visitService;
this.workerService = workerService;
this.clientService = clientService;
this.servicesService = servicesService;
}
@GetMapping("/")
public String main(Model model) {
model.addAttribute("visits", visitService.all());
model.addAttribute("workers", workerService.all());
model.addAttribute("create", "worker/create");
return "workerMain";
}
@GetMapping("/create")
public String create(Model model) {
model.addAttribute("clients", clientService.all());
model.addAttribute("workers", workerService.all());
model.addAttribute("services", servicesService.all());
return "workerCreate";
}
@PostMapping("/create")
public String createSubmit(Long worker,
Long client,
Long service,
String date,
String time,
Model model) {
model.addAttribute("visits", visitService.all());
model.addAttribute("workers", workerService.all());
model.addAttribute("services", servicesService.all());
Visit visit = visitService.create(
client,
service,
worker,
mappingUtils.strToDate(date),
mappingUtils.strToTime(time)
);
return "workerMain";
}
}

View File

@ -24,11 +24,11 @@ public class Service {
@OneToMany( mappedBy = "service" )
private Collection<Visit> visit;
public Collection<Visit> getVisit() {
return visit;
}
public void setVisit(Collection<Visit> visit) {
this.visit = visit;
}
public String getInfo() {
return String.format("%s %d$", name, cost);
}
}

View File

@ -4,6 +4,7 @@ import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.text.MessageFormat;
import java.util.Collection;
@Getter
@ -28,4 +29,8 @@ public class Worker {
@OneToMany( mappedBy = "worker" )
private Collection<Visit> visits;
public String getFio() {
return MessageFormat.format("{0} {1} {2}", secondName, name, lastName);
}
}

View File

@ -44,12 +44,12 @@ public class WorkerService implements IService<Worker, Long> {
}
@Override
public @Nullable Worker get(Long aLong) {
return null;
public @Nullable Worker get(Long id) {
return workerRepository.findById(id).orElse(null);
}
@Override
public @Nullable Worker delete(Long aLong) {
public @Nullable Worker delete(Long id) {
return null;
}

View File

@ -1,5 +1,14 @@
package com.example.sybd.utils;
import com.example.sybd.Dto.*;
import com.example.sybd.models.*;
import org.jetbrains.annotations.NotNull;
import java.sql.Date;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.StreamSupport;
@ -7,4 +16,126 @@ public class mappingUtils {
public static <T> List<T> iterableToList(Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false).toList();
}
public static ClientDto clientDto(Client client) {
ClientDto clientDto = new ClientDto();
clientDto.setName(client.getName());
clientDto.setId(client.getId());
clientDto.setAddress(client.getAddress());
clientDto.setSecondName(client.getSecondName());
clientDto.setLastName(client.getLastName());
clientDto.setTelephone(client.getTelephone());
clientDto.setVisits(client.getVisits().stream().map(Visit::getId).toList());
return clientDto;
}
public static IdNameDto idNameDto(Long id, String name) {
IdNameDto idNameDto = new IdNameDto();
idNameDto.setId(id);
idNameDto.setName(name);
return idNameDto;
}
public static PostDto postDto(Post post) {
PostDto postDto = new PostDto();
postDto.setId(post.getId());
postDto.setName(post.getName());
postDto.setGroup(
idNameDto(post.getGroup().getId(), post.getGroup().getName())
);
return postDto;
}
public static ServiceDto serviceDto(Service service) {
ServiceDto serviceDto = new ServiceDto();
serviceDto.setCost(service.getCost());
serviceDto.setId(service.getId());
serviceDto.setName(service.getName());
serviceDto.setGroup(
idNameDto(service.getGroup().getId(), service.getGroup().getName())
);
return serviceDto;
}
public static ServiceGroupDto serviceGroupDto(ServiceGroup serviceGroup) {
ServiceGroupDto serviceGroupDto = new ServiceGroupDto();
serviceGroupDto.setId(serviceGroup.getId());
serviceGroupDto.setName(serviceGroup.getName());
return serviceGroupDto;
}
public static VisitDto visitDto(Visit visit) {
VisitDto visitDto = new VisitDto();
visitDto.setId(visit.getId());
visitDto.setDate(visit.getDateVisit());
visitDto.setTime(visit.getTime());
visitDto.setService(serviceDto(visit.getService()));
visitDto.setEnded(visit.isEnded());
visitDto.setClient(
idNameDto(visit.getClient().getId(), visit.getClient().getFio())
);
visitDto.setWorker(
idNameDto(visit.getWorker().getId(), visit.getWorker().getFio())
);
return visitDto;
}
public static WorkerDto workerDto(Worker worker) {
WorkerDto workerDto = new WorkerDto();
workerDto.setName(worker.getName());
workerDto.setPost(postDto(worker.getPost()));
workerDto.setId(worker.getId());
workerDto.setAddress(worker.getAddress());
workerDto.setSecondName(worker.getSecondName());
workerDto.setLastName(worker.getLastName());
workerDto.setTelephone(worker.getTelephone());
workerDto.setVisits(worker.getVisits().stream().map(Visit::getId).toList());
return workerDto;
}
public static Client client(ClientDto clientDto) {
Client client = new Client();
client.setName(clientDto.getName());
client.setId(clientDto.getId());
client.setAddress(clientDto.getAddress());
client.setSecondName(clientDto.getSecondName());
client.setLastName(clientDto.getLastName());
client.setTelephone(clientDto.getTelephone());
return client;
}
public static Post post(PostDto postDto) {
Post post = new Post();
post.setId(postDto.getId());
post.setName(postDto.getName());
return post;
}
public static Service service(ServiceDto serviceDto) {
Service service = new Service();
service.setCost(serviceDto.getCost());
service.setId(serviceDto.getId());
service.setName(serviceDto.getName());
return service;
}
public static ServiceGroup serviceGroup(ServiceGroupDto serviceGroupDto) {
ServiceGroup serviceGroup = new ServiceGroup();
serviceGroup.setId(serviceGroupDto.getId());
serviceGroup.setName(serviceGroupDto.getName());
return serviceGroup;
}
public static Visit visit(VisitDto visitDto) {
Visit visit = new Visit();
visit.setId(visitDto.getId());
visit.setDateVisit(visitDto.getDate());
visit.setTime(visitDto.getTime());
visit.setEnded(visit.isEnded());
return visit;
}
public static Worker worker(WorkerDto workerDto) {
Worker worker = new Worker();
worker.setName(workerDto.getName());
worker.setId(workerDto.getId());
worker.setAddress(workerDto.getAddress());
worker.setSecondName(workerDto.getSecondName());
worker.setLastName(workerDto.getLastName());
worker.setTelephone(workerDto.getTelephone());
return worker;
}
public static Date strToDate(@NotNull String date) {
return Date.valueOf(LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy")));
}
public static Time strToTime(@NotNull String time) {
return Time.valueOf(LocalTime.parse(time, DateTimeFormatter.ofPattern("H-mm")));
}
}

View File

@ -0,0 +1,42 @@
<html xmlns:th="https://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous">
</head>
<body>
<nav class="nav nav-pills flex-row p-2">
<a class="flex-fill text-sm-center nav-link" aria-current="page" href="/new">Новый сотрудник</a>
<a class="flex-fill text-sm-center nav-link" href="/visit">Создать визит</a>
<a class="flex-fill text-sm-center nav-link" href="/services">Услуги</a>
<select class="form-select form-select-sm w-25" aria-label="Сотдрудник">
<option th:each="worker : ${workers}" th:value="${worker.getId()}" th:text="${worker.getName()}"></option>
</select>
</nav>
<main class="mt-5 p-2">
<form action="#" th:action="@{/worker/create}" method="post">
<select class="form-select form-select-sm w-25" aria-label="Сотдрудник" th:name="worker">
<option th:each="worker : ${workers}" th:value="${worker.getId()}" th:text="${worker.getName()}"></option>
</select>
<select class="form-select form-select-sm w-25" aria-label="Клиент" th:name="client">
<option th:each="client : ${clients}" th:value="${client.getId()}" th:text="${client.getName()}"></option>
</select>
<select class="form-select form-select-sm w-25" aria-label="Услуга" th:name="service">
<option th:each="service : ${services}" th:value="${service.getId()}" th:text="${service.getInfo()}"></option>
</select>
<div class="mb-3">
<label for="date" class="form-label">Дата</label>
<input type="text" class="form-control" id="date" placeholder="День Месяц Год" th:name="date">
</div>
<div class="mb-3">
<label for="time" class="form-label">Время</label>
<input type="text" class="form-control" id="time" placeholder="17:00" th:name="time">
</div>
<button type="submit" class="btn btn-primary mb-3">Создать</button>
</form>
</main>
</body>
</html>