diff --git a/src/main/java/com/kalyshev/yan/SecurityConfiguration.java b/src/main/java/com/kalyshev/yan/SecurityConfiguration.java index fa727e7..52a970a 100644 --- a/src/main/java/com/kalyshev/yan/SecurityConfiguration.java +++ b/src/main/java/com/kalyshev/yan/SecurityConfiguration.java @@ -51,7 +51,7 @@ public class SecurityConfiguration { .logout().permitAll() .and() .exceptionHandling().accessDeniedPage("/login"); - http.formLogin().defaultSuccessUrl("/index", true); + http.formLogin().defaultSuccessUrl("/categories", true); return http.userDetailsService(userService).build(); } diff --git a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetController.java b/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetController.java deleted file mode 100644 index 7ab6b41..0000000 --- a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetController.java +++ /dev/null @@ -1,65 +0,0 @@ -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 org.springframework.web.bind.annotation.*; - -import java.util.List; - -@RestController -@RequestMapping(WebConfiguration.REST_API + "/cabinet") -public class CabinetController { - private final CabinetService cabinetService; - public CabinetController(CabinetService cabinetService) { - this.cabinetService = cabinetService; - } - @GetMapping("/{id}") - public CabinetDto getCabinet(@PathVariable Long id) { - return new CabinetDto(cabinetService.findCabinet(id)); - } - @GetMapping("/") - public List getCabinets() { - return cabinetService.findAllCabinets().stream() - .map(CabinetDto::new) - .toList(); - } - @GetMapping("/{id}/computers") - public List getCabinetComputers(@PathVariable Long id) { - return cabinetService.listComputersFromCabinet(id).stream() - .map(ComputerDto::new) - .toList(); - } - @GetMapping("/filter") - public List getFilteredCabinets(@RequestParam(value = "id", required = false) Long id, - @RequestParam(value = "number", required = false) String number) { - return cabinetService.findFilteredCabinets(id, number).stream() - .map(CabinetDto::new) - .toList(); - } - @PostMapping("/") - public CabinetDto createCabinet(@RequestBody @Valid CabinetDto cabinetDto) { - return new CabinetDto(cabinetService.addCabinet(cabinetDto.getNumber())); - } - @PostMapping("/{id}/computer") - public void createCabinetComputer(@PathVariable Long id, - @RequestParam("computerId") Long computerId) { - cabinetService.addComputerToCabinet(computerId, id); - } - @PutMapping("/{id}") - public CabinetDto updateCabinet(@PathVariable Long id, - @RequestBody @Valid CabinetDto cabinetDto) { - return new CabinetDto(cabinetService.updateCabinet(id, cabinetDto.getNumber())); - } - @DeleteMapping("/{id}") - public CabinetDto deleteCabinet(@PathVariable Long id) { - return new CabinetDto(cabinetService.deleteCabinet(id)); - } - @DeleteMapping("/{cabinetId}/computer") - public void deleteCabinetComputer(@PathVariable Long cabinetId, - @RequestParam("computerId") Long computerId) { - cabinetService.deleteComputerFromCabinet(computerId, cabinetId); - } -} \ No newline at end of file diff --git a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetDto.java b/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetDto.java deleted file mode 100644 index b996261..0000000 --- a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetDto.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.kalyshev.yan.cabinet.controller; - -import com.kalyshev.yan.cabinet.model.Cabinet; -import com.kalyshev.yan.computer.model.Computer; - -import java.util.ArrayList; -import java.util.List; - -public class CabinetDto { - private Long id; - private String number; - private List computerIds; - public CabinetDto() {} - public CabinetDto(Cabinet cabinet) { - this.id = cabinet.getId(); - this.number = cabinet.getNumber(); - if (cabinet.getComputers() == null) { - this.computerIds = new ArrayList<>(); - } else { - this.computerIds = new ArrayList<>(); - List computers = cabinet.getComputers(); - for (Computer computer : computers) { - computerIds.add(computer.getId()); - } - } - } - public Long getId() { return this.id; } - public void setId(Long id) { this.id = id; } - public String getNumber() { return this.number; } - public void setNumber(String number) { this.number = number; } - public List getComputerIds() { return this.computerIds; } - public void setComputerIds(List computerIds) { this.computerIds = computerIds; } -} diff --git a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetMvcController.java b/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetMvcController.java deleted file mode 100644 index 11eeddb..0000000 --- a/src/main/java/com/kalyshev/yan/cabinet/controller/CabinetMvcController.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.kalyshev.yan.cabinet.controller; - -import com.kalyshev.yan.cabinet.service.CabinetService; -import jakarta.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()); - model.addAttribute("allComputers", - computerService.findAllComputers().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 deleted file mode 100644 index 912fc87..0000000 --- a/src/main/java/com/kalyshev/yan/cabinet/model/Cabinet.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.kalyshev.yan.cabinet.model; - -import com.kalyshev.yan.computer.model.Computer; -import jakarta.persistence.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -@Entity -@Table(name = "cabinet") -public class Cabinet { - @Id - @GeneratedValue - private Long id; - private String number; - @OneToMany(cascade = {CascadeType.MERGE}) - private List computers; - - public Cabinet() { - } - public Cabinet(String number) { - this.number = number; - } - public Long getId() { - return id; - } - public String getNumber() { - return number; - } - public void setNumber(String number) { - this.number = number; - } - public List getComputers() { - return computers; - } - public void addComputer(Computer computer){ - if (computers == null){ - this.computers = new ArrayList<>(); - } - if (!computers.contains(computer)) { - this.computers.add(computer); - computer.setCabinet(this); - } - } - public void removeComputer(Computer computer){ - if (computers.contains(computer)) - this.computers.remove(computer); - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (!(o instanceof Cabinet cabinet)) - return false; - return Objects.equals(id, cabinet.id); - } - @Override - public int hashCode() { - return Objects.hash(id); - } - @Override - public String toString() { - return "Cabinet{" + - "id=" + id + - ", number='" + number + '\'' + - '}'; - } -} diff --git a/src/main/java/com/kalyshev/yan/cabinet/repository/CabinetNotFoundException.java b/src/main/java/com/kalyshev/yan/cabinet/repository/CabinetNotFoundException.java deleted file mode 100644 index 519e713..0000000 --- a/src/main/java/com/kalyshev/yan/cabinet/repository/CabinetNotFoundException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.kalyshev.yan.cabinet.repository; - -public class CabinetNotFoundException extends RuntimeException { - public CabinetNotFoundException(Long id) { - super(String.format("Cabinet with id [%s] is not found", id)); - } -} diff --git a/src/main/java/com/kalyshev/yan/cabinet/repository/CabinetRepository.java b/src/main/java/com/kalyshev/yan/cabinet/repository/CabinetRepository.java deleted file mode 100644 index 1edf33f..0000000 --- a/src/main/java/com/kalyshev/yan/cabinet/repository/CabinetRepository.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.kalyshev.yan.cabinet.repository; - -import com.kalyshev.yan.cabinet.model.Cabinet; -import com.kalyshev.yan.monitor.model.Monitor; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; - -import java.util.List; - -public interface CabinetRepository extends JpaRepository { - @Query(value = "select s from Cabinet s where (s.id = :id or :id is Null) and (s.number = :number or :number is Null)") - public List findFilteredCabinets(@Param("id") Long id, - @Param("number") String number); -} \ No newline at end of file diff --git a/src/main/java/com/kalyshev/yan/cabinet/service/CabinetService.java b/src/main/java/com/kalyshev/yan/cabinet/service/CabinetService.java deleted file mode 100644 index ba77a67..0000000 --- a/src/main/java/com/kalyshev/yan/cabinet/service/CabinetService.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.kalyshev.yan.cabinet.service; - -import com.kalyshev.yan.cabinet.model.Cabinet; -import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException; -import com.kalyshev.yan.cabinet.repository.CabinetRepository; -import com.kalyshev.yan.computer.model.Computer; -import com.kalyshev.yan.computer.service.ComputerService; -import com.kalyshev.yan.util.validation.ValidatorUtil; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Optional; - -@Service -public class CabinetService { - private final CabinetRepository cabinetRepository; - private final ComputerService computerService; - private final ValidatorUtil validatorUtil; - public CabinetService(CabinetRepository cabinetRepository, - ComputerService computerService, - ValidatorUtil validatorUtil) { - this.cabinetRepository = cabinetRepository; - this.computerService = computerService; - this.validatorUtil = validatorUtil; - } - @Transactional - public Cabinet addCabinet(String number) { - if (!StringUtils.hasText(number)) { - throw new IllegalArgumentException("Cabinet number is null or empty"); - } - final Cabinet cabinet = new Cabinet(number); - validatorUtil.validate(cabinet); - return cabinetRepository.save(cabinet); - } - @Transactional(readOnly = true) - public Cabinet findCabinet(Long id) { - final Optional cabinet = cabinetRepository.findById(id); - return cabinet.orElseThrow(() -> new CabinetNotFoundException(id)); - } - @Transactional(readOnly = true) - public List findFilteredCabinets(Long id, String number) { - return cabinetRepository.findFilteredCabinets(id, number); - } - @Transactional(readOnly = true) - public List findAllCabinets() { - return cabinetRepository.findAll(); - } - @Transactional - public Cabinet updateCabinet(Long id, String number) { - if (!StringUtils.hasText(number)) { - throw new IllegalArgumentException("Cabinet number is null or empty"); - } - final Cabinet currentCabinet = findCabinet(id); - currentCabinet.setNumber(number); - validatorUtil.validate(currentCabinet); - return cabinetRepository.save(currentCabinet); - } - @Transactional - public Cabinet deleteCabinet(Long id) { - final Cabinet currentCabinet = findCabinet(id); - computerService.deleteRelationsWithCabinets(currentCabinet.getComputers()); - cabinetRepository.delete(currentCabinet); - return currentCabinet; - } - @Transactional - public void deleteAllCabinets() { - cabinetRepository.deleteAll(); - } - @Transactional - public List listComputersFromCabinet(Long id) { - if ((Object)id == null) { - throw new IllegalArgumentException("Cabinet id is null or empty"); - } - return findCabinet(id).getComputers(); - } - @Transactional - public void addComputerToCabinet(Long computerId, Long cabinetId) { - if ((Object)computerId == null) { - throw new IllegalArgumentException("Computer id is null or empty"); - } - if ((Object)cabinetId == null) { - throw new IllegalArgumentException("Cabinet id is null or empty"); - } - final Computer computer = computerService.findComputer(computerId); - final Cabinet cabinet = findCabinet(cabinetId); - cabinet.addComputer(computer); - } - @Transactional - public void deleteComputerFromCabinet(Long computerId, Long cabinetId) { - if ((Object) computerId == null) { - throw new IllegalArgumentException("Computer id is null or empty"); - } - if ((Object) cabinetId == null) { - throw new IllegalArgumentException("Cabinet id is null or empty"); - } - 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/category/controller/CategoryController.java b/src/main/java/com/kalyshev/yan/category/controller/CategoryController.java new file mode 100644 index 0000000..2171559 --- /dev/null +++ b/src/main/java/com/kalyshev/yan/category/controller/CategoryController.java @@ -0,0 +1,51 @@ +package com.kalyshev.yan.category.controller; + +import com.kalyshev.yan.WebConfiguration; +import com.kalyshev.yan.category.service.CategoryService; +import com.kalyshev.yan.video.service.VideoService; +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping(WebConfiguration.REST_API + "/category") +public class CategoryController { + private final CategoryService categoryService; + private final VideoService videoService; + public CategoryController(CategoryService categoryService, + VideoService videoService) { + this.categoryService = categoryService; + this.videoService = videoService; + } + @GetMapping("/{id}") + public CategoryDto getCategory(@PathVariable Long id) { + return new CategoryDto(categoryService.findCategory(id)); + } + @GetMapping("/") + public List getCategories() { + return categoryService.findAllCategories().stream() + .map(CategoryDto::new) + .toList(); + } + @GetMapping("/filter") + public List getFilteredCategories(@RequestParam(value = "id", required = false) Long id, + @RequestParam(value = "name", required = false) String name) { + return categoryService.findFilteredCategories(id, name).stream() + .map(CategoryDto::new) + .toList(); + } + @PostMapping("/") + public CategoryDto createMonitor(@RequestBody @Valid CategoryDto categoryDto) { + return new CategoryDto(categoryService.addCategory(categoryDto.getName())); + } + @PutMapping("/{id}") + public CategoryDto updateMonitor(@PathVariable Long id, + @RequestBody @Valid CategoryDto categoryDto) { + return new CategoryDto(categoryService.updateCategory(id, categoryDto.getName())); + } + @DeleteMapping("/{id}") + public CategoryDto deleteMonitor(@PathVariable Long id) { + return new CategoryDto(categoryService.deleteCategory(id)); + } +} \ No newline at end of file diff --git a/src/main/java/com/kalyshev/yan/category/controller/CategoryDto.java b/src/main/java/com/kalyshev/yan/category/controller/CategoryDto.java new file mode 100644 index 0000000..b56a6b4 --- /dev/null +++ b/src/main/java/com/kalyshev/yan/category/controller/CategoryDto.java @@ -0,0 +1,35 @@ +package com.kalyshev.yan.category.controller; + +import com.kalyshev.yan.cabinet.model.Cabinet; +import com.kalyshev.yan.category.model.Category; +import com.kalyshev.yan.computer.model.Computer; +import com.kalyshev.yan.video.model.Video; + +import java.util.ArrayList; +import java.util.List; + +public class CategoryDto { + private Long id; + private String name; + private List videoIds; + public CategoryDto() {} + public CategoryDto(Category category) { + this.id = category.getId(); + this.name = category.getName(); + if (category.getVideos() == null) { + this.videoIds = new ArrayList<>(); + } else { + this.videoIds = new ArrayList<>(); + List