From 5e26c1aa1eaa8bd1e93694f0a5d8fc753d58f875 Mon Sep 17 00:00:00 2001 From: Zyzf Date: Sun, 7 May 2023 23:36:00 +0400 Subject: [PATCH] some done --- build.gradle | 3 +- .../cabinet/controller/CabinetController.java | 2 +- .../controller/CabinetMvcController.java | 79 +++++++++++++++++++ .../kalyshev/yan/cabinet/model/Cabinet.java | 2 +- .../yan/cabinet/service/CabinetService.java | 1 + .../controller/ComputerController.java | 2 +- .../yan/computer/controller/ComputerDto.java | 8 ++ .../controller/ComputerMvcController.java | 66 ++++++++++++++++ .../kalyshev/yan/computer/model/Computer.java | 2 +- .../yan/computer/service/ComputerService.java | 4 +- .../monitor/controller/MonitorController.java | 2 +- .../controller/MonitorMvcController.java | 62 +++++++++++++++ .../kalyshev/yan/monitor/model/Monitor.java | 8 +- .../yan/util/validation/ValidatorUtil.java | 6 +- src/main/resources/public/css/style.css | 15 ++++ .../resources/templates/cabinet-edit.html | 71 +++++++++++++++++ .../resources/templates/computer-edit.html | 38 +++++++++ src/main/resources/templates/computers.html | 57 +++++++++++++ src/main/resources/templates/default.html | 44 +++++++++++ src/main/resources/templates/error.html | 13 +++ src/main/resources/templates/index.html | 51 ++++++++++++ .../resources/templates/monitor-edit.html | 27 +++++++ src/main/resources/templates/monitors.html | 51 ++++++++++++ 23 files changed, 599 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/kalyshev/yan/cabinet/controller/CabinetMvcController.java create mode 100644 src/main/java/com/kalyshev/yan/computer/controller/ComputerMvcController.java create mode 100644 src/main/java/com/kalyshev/yan/monitor/controller/MonitorMvcController.java create mode 100644 src/main/resources/public/css/style.css create mode 100644 src/main/resources/templates/cabinet-edit.html create mode 100644 src/main/resources/templates/computer-edit.html create mode 100644 src/main/resources/templates/computers.html create mode 100644 src/main/resources/templates/default.html create mode 100644 src/main/resources/templates/error.html create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/monitor-edit.html create mode 100644 src/main/resources/templates/monitors.html diff --git a/build.gradle b/build.gradle index 9d06f08..50d768c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.0.2' + id 'org.springframework.boot' version '2.6.3' id 'io.spring.dependency-management' version '1.1.0' } @@ -28,6 +28,7 @@ dependencies { implementation 'org.hibernate.validator:hibernate-validator' implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' + implementation 'org.jetbrains:annotations:24.0.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetController.java b/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetController.java index bef385a..a41696f 100644 --- a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetController.java +++ b/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetController.java @@ -3,7 +3,7 @@ package com.kalyshev.yan.cabinet.controller; import com.kalyshev.yan.WebConfiguration; import com.kalyshev.yan.cabinet.service.CabinetService; import com.kalyshev.yan.computer.controller.ComputerDto; -import jakarta.validation.Valid; +import javax.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; diff --git a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetMvcController.java b/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetMvcController.java new file mode 100644 index 0000000..ae47a47 --- /dev/null +++ b/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetMvcController.java @@ -0,0 +1,79 @@ +package com.kalyshev.yan.cabinet.controller; + +import com.kalyshev.yan.cabinet.service.CabinetService; +import javax.validation.Valid; + +import com.kalyshev.yan.computer.controller.ComputerDto; +import com.kalyshev.yan.computer.service.ComputerService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +@Controller +@RequestMapping("/index") +public class CabinetMvcController { + private final CabinetService cabinetService; + private final ComputerService computerService; + public CabinetMvcController(CabinetService cabinetService, + ComputerService computerService) { + this.cabinetService = cabinetService; + this.computerService = computerService; + } + @GetMapping + public String getCabinets(Model model) { + model.addAttribute("cabinets", + cabinetService.findAllCabinets().stream() + .map(CabinetDto::new) + .toList()); + return "index"; + } + @GetMapping(value = {"/edit", "/edit/{id}"}) + public String editCabinet(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("cabinetDto", new CabinetDto()); + } else { + model.addAttribute("cabinetId", id); + model.addAttribute("cabinetDto", new CabinetDto(cabinetService.findCabinet(id))); + } + model.addAttribute("computers", + computerService.findFilteredComputers(null, null, null, null, id).stream() + .map(ComputerDto::new) + .toList()); + return "cabinet-edit"; + } + @PostMapping(value = {"", "/{id}"}) + public String saveCabinet(@PathVariable(required = false) Long id, + @ModelAttribute @Valid CabinetDto cabinetDto, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "cabinet-edit"; + } + if (id == null || id <= 0) { + cabinetService.addCabinet(cabinetDto.getNumber()); + } else { + cabinetService.updateCabinet(id, cabinetDto.getNumber()); + } + return "redirect:/index"; + } + @PostMapping(value = "/{id}/computer/{computerId}") + public String addCabinetComputer(@PathVariable(value = "id") Long id, + @PathVariable(value = "computerId") Long computerId) { + cabinetService.addComputerToCabinet(computerId, id); + return "redirect:/index"; + } + @PostMapping(value = "/{id}/computerDelete/{computerId}") + public String deleteCabinetComputer(@PathVariable(value = "id") Long id, + @PathVariable(value = "computerId") Long computerId) { + cabinetService.deleteComputerFromCabinet(computerId, id); + return "redirect:/index"; + } + @PostMapping("/delete/{id}") + public String deleteCabinet(@PathVariable Long id) { + cabinetService.deleteCabinet(id); + return "redirect:/index"; + } +} \ No newline at end of file diff --git a/src/main/java/com/kalyshev/yan/cabinet/model/Cabinet.java b/src/main/java/com/kalyshev/yan/cabinet/model/Cabinet.java index b6ebdd6..94a91d5 100644 --- a/src/main/java/com/kalyshev/yan/cabinet/model/Cabinet.java +++ b/src/main/java/com/kalyshev/yan/cabinet/model/Cabinet.java @@ -1,7 +1,7 @@ package com.kalyshev.yan.cabinet.model; import com.kalyshev.yan.computer.model.Computer; -import jakarta.persistence.*; +import javax.persistence.*; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/kalyshev/yan/cabinet/service/CabinetService.java b/src/main/java/com/kalyshev/yan/cabinet/service/CabinetService.java index 8ea00f4..ba77a67 100644 --- a/src/main/java/com/kalyshev/yan/cabinet/service/CabinetService.java +++ b/src/main/java/com/kalyshev/yan/cabinet/service/CabinetService.java @@ -100,5 +100,6 @@ public class CabinetService { final Computer computer = computerService.findComputer(computerId); final Cabinet cabinet = findCabinet(cabinetId); cabinet.removeComputer(computer); + computer.setCabinet(null); } } \ No newline at end of file diff --git a/src/main/java/com/kalyshev/yan/computer/controller/ComputerController.java b/src/main/java/com/kalyshev/yan/computer/controller/ComputerController.java index 0254acb..6071a01 100644 --- a/src/main/java/com/kalyshev/yan/computer/controller/ComputerController.java +++ b/src/main/java/com/kalyshev/yan/computer/controller/ComputerController.java @@ -3,7 +3,7 @@ package com.kalyshev.yan.computer.controller; import com.kalyshev.yan.WebConfiguration; import com.kalyshev.yan.computer.service.ComputerService; import com.kalyshev.yan.monitor.controller.MonitorDto; -import jakarta.validation.Valid; +import javax.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; diff --git a/src/main/java/com/kalyshev/yan/computer/controller/ComputerDto.java b/src/main/java/com/kalyshev/yan/computer/controller/ComputerDto.java index bc6f794..ee35de1 100644 --- a/src/main/java/com/kalyshev/yan/computer/controller/ComputerDto.java +++ b/src/main/java/com/kalyshev/yan/computer/controller/ComputerDto.java @@ -7,7 +7,9 @@ public class ComputerDto { private String modelName; private String serialNum; private Long monitorId; + private String monitorModelName; private Long cabinetId; + private String cabinetNumber; public ComputerDto() {} public ComputerDto(Computer computer) { this.id = computer.getId(); @@ -15,13 +17,17 @@ public class ComputerDto { this.serialNum = computer.getSerialNum(); if (computer.getMonitor() == null) { this.monitorId = null; + this.monitorModelName = ""; } else { this.monitorId = computer.getMonitor().getId(); + this.monitorModelName = computer.getMonitor().getModelName(); } if (computer.getCabinet() == null) { this.cabinetId = null; + this.cabinetNumber = ""; } else { this.cabinetId = computer.getCabinet().getId(); + this.cabinetNumber = computer.getCabinet().getNumber(); } } public Long getId() { return this.id; } @@ -33,4 +39,6 @@ public class ComputerDto { public void setMonitorId(Long monitorId) { this.monitorId = monitorId; } public Long getCabinetId() { return this.cabinetId; } public void setCabinetId(Long cabinetId) { this.cabinetId = cabinetId; } + public String getMonitorModelName() { return this.monitorModelName; } + public String getCabinetNumber() { return this.cabinetNumber; } } diff --git a/src/main/java/com/kalyshev/yan/computer/controller/ComputerMvcController.java b/src/main/java/com/kalyshev/yan/computer/controller/ComputerMvcController.java new file mode 100644 index 0000000..4b27754 --- /dev/null +++ b/src/main/java/com/kalyshev/yan/computer/controller/ComputerMvcController.java @@ -0,0 +1,66 @@ +package com.kalyshev.yan.computer.controller; + +import com.kalyshev.yan.cabinet.controller.CabinetDto; +import com.kalyshev.yan.cabinet.service.CabinetService; +import com.kalyshev.yan.computer.service.ComputerService; +import com.kalyshev.yan.monitor.service.MonitorService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +@Controller +@RequestMapping("/computers") +public class ComputerMvcController { + private final ComputerService computerService; + private final MonitorService monitorService; + public ComputerMvcController(ComputerService computerService, + MonitorService monitorService) { + this.computerService = computerService; + this.monitorService = monitorService; + } + @GetMapping + public String getComputers(Model model) { + model.addAttribute("computers", + computerService.findAllComputers().stream() + .map(ComputerDto::new) + .toList()); + return "computers"; + } + @GetMapping(value = {"/edit", "/edit/{id}"}) + public String editComputer(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("computerDto", new ComputerDto()); + } else { + model.addAttribute("computerId", id); + model.addAttribute("computerDto", new ComputerDto(computerService.findComputer(id))); + } + model.addAttribute("monitors", + monitorService.findAllMonitors()); + return "computer-edit"; + } + @PostMapping(value = {"", "/{id}"}) + public String saveComputer(@PathVariable(required = false) Long id, + @ModelAttribute @Valid ComputerDto computerDto, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "computer-edit"; + } + if (id == null || id <= 0) { + computerService.addComputer(computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId()); + } else { + computerService.updateComputer(id, computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId(), computerDto.getCabinetId()); + } + return "redirect:/computers"; + } + @PostMapping("/delete/{id}") + public String deleteComputer(@PathVariable Long id) { + computerService.deleteComputer(id); + return "redirect:/computers"; + } +} \ No newline at end of file diff --git a/src/main/java/com/kalyshev/yan/computer/model/Computer.java b/src/main/java/com/kalyshev/yan/computer/model/Computer.java index 2cdf669..289d37a 100644 --- a/src/main/java/com/kalyshev/yan/computer/model/Computer.java +++ b/src/main/java/com/kalyshev/yan/computer/model/Computer.java @@ -2,8 +2,8 @@ package com.kalyshev.yan.computer.model; import com.kalyshev.yan.cabinet.model.Cabinet; import com.kalyshev.yan.monitor.model.Monitor; -import jakarta.persistence.*; +import javax.persistence.*; import java.util.Objects; @Entity diff --git a/src/main/java/com/kalyshev/yan/computer/service/ComputerService.java b/src/main/java/com/kalyshev/yan/computer/service/ComputerService.java index af1b1a1..5615733 100644 --- a/src/main/java/com/kalyshev/yan/computer/service/ComputerService.java +++ b/src/main/java/com/kalyshev/yan/computer/service/ComputerService.java @@ -7,7 +7,7 @@ import com.kalyshev.yan.computer.repository.ComputerRepository; import com.kalyshev.yan.monitor.model.Monitor; import com.kalyshev.yan.monitor.service.MonitorService; import com.kalyshev.yan.util.validation.ValidatorUtil; -import jakarta.annotation.Nullable; +import javax.validation.constraints.Null; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; @@ -27,7 +27,7 @@ public class ComputerService { this.validatorUtil = validatorUtil; } @Transactional - public Computer addComputer(String modelName, String serialNum, @Nullable Long monitorId) { + public Computer addComputer(String modelName, String serialNum, @Null Long monitorId) { if (!StringUtils.hasText(modelName)) { throw new IllegalArgumentException("Computer model name is null or empty"); } diff --git a/src/main/java/com/kalyshev/yan/monitor/controller/MonitorController.java b/src/main/java/com/kalyshev/yan/monitor/controller/MonitorController.java index e8b58d3..70ffc89 100644 --- a/src/main/java/com/kalyshev/yan/monitor/controller/MonitorController.java +++ b/src/main/java/com/kalyshev/yan/monitor/controller/MonitorController.java @@ -3,7 +3,7 @@ package com.kalyshev.yan.monitor.controller; import com.kalyshev.yan.WebConfiguration; import com.kalyshev.yan.computer.service.ComputerService; import com.kalyshev.yan.monitor.service.MonitorService; -import jakarta.validation.Valid; +import javax.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; diff --git a/src/main/java/com/kalyshev/yan/monitor/controller/MonitorMvcController.java b/src/main/java/com/kalyshev/yan/monitor/controller/MonitorMvcController.java new file mode 100644 index 0000000..af113c1 --- /dev/null +++ b/src/main/java/com/kalyshev/yan/monitor/controller/MonitorMvcController.java @@ -0,0 +1,62 @@ +package com.kalyshev.yan.monitor.controller; + +import com.kalyshev.yan.cabinet.controller.CabinetDto; +import com.kalyshev.yan.cabinet.service.CabinetService; +import com.kalyshev.yan.computer.service.ComputerService; +import com.kalyshev.yan.monitor.model.Monitor; +import com.kalyshev.yan.monitor.service.MonitorService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +@Controller +@RequestMapping("/monitors") +public class MonitorMvcController { + private final MonitorService monitorService; + public MonitorMvcController(MonitorService monitorService) { + this.monitorService = monitorService; + } + @GetMapping + public String getMonitors(Model model) { + model.addAttribute("monitors", + monitorService.findAllMonitors().stream() + .map(MonitorDto::new) + .toList()); + return "monitors"; + } + @GetMapping(value = {"/edit", "/edit/{id}"}) + public String editMonitor(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("monitorDto", new MonitorDto()); + } else { + model.addAttribute("monitorId", id); + model.addAttribute("monitorDto", new MonitorDto(monitorService.findMonitor(id))); + } + return "monitor-edit"; + } + @PostMapping(value = {"", "/{id}"}) + public String saveComputer(@PathVariable(required = false) Long id, + @ModelAttribute @Valid MonitorDto monitorDto, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "monitor-edit"; + } + if (id == null || id <= 0) { + monitorService.addMonitor(monitorDto.getModelName()); + } else { + monitorService.updateMonitor(id, monitorDto.getModelName()); + } + return "redirect:/monitors"; + } + @PostMapping("/delete/{id}") + public String deleteComputer(@PathVariable Long id) { + monitorService.deleteMonitor(monitorService.findMonitor(id)); + return "redirect:/monitors"; + } +} \ No newline at end of file diff --git a/src/main/java/com/kalyshev/yan/monitor/model/Monitor.java b/src/main/java/com/kalyshev/yan/monitor/model/Monitor.java index 3fb247b..ff4e4cf 100644 --- a/src/main/java/com/kalyshev/yan/monitor/model/Monitor.java +++ b/src/main/java/com/kalyshev/yan/monitor/model/Monitor.java @@ -1,9 +1,9 @@ package com.kalyshev.yan.monitor.model; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.Table; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; import java.util.Objects; diff --git a/src/main/java/com/kalyshev/yan/util/validation/ValidatorUtil.java b/src/main/java/com/kalyshev/yan/util/validation/ValidatorUtil.java index d46a447..7230b78 100644 --- a/src/main/java/com/kalyshev/yan/util/validation/ValidatorUtil.java +++ b/src/main/java/com/kalyshev/yan/util/validation/ValidatorUtil.java @@ -1,8 +1,8 @@ package com.kalyshev.yan.util.validation; -import jakarta.validation.ConstraintViolation; -import jakarta.validation.Validation; -import jakarta.validation.Validator; +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; import org.springframework.stereotype.Component; import java.util.Set; diff --git a/src/main/resources/public/css/style.css b/src/main/resources/public/css/style.css new file mode 100644 index 0000000..a68a220 --- /dev/null +++ b/src/main/resources/public/css/style.css @@ -0,0 +1,15 @@ +.container-padding { + padding: 10px; +} + +.margin-bottom { + margin-bottom: 10px; +} + +.button-fixed { + min-width: 120px; +} + +.button-sm { + padding: 1px; +} \ No newline at end of file diff --git a/src/main/resources/templates/cabinet-edit.html b/src/main/resources/templates/cabinet-edit.html new file mode 100644 index 0000000..5521e00 --- /dev/null +++ b/src/main/resources/templates/cabinet-edit.html @@ -0,0 +1,71 @@ + + + + + +
+
+
+
+ + +
+
+ + + + + + + + + + + + + + + + +
#НазваниеСерийный номерМониторКабинет
+ + + + +
+ +
+ + + +
+
+ + + Назад + +
+ +
+
+ + + +
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/computer-edit.html b/src/main/resources/templates/computer-edit.html new file mode 100644 index 0000000..4432e70 --- /dev/null +++ b/src/main/resources/templates/computer-edit.html @@ -0,0 +1,38 @@ + + + + + +
+
+
+
+ + +
+
+ + +
+ + +
+ + + Назад + +
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/computers.html b/src/main/resources/templates/computers.html new file mode 100644 index 0000000..0111352 --- /dev/null +++ b/src/main/resources/templates/computers.html @@ -0,0 +1,57 @@ + + + + + +
+
+ + Добавить + +
+
+ + + + + + + + + + + + + + + + +
#НазваниеСерийный номерМониторКабинет
+ + + + + +
+ + Изменить + + +
+
+ +
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..d35989a --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,44 @@ + + + + + MVC App + + + + + + + +
+
+
+
+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..78fb62d --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,13 @@ + + + + + +
+
+ На главную +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..d52de28 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,51 @@ + + + + + +
+
+ + Добавить + +
+
+ + + + + + + + + + + + + +
#Номер кабинета
+ + +
+ + Изменить + + +
+
+ +
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/monitor-edit.html b/src/main/resources/templates/monitor-edit.html new file mode 100644 index 0000000..27bdb94 --- /dev/null +++ b/src/main/resources/templates/monitor-edit.html @@ -0,0 +1,27 @@ + + + + + +
+
+
+
+ + +
+
+ + + Назад + +
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/monitors.html b/src/main/resources/templates/monitors.html new file mode 100644 index 0000000..a7815b4 --- /dev/null +++ b/src/main/resources/templates/monitors.html @@ -0,0 +1,51 @@ + + + + + +
+
+ + Добавить + +
+
+ + + + + + + + + + + + + +
#Название
+ + +
+ + Изменить + + +
+
+ +
+
+
+
+ + \ No newline at end of file