diff --git a/src/main/java/com/example/sybd/Dto/ClientDto.java b/src/main/java/com/example/sybd/Dto/ClientDto.java new file mode 100644 index 0000000..a550611 --- /dev/null +++ b/src/main/java/com/example/sybd/Dto/ClientDto.java @@ -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 visits; +} diff --git a/src/main/java/com/example/sybd/Dto/IdNameDto.java b/src/main/java/com/example/sybd/Dto/IdNameDto.java new file mode 100644 index 0000000..61857d2 --- /dev/null +++ b/src/main/java/com/example/sybd/Dto/IdNameDto.java @@ -0,0 +1,9 @@ +package com.example.sybd.Dto; + +import lombok.Data; + +@Data +public class IdNameDto { + private Long id; + private String name; +} diff --git a/src/main/java/com/example/sybd/Dto/PostDto.java b/src/main/java/com/example/sybd/Dto/PostDto.java new file mode 100644 index 0000000..85fb236 --- /dev/null +++ b/src/main/java/com/example/sybd/Dto/PostDto.java @@ -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; +} diff --git a/src/main/java/com/example/sybd/Dto/ServiceDto.java b/src/main/java/com/example/sybd/Dto/ServiceDto.java new file mode 100644 index 0000000..da11294 --- /dev/null +++ b/src/main/java/com/example/sybd/Dto/ServiceDto.java @@ -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; +} diff --git a/src/main/java/com/example/sybd/Dto/ServiceGroupDto.java b/src/main/java/com/example/sybd/Dto/ServiceGroupDto.java new file mode 100644 index 0000000..a200dee --- /dev/null +++ b/src/main/java/com/example/sybd/Dto/ServiceGroupDto.java @@ -0,0 +1,9 @@ +package com.example.sybd.Dto; + +import lombok.Data; + +@Data +public class ServiceGroupDto { + private Long id; + private String name; +} diff --git a/src/main/java/com/example/sybd/Dto/VisitDto.java b/src/main/java/com/example/sybd/Dto/VisitDto.java new file mode 100644 index 0000000..29777b9 --- /dev/null +++ b/src/main/java/com/example/sybd/Dto/VisitDto.java @@ -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; +} diff --git a/src/main/java/com/example/sybd/Dto/WorkerDto.java b/src/main/java/com/example/sybd/Dto/WorkerDto.java new file mode 100644 index 0000000..c765ed4 --- /dev/null +++ b/src/main/java/com/example/sybd/Dto/WorkerDto.java @@ -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 visits; +} diff --git a/src/main/java/com/example/sybd/controllers/WorkerController.java b/src/main/java/com/example/sybd/controllers/WorkerController.java index 3a783a0..0ad5fee 100644 --- a/src/main/java/com/example/sybd/controllers/WorkerController.java +++ b/src/main/java/com/example/sybd/controllers/WorkerController.java @@ -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"; } } diff --git a/src/main/java/com/example/sybd/models/Service.java b/src/main/java/com/example/sybd/models/Service.java index 5a172fd..fdf9903 100644 --- a/src/main/java/com/example/sybd/models/Service.java +++ b/src/main/java/com/example/sybd/models/Service.java @@ -24,11 +24,11 @@ public class Service { @OneToMany( mappedBy = "service" ) private Collection visit; - public Collection getVisit() { - return visit; - } - public void setVisit(Collection visit) { this.visit = visit; } + + public String getInfo() { + return String.format("%s %d$", name, cost); + } } \ No newline at end of file diff --git a/src/main/java/com/example/sybd/models/Worker.java b/src/main/java/com/example/sybd/models/Worker.java index 0935c51..c04a9e7 100644 --- a/src/main/java/com/example/sybd/models/Worker.java +++ b/src/main/java/com/example/sybd/models/Worker.java @@ -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 visits; + + public String getFio() { + return MessageFormat.format("{0} {1} {2}", secondName, name, lastName); + } } \ No newline at end of file diff --git a/src/main/java/com/example/sybd/services/WorkerService.java b/src/main/java/com/example/sybd/services/WorkerService.java index cf5c987..2c2eb92 100644 --- a/src/main/java/com/example/sybd/services/WorkerService.java +++ b/src/main/java/com/example/sybd/services/WorkerService.java @@ -44,12 +44,12 @@ public class WorkerService implements IService { } @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; } diff --git a/src/main/java/com/example/sybd/utils/mappingUtils.java b/src/main/java/com/example/sybd/utils/mappingUtils.java index 0590874..2a3cbe8 100644 --- a/src/main/java/com/example/sybd/utils/mappingUtils.java +++ b/src/main/java/com/example/sybd/utils/mappingUtils.java @@ -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 List iterableToList(Iterable 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"))); + } } diff --git a/src/main/resources/templates/workerCreate.html b/src/main/resources/templates/workerCreate.html new file mode 100644 index 0000000..32f0c13 --- /dev/null +++ b/src/main/resources/templates/workerCreate.html @@ -0,0 +1,42 @@ + + + + Title + + + + +
+
+ + + +
+ + +
+
+ + +
+ +
+
+ + \ No newline at end of file