added user api
This commit is contained in:
parent
e4b1cf3daa
commit
1ea8ed0e93
@ -1,64 +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<CabinetDto> getCabinets() {
|
||||
return cabinetService.findAllCabinets().stream()
|
||||
.map(CabinetDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/{id}/computers")
|
||||
public List<ComputerDto> getCabinetComputers(@PathVariable Long id) {
|
||||
return cabinetService.listComputersFromCabinet(id).stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/filter")
|
||||
public List<CabinetDto> 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);
|
||||
}
|
||||
}
|
@ -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<Long> 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<Computer> 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<Long> getComputerIds() { return this.computerIds; }
|
||||
public void setComputerIds(List<Long> computerIds) { this.computerIds = computerIds; }
|
||||
}
|
@ -1,71 +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<Computer> 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<Computer> 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))
|
||||
return false;
|
||||
Cabinet cabinet = (Cabinet) o;
|
||||
return Objects.equals(id, cabinet.id) && Objects.equals(this.number, cabinet.number);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Cabinet{" +
|
||||
"id=" + id +
|
||||
", number='" + number + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -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<Cabinet, Long> {
|
||||
@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<Cabinet> findFilteredCabinets(@Param("id") Long id,
|
||||
@Param("number") String number);
|
||||
}
|
@ -1,104 +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> cabinet = cabinetRepository.findById(id);
|
||||
return cabinet.orElseThrow(() -> new CabinetNotFoundException(id));
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Cabinet> findFilteredCabinets(Long id, String number) {
|
||||
return cabinetRepository.findFilteredCabinets(id, number);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Cabinet> 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<Computer> 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);
|
||||
}
|
||||
}
|
@ -1,7 +1,11 @@
|
||||
package com.kalyshev.yan.coffee.controller;
|
||||
|
||||
import com.kalyshev.yan.coffee.model.Coffee;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class CoffeeDto {
|
||||
private Long id;
|
||||
private String name;
|
||||
@ -14,12 +18,4 @@ public class CoffeeDto {
|
||||
this.cost = coffee.getCost();
|
||||
this.ingredients = coffee.getIngredients();
|
||||
}
|
||||
public Long getId() { return this.id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getName() { return this.name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public Double getCost() { return this.cost; }
|
||||
public void setCost(Double cost) { this.cost = cost; }
|
||||
public String getIngredients() { return this.ingredients; }
|
||||
public void setIngredients(String ingredients) { this.ingredients = ingredients; }
|
||||
}
|
||||
|
@ -4,17 +4,23 @@ import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Getter
|
||||
@Entity
|
||||
@Table(name = "coffee")
|
||||
public class Coffee {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
@Setter
|
||||
private String name;
|
||||
@Setter
|
||||
private Double cost;
|
||||
@Setter
|
||||
private String ingredients;
|
||||
public Coffee() {
|
||||
}
|
||||
@ -23,28 +29,6 @@ public class Coffee {
|
||||
this.cost = cost;
|
||||
this.ingredients = ingredients;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Double getCost() {
|
||||
return cost;
|
||||
}
|
||||
public void setCost(Double cost) {
|
||||
this.cost = cost;
|
||||
}
|
||||
public String getIngredients() {
|
||||
return ingredients;
|
||||
}
|
||||
public void setIngredients(String ingredients) {
|
||||
this.ingredients = ingredients;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
|
@ -5,6 +5,7 @@ import com.kalyshev.yan.coffee.controller.CoffeeDto;
|
||||
import com.kalyshev.yan.coffee.controller.CoffeeResponse;
|
||||
import com.kalyshev.yan.coffee.model.Coffee;
|
||||
import com.kalyshev.yan.coffee.repository.CoffeeRepository;
|
||||
import com.kalyshev.yan.user.repository.UserNotFoundException;
|
||||
import com.kalyshev.yan.util.validation.ValidatorUtil;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@ -41,7 +42,7 @@ public class CoffeeService {
|
||||
@Transactional(readOnly = true)
|
||||
public Coffee findCoffee(Long id) {
|
||||
final Optional<Coffee> coffee = coffeeRepository.findById(id);
|
||||
return coffee.orElseThrow(() -> new CabinetNotFoundException(id));
|
||||
return coffee.orElseThrow(() -> new UserNotFoundException(id));
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public CoffeeResponse findAllCoffees(int pageNo, int pageSize, String sortBy, String sortDir) {
|
||||
|
@ -1,56 +0,0 @@
|
||||
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 org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/computer")
|
||||
public class ComputerController {
|
||||
private final ComputerService computerService;
|
||||
public ComputerController(ComputerService computerService) {
|
||||
this.computerService = computerService;
|
||||
}
|
||||
@GetMapping("/{id}")
|
||||
public ComputerDto getStudent(@PathVariable Long id) {
|
||||
return new ComputerDto(computerService.findComputer(id));
|
||||
}
|
||||
@GetMapping("/")
|
||||
public List<ComputerDto> getComputers() {
|
||||
return computerService.findAllComputers().stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/filter")
|
||||
public List<ComputerDto> getFilteredComputers(@RequestParam(value = "id", required = false) Long id,
|
||||
@RequestParam(value = "modelName", required = false) String modelName,
|
||||
@RequestParam(value = "serialNum", required = false) String serialNum,
|
||||
@RequestParam(value = "monitorId", required = false) Long monitorId,
|
||||
@RequestParam(value = "cabinetId", required = false) Long cabinetId) {
|
||||
return computerService.findFilteredComputers(id, modelName, serialNum, monitorId, cabinetId).stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
@PostMapping("/")
|
||||
public ComputerDto createComputer(@RequestBody @Valid ComputerDto computerDto) {
|
||||
return new ComputerDto(computerService.addComputer(computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId()));
|
||||
}
|
||||
@PostMapping("/{id}/monitor")
|
||||
public MonitorDto setMonitorComputer(@PathVariable Long id,
|
||||
@RequestParam("monitorId") Long monitorId) {
|
||||
return new MonitorDto(computerService.setMonitor(monitorId, id));
|
||||
}
|
||||
@PutMapping("/{id}")
|
||||
public ComputerDto updateComputer(@PathVariable Long id,
|
||||
@RequestBody @Valid ComputerDto computerDto) {
|
||||
return new ComputerDto(computerService.updateComputer(id, computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId(), computerDto.getCabinetId()));
|
||||
}
|
||||
@DeleteMapping("/{id}")
|
||||
public ComputerDto deleteComputer(@PathVariable Long id) {
|
||||
return new ComputerDto(computerService.deleteComputer(id));
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package com.kalyshev.yan.computer.controller;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
|
||||
public class ComputerDto {
|
||||
private Long id;
|
||||
private String modelName;
|
||||
private String serialNum;
|
||||
private Long monitorId;
|
||||
private Long cabinetId;
|
||||
public ComputerDto() {}
|
||||
public ComputerDto(Computer computer) {
|
||||
this.id = computer.getId();
|
||||
this.modelName = computer.getModelName();
|
||||
this.serialNum = computer.getSerialNum();
|
||||
if (computer.getMonitor() == null) {
|
||||
this.monitorId = null;
|
||||
} else {
|
||||
this.monitorId = computer.getMonitor().getId();
|
||||
}
|
||||
if (computer.getCabinet() == null) {
|
||||
this.cabinetId = null;
|
||||
} else {
|
||||
this.cabinetId = computer.getCabinet().getId();
|
||||
}
|
||||
}
|
||||
public Long getId() { return this.id; }
|
||||
public String getModelName() { return this.modelName; }
|
||||
public void setModelName(String modelName) { this.modelName = modelName; }
|
||||
public String getSerialNum() { return this.serialNum; }
|
||||
public void setSerialNum(String serialNum) { this.serialNum = serialNum; }
|
||||
public Long getMonitorId() { return this.monitorId; }
|
||||
public void setMonitorId(Long monitorId) { this.monitorId = monitorId; }
|
||||
public Long getCabinetId() { return this.cabinetId; }
|
||||
public void setCabinetId(Long cabinetId) { this.cabinetId = cabinetId; }
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
package com.kalyshev.yan.computer.model;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "computer")
|
||||
public class Computer {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String modelName;
|
||||
private String serialNum;
|
||||
@ManyToOne( cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "cabinet", nullable = true)
|
||||
private Cabinet cabinet;
|
||||
@OneToOne(cascade = {CascadeType.MERGE})
|
||||
@JoinColumn(name = "monitor_id")
|
||||
private Monitor monitor;
|
||||
|
||||
public Computer() {
|
||||
}
|
||||
public Computer(String modelName, String serialNum) {
|
||||
this.modelName = modelName;
|
||||
this.serialNum = serialNum;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
public String getSerialNum() {
|
||||
return serialNum;
|
||||
}
|
||||
public void setSerialNum(String serialNum) { this.serialNum = serialNum; }
|
||||
public Cabinet getCabinet() {
|
||||
return cabinet;
|
||||
}
|
||||
public void setCabinet(Cabinet cabinet) { this.cabinet = cabinet; }
|
||||
|
||||
public Monitor getMonitor() {
|
||||
return monitor;
|
||||
}
|
||||
public void setMonitor(Monitor monitor) {
|
||||
this.monitor = monitor;
|
||||
}
|
||||
public Monitor removeMonitor() {
|
||||
Monitor temp = this.monitor;
|
||||
this.monitor = null;
|
||||
return temp;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Computer))
|
||||
return false;
|
||||
Computer computer = (Computer) o;
|
||||
return Objects.equals(id, computer.id) && Objects.equals(this.modelName, computer.modelName) && Objects.equals(this.monitor, computer.monitor) && Objects.equals(this.cabinet, computer.cabinet);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Computer{" +
|
||||
"id=" + id +
|
||||
", modelName='" + modelName + '\'' +
|
||||
", serialNum='" + serialNum + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package com.kalyshev.yan.computer.repository;
|
||||
|
||||
public class ComputerNotFoundException extends RuntimeException {
|
||||
public ComputerNotFoundException(Long id) {
|
||||
super(String.format("Computer with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package com.kalyshev.yan.computer.repository;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public interface ComputerRepository extends JpaRepository<Computer, Long> {
|
||||
@Query(value = "select * from Computer where (\"ID\" = :id or :id is Null) and " +
|
||||
"(\"MODEL_NAME\" = :modelName or :modelName is Null) and " +
|
||||
"(\"SERIAL_NUM\" = :serialNum or :serialNum is Null) and " +
|
||||
"(\"MONITOR_ID\" = :monitorId or :monitorId is Null) and " +
|
||||
"(\"CABINET\" = :cabinetId or :cabinetId is Null)", nativeQuery = true)
|
||||
public List<Computer> findFilteredComputers(@Param("id") Long id,
|
||||
@Param("modelName") String modelName,
|
||||
@Param("serialNum") String serialNum,
|
||||
@Param("monitorId") Long monitorId,
|
||||
@Param("cabinetId") Long cabinetId);
|
||||
|
||||
@Query(value = "select * from Computer where \"MONITOR_ID\" = :monitorId", nativeQuery = true)
|
||||
public Computer findComputerByMonitor(@Param("monitorId") Long monitorId);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "update Computer set \"MONITOR_ID\" = null", nativeQuery = true)
|
||||
public void removeAllComputerMonitorRelations();
|
||||
|
||||
// @Query(value = "update Computer where id in :computerIds set cabinet_id = null")
|
||||
// public void deleteRelationsWithCabinets(@Param("computerIds") Collection<Long> computerIds);
|
||||
}
|
@ -1,135 +0,0 @@
|
||||
package com.kalyshev.yan.computer.service;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.repository.ComputerNotFoundException;
|
||||
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 org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class ComputerService {
|
||||
private final ComputerRepository computerRepository;
|
||||
private final MonitorService monitorService;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public ComputerService(ComputerRepository computerRepository,
|
||||
MonitorService monitorService,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.computerRepository = computerRepository;
|
||||
this.monitorService = monitorService;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public Computer addComputer(String modelName, String serialNum, @Nullable Long monitorId) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Computer model name is null or empty");
|
||||
}
|
||||
if (!StringUtils.hasText(serialNum)) {
|
||||
throw new IllegalArgumentException("Computer serial number is null or empty");
|
||||
}
|
||||
final Computer computer = new Computer(modelName, serialNum);
|
||||
if (monitorId != null) {
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
computer.setMonitor(monitor);
|
||||
}
|
||||
validatorUtil.validate(computer);
|
||||
return computerRepository.save(computer);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public Computer findComputer(Long id) {
|
||||
final Optional<Computer> computer = computerRepository.findById(id);
|
||||
return computer.orElseThrow(() -> new ComputerNotFoundException(id));
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Computer> findAllComputers() {
|
||||
return computerRepository.findAll();
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Computer> findFilteredComputers(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||
return computerRepository.findFilteredComputers(id, modelName, serialNum, monitorId, cabinetId);
|
||||
}
|
||||
@Transactional
|
||||
public Computer updateComputer(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||
if (!(StringUtils.hasText(modelName) || StringUtils.hasText(serialNum))) {
|
||||
throw new IllegalArgumentException("Need at least one argument");
|
||||
}
|
||||
final Computer currentComputer = findComputer(id);
|
||||
if (modelName != null) {
|
||||
currentComputer.setModelName(modelName);
|
||||
}
|
||||
if (serialNum != null) {
|
||||
currentComputer.setSerialNum(serialNum);
|
||||
}
|
||||
if (monitorId != null) {
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
currentComputer.setMonitor(monitor);
|
||||
}
|
||||
validatorUtil.validate(currentComputer);
|
||||
return computerRepository.save(currentComputer);
|
||||
}
|
||||
@Transactional
|
||||
public Computer deleteComputer(Long id) {
|
||||
final Computer currentComputer = findComputer(id);
|
||||
Cabinet currentComputerCabinet = currentComputer.getCabinet();
|
||||
if (currentComputerCabinet != null) {
|
||||
currentComputerCabinet.removeComputer(currentComputer);
|
||||
}
|
||||
computerRepository.delete(currentComputer);
|
||||
return currentComputer;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllComputers() {
|
||||
List<Computer> computers = findAllComputers();
|
||||
for (Computer computer : computers) {
|
||||
deleteComputer(computer.getId());
|
||||
}
|
||||
}
|
||||
@Transactional
|
||||
public Monitor setMonitor(Long monitorId, Long computerId) {
|
||||
if ((Object)computerId == null) {
|
||||
throw new IllegalArgumentException("Computer id is null or empty");
|
||||
}
|
||||
if ((Object)monitorId == null) {
|
||||
throw new IllegalArgumentException("Monitor id is null or empty");
|
||||
}
|
||||
final Computer computer = findComputer(computerId);
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
computer.setMonitor(monitor);
|
||||
return monitor;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteRelationsWithCabinets(List<Computer> computers) {
|
||||
for (Computer computer: computers) {
|
||||
computer.setCabinet(null);
|
||||
}
|
||||
}
|
||||
@Transactional
|
||||
public Computer findComputerByMonitor(Monitor monitor) {
|
||||
if (monitor.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
return computerRepository.findComputerByMonitor(monitor.getId());
|
||||
}
|
||||
@Transactional
|
||||
public Monitor deleteMonitorWithRelation(Long id) {
|
||||
final Monitor currentMonitor = monitorService.findMonitor(id);
|
||||
Computer computer = findComputerByMonitor(currentMonitor);
|
||||
if (computer != null) {
|
||||
computer.removeMonitor();
|
||||
}
|
||||
monitorService.deleteMonitor(currentMonitor);
|
||||
return currentMonitor;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllMonitorsWithRelations() {
|
||||
computerRepository.removeAllComputerMonitorRelations();
|
||||
monitorService.deleteAllMonitors();
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
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 org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/monitor")
|
||||
public class MonitorController {
|
||||
private final MonitorService monitorService;
|
||||
private final ComputerService computerService;
|
||||
public MonitorController(MonitorService monitorService,
|
||||
ComputerService computerService) {
|
||||
this.monitorService = monitorService;
|
||||
this.computerService = computerService;
|
||||
}
|
||||
@GetMapping("/{id}")
|
||||
public MonitorDto getMonitor(@PathVariable Long id) {
|
||||
return new MonitorDto(monitorService.findMonitor(id));
|
||||
}
|
||||
@GetMapping("/")
|
||||
public List<MonitorDto> getMonitors() {
|
||||
return monitorService.findAllMonitors().stream()
|
||||
.map(MonitorDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/filter")
|
||||
public List<MonitorDto> getFilteredmonitors(@RequestParam(value = "id", required = false) Long id,
|
||||
@RequestParam(value = "modelName", required = false) String modelName) {
|
||||
return monitorService.findFilteredMonitors(id, modelName).stream()
|
||||
.map(MonitorDto::new)
|
||||
.toList();
|
||||
}
|
||||
@PostMapping("/")
|
||||
public MonitorDto createMonitor(@RequestBody @Valid MonitorDto monitorDto) {
|
||||
return new MonitorDto(monitorService.addMonitor(monitorDto.getModelName()));
|
||||
}
|
||||
@PutMapping("/{id}")
|
||||
public MonitorDto updateMonitor(@PathVariable Long id,
|
||||
@RequestBody @Valid MonitorDto monitorDto) {
|
||||
return new MonitorDto(monitorService.updateMonitor(id, monitorDto.getModelName()));
|
||||
}
|
||||
@DeleteMapping("/{id}")
|
||||
public MonitorDto deleteMonitor(@PathVariable Long id) {
|
||||
return new MonitorDto(computerService.deleteMonitorWithRelation(id));
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.kalyshev.yan.monitor.controller;
|
||||
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
|
||||
public class MonitorDto {
|
||||
private Long id;
|
||||
private String modelName;
|
||||
public MonitorDto() {
|
||||
}
|
||||
public MonitorDto(Monitor monitor) {
|
||||
this.id = monitor.getId();
|
||||
this.modelName = monitor.getModelName();
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package com.kalyshev.yan.monitor.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "monitor")
|
||||
public class Monitor {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String modelName;
|
||||
public Monitor() {
|
||||
}
|
||||
public Monitor(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Monitor))
|
||||
return false;
|
||||
Monitor monitor = (Monitor) o;
|
||||
return Objects.equals(id, monitor.id) && Objects.equals(this.modelName, monitor.modelName);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Monitor{" +
|
||||
"id=" + id +
|
||||
", modelName='" + modelName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package com.kalyshev.yan.monitor.repository;
|
||||
|
||||
public class MonitorNotFoundException extends RuntimeException {
|
||||
public MonitorNotFoundException(Long id) {
|
||||
super(String.format("Monitor with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package com.kalyshev.yan.monitor.repository;
|
||||
|
||||
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 MonitorRepository extends JpaRepository<Monitor, Long> {
|
||||
@Query(value = "select * from Monitor where (\"ID\" = :id or :id is Null) and " +
|
||||
"(\"MODEL_NAME\" = :modelName or :modelName is Null)", nativeQuery = true)
|
||||
public List<Monitor> findFilteredMonitors(@Param("id") Long id,
|
||||
@Param("modelName") String modelName);
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package com.kalyshev.yan.monitor.service;
|
||||
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorNotFoundException;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorRepository;
|
||||
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 MonitorService {
|
||||
private final MonitorRepository monitorRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public MonitorService(MonitorRepository monitorRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.monitorRepository = monitorRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public Monitor addMonitor(String modelName) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Monitor model name is null or empty");
|
||||
}
|
||||
final Monitor monitor = new Monitor(modelName);
|
||||
validatorUtil.validate(monitor);
|
||||
return monitorRepository.save(monitor);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public Monitor findMonitor(Long id) {
|
||||
final Optional<Monitor> monitor = monitorRepository.findById(id);
|
||||
return monitor.orElseThrow(() -> new MonitorNotFoundException(id));
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Monitor> findAllMonitors() {
|
||||
return monitorRepository.findAll();
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Monitor> findFilteredMonitors(Long id, String modelName) {
|
||||
return monitorRepository.findFilteredMonitors(id, modelName);
|
||||
}
|
||||
@Transactional
|
||||
public Monitor updateMonitor(Long id, String modelName) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Monitor model name is null or empty");
|
||||
}
|
||||
final Monitor currentMonitor = findMonitor(id);
|
||||
currentMonitor.setModelName(modelName);
|
||||
validatorUtil.validate(currentMonitor);
|
||||
return monitorRepository.save(currentMonitor);
|
||||
}
|
||||
@Transactional
|
||||
public void deleteMonitor(Monitor monitor) {
|
||||
monitorRepository.delete(monitor);
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllMonitors() { monitorRepository.deleteAll(); }
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.kalyshev.yan.user.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.user.service.UserService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/user")
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
@GetMapping("/{id}")
|
||||
public UserDto getUser(@PathVariable Long id) {
|
||||
return new UserDto(userService.findUser(id));
|
||||
}
|
||||
@GetMapping("/")
|
||||
public UserResponse getAllUsers(
|
||||
@RequestParam(value = "pageNo", defaultValue = "0", required = false) int pageNo,
|
||||
@RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
|
||||
@RequestParam(value = "sortBy", defaultValue = "id", required = false) String sortBy,
|
||||
@RequestParam(value = "sortDir", defaultValue = "asc", required = false) String sortDir
|
||||
){
|
||||
return userService.findAllUsers(pageNo, pageSize, sortBy, sortDir);
|
||||
}
|
||||
@PostMapping("/")
|
||||
public UserDto createUser(@RequestBody @Valid UserDto userDto) {
|
||||
return new UserDto(userService.addUser(userDto.getLogin(), userDto.getFio(), userDto.getPhone(), userDto.getPassword(), userDto.getRole()));
|
||||
}
|
||||
@PutMapping("/{id}")
|
||||
public UserDto updateUser(@PathVariable Long id,
|
||||
@RequestBody @Valid UserDto userDto) {
|
||||
return new UserDto(userService.updateUser(id, userDto.getLogin(), userDto.getFio(), userDto.getPhone(), userDto.getPassword(), userDto.getRole()));
|
||||
}
|
||||
@DeleteMapping("/{id}")
|
||||
public UserDto deleteUser(@PathVariable Long id) {
|
||||
return new UserDto(userService.deleteUser(id));
|
||||
}
|
||||
@DeleteMapping("/")
|
||||
public void deleteAllUsers() {
|
||||
userService.deleteAllUsers();
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.kalyshev.yan.user.controller;
|
||||
|
||||
import com.kalyshev.yan.user.model.User;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class UserDto {
|
||||
private Long id;
|
||||
private String login;
|
||||
private String fio;
|
||||
private String phone;
|
||||
private String password;
|
||||
private String role;
|
||||
public UserDto() {}
|
||||
public UserDto(User user) {
|
||||
this.id = user.getId();
|
||||
this.login = user.getLogin();
|
||||
this.fio = user.getFio();
|
||||
this.phone = user.getPhone();
|
||||
this.password = user.getPassword();
|
||||
this.role = user.getRole();
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.kalyshev.yan.user.controller;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UserResponse {
|
||||
private List<UserDto> content;
|
||||
private int pageNo;
|
||||
private int pageSize;
|
||||
private long totalElements;
|
||||
private int totalPages;
|
||||
private boolean last;
|
||||
}
|
67
backend/src/main/java/com/kalyshev/yan/user/model/User.java
Normal file
67
backend/src/main/java/com/kalyshev/yan/user/model/User.java
Normal file
@ -0,0 +1,67 @@
|
||||
package com.kalyshev.yan.user.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Getter
|
||||
@Entity
|
||||
@Table(name = "user")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
@Setter
|
||||
private String login;
|
||||
@Setter
|
||||
private String fio;
|
||||
@Setter
|
||||
private String phone;
|
||||
@Setter
|
||||
private String password;
|
||||
@Setter
|
||||
private String role;
|
||||
public User() {
|
||||
}
|
||||
public User(String login, String fio, String phone, String password, String role) {
|
||||
this.login = login;
|
||||
this.fio = fio;
|
||||
this.phone = phone;
|
||||
this.password = password;
|
||||
this.role = role;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof User))
|
||||
return false;
|
||||
User user = (User) o;
|
||||
return Objects.equals(id, user.id) &&
|
||||
Objects.equals(this.login, user.login) &&
|
||||
Objects.equals(this.fio, user.fio) &&
|
||||
Objects.equals(this.phone, user.phone) &&
|
||||
Objects.equals(this.password, user.password) &&
|
||||
Objects.equals(this.role, user.role);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", login='" + login + '\'' +
|
||||
", fio='" + fio + '\'' +
|
||||
", phone='" + phone + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
", role='" + role + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.user.repository;
|
||||
|
||||
public class UserNotFoundException extends RuntimeException {
|
||||
public UserNotFoundException(Long id) {
|
||||
super(String.format("User with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.user.repository;
|
||||
|
||||
import com.kalyshev.yan.user.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
package com.kalyshev.yan.user.service;
|
||||
|
||||
import com.kalyshev.yan.user.controller.UserDto;
|
||||
import com.kalyshev.yan.user.controller.UserResponse;
|
||||
import com.kalyshev.yan.user.model.User;
|
||||
import com.kalyshev.yan.user.repository.UserNotFoundException;
|
||||
import com.kalyshev.yan.user.repository.UserRepository;
|
||||
import com.kalyshev.yan.util.validation.ValidatorUtil;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
private final UserRepository userRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public UserService(UserRepository userRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.userRepository = userRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public User addUser(String login, String fio, String phone, String password, String role) {
|
||||
if (!StringUtils.hasText(login)) {
|
||||
throw new IllegalArgumentException("User login is null or empty");
|
||||
}
|
||||
if (!StringUtils.hasText(fio)) {
|
||||
throw new IllegalArgumentException("User fio is null or empty");
|
||||
}
|
||||
if (!StringUtils.hasText(phone)) {
|
||||
throw new IllegalArgumentException("User phone is null or empty");
|
||||
}
|
||||
if (!StringUtils.hasText(password)) {
|
||||
throw new IllegalArgumentException("User password is null or empty");
|
||||
}
|
||||
if (!StringUtils.hasText(role)) {
|
||||
throw new IllegalArgumentException("User role is null or empty");
|
||||
}
|
||||
final User user = new User(login, fio, phone, password, role);
|
||||
validatorUtil.validate(user);
|
||||
return userRepository.save(user);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public User findUser(Long id) {
|
||||
final Optional<User> user = userRepository.findById(id);
|
||||
return user.orElseThrow(() -> new UserNotFoundException(id));
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public UserResponse findAllUsers(int pageNo, int pageSize, String sortBy, String sortDir) {
|
||||
Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortBy).ascending()
|
||||
: Sort.by(sortBy).descending();
|
||||
|
||||
// create Pageable instance
|
||||
Pageable pageable = PageRequest.of(pageNo, pageSize, sort);
|
||||
|
||||
Page<User> users = userRepository.findAll(pageable);
|
||||
|
||||
// get content for page object
|
||||
List<User> listOfUsers = users.getContent();
|
||||
|
||||
List<UserDto> content = listOfUsers.stream()
|
||||
.map(UserDto::new)
|
||||
.toList();
|
||||
|
||||
UserResponse userResponse = new UserResponse();
|
||||
userResponse.setContent(content);
|
||||
userResponse.setPageNo(users.getNumber());
|
||||
userResponse.setPageSize(users.getSize());
|
||||
userResponse.setTotalElements(users.getTotalElements());
|
||||
userResponse.setTotalPages(users.getTotalPages());
|
||||
userResponse.setLast(users.isLast());
|
||||
|
||||
return userResponse;
|
||||
}
|
||||
@Transactional
|
||||
public User updateUser(Long id, String login, String fio, String phone, String password, String role) {
|
||||
final User currentUser = findUser(id);
|
||||
if (StringUtils.hasText(login)) {
|
||||
currentUser.setLogin(login);
|
||||
}
|
||||
if (StringUtils.hasText(fio)) {
|
||||
currentUser.setFio(fio);
|
||||
}
|
||||
if (StringUtils.hasText(phone)) {
|
||||
currentUser.setPhone(phone);
|
||||
}
|
||||
if (StringUtils.hasText(password)) {
|
||||
currentUser.setPassword(password);
|
||||
}
|
||||
if (StringUtils.hasText(role)) {
|
||||
currentUser.setRole(role);
|
||||
}
|
||||
validatorUtil.validate(currentUser);
|
||||
return userRepository.save(currentUser);
|
||||
}
|
||||
@Transactional
|
||||
public User deleteUser(Long id) {
|
||||
final User currentUser = findUser(id);
|
||||
userRepository.delete(currentUser);
|
||||
return currentUser;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllUsers() {
|
||||
userRepository.deleteAll();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user