Skip to content
Snippets Groups Projects
Commit b1786c3e authored by Marek Skácelík's avatar Marek Skácelík
Browse files

Merge branch 'device' into 'milestone-1'

Added UPDATE/DELETE for manufacturer and device and added all openAPI for all...

See merge request !12
parents d3e3716a 79de2be5
No related branches found
No related tags found
2 merge requests!25Milestone 1,!12Added UPDATE/DELETE for manufacturer and device and added all openAPI for all...
Pipeline #
Showing
with 284 additions and 153 deletions
package cz.muni.fi.pa165.core.common;
import cz.muni.fi.pa165.core.manufacturer.Manufacturer;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
......@@ -41,4 +42,18 @@ public abstract class DomainService<T extends DomainObject> {
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Entity with '" + id + "' not found."));
}
@Transactional(readOnly = true)
public void deleteAll() {
getRepository().deleteAll();
}
@Transactional(readOnly = true)
public T deleteById(String id) {
T result = findById(id);
if (result == null)
throw new EntityNotFoundException("Entity '" + id + "' not found.");
getRepository().deleteById(id);
return result;
}
}
......@@ -22,9 +22,11 @@ public class Device extends DomainObject {
private String name;
@OneToMany private List<SmartMeter> smartMeterList;
@OneToMany
private List<SmartMeter> smartMeterList;
@ManyToOne private Manufacturer manufacturer;
@ManyToOne
private Manufacturer manufacturer;
// Company ??
// private Company company;
......
......@@ -4,64 +4,84 @@ import cz.muni.fi.pa165.model.dto.device.DeviceCreateDto;
import cz.muni.fi.pa165.model.dto.device.DeviceDto;
import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/device")
@Tag(name = "Device", description = "Device API")
public class DeviceController {
private final DeviceService deviceService;
private final DeviceMapper mapper;
private DeviceFacade deviceFacade;
@Autowired
public DeviceController(DeviceService deviceService, DeviceMapper mapper) {
this.deviceService = deviceService;
this.mapper = mapper;
public DeviceController(DeviceFacade deviceFacade) {
this.deviceFacade = deviceFacade;
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get all devices pageable",
description = "...")
@GetMapping("pageable")
/*
@Operation(
summary = "Get all devices pageable",
description = "Returns a pageable list of all devices.")
@GetMapping("/pageable/")
public List<DeviceDto> findAllPageable(Pageable pageable) {
return mapper.toDtoList((List<Device>) deviceService.findAllPageable(pageable));
return deviceFacade.findAllPageable(pageable);
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get all devices",
description = "...")
*/
@Operation(
summary = "Get all devices",
description = "Returns a list of all devices.")
@CrossOrigin(origins = "*")
@GetMapping // CORS headers needed for JavaScript clients
@GetMapping
public List<DeviceDto> findAll() {
return mapper.toDtoList(deviceService.findAll());
return deviceFacade.findAll();
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get device by id",
description = "...")
@Operation(
summary = "Get device by id",
description = "Returns a device with the specified id.")
@GetMapping("/{id}")
public DeviceDto find(@PathVariable String id) {
return mapper.toDto(deviceService.findById(id));
public DeviceDto findById(@PathVariable @Parameter(description = "The id of the device.") String id) {
return deviceFacade.findById(id);
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Create device",
description = "...")
@Operation(
summary = "Create device",
description = "Creates a new device.")
@PostMapping
public DeviceDto create(@RequestBody DeviceCreateDto deviceDto) {
return mapper.toDto(deviceService.create(mapper.fromCreateDto(deviceDto)));
public DeviceDto create(@RequestBody DeviceCreateDto deviceCreateDto) {
return deviceFacade.create(deviceCreateDto);
}
@Operation(
summary = "Update device",
description = "Updates the device with the specified id.")
@PutMapping("/{id}")
public DeviceDto update(@PathVariable @Parameter(description = "The id of the device.") String id,
@RequestBody @Valid DeviceUpdateDto deviceUpdateDto) {
return deviceFacade.update(deviceUpdateDto, id);
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Update device",
description = "...")
@PutMapping
public DeviceDto update(@RequestBody DeviceUpdateDto deviceDto) {
return mapper.toDto(deviceService.create(mapper.fromUpdateDto(deviceDto)));
@Operation(
summary = "Delete device",
description = "Deletes the device with the specified id.")
@DeleteMapping("/{id}")
public DeviceDto delete(
@PathVariable @Parameter(description = "The id of the device.") String id) {
return deviceFacade.delete(id);
}
}
package cz.muni.fi.pa165.core.device;
import cz.muni.fi.pa165.model.dto.common.Result;
import cz.muni.fi.pa165.model.dto.device.DeviceCreateDto;
import cz.muni.fi.pa165.model.dto.device.DeviceDto;
import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Component
@Transactional
public class DeviceFacade {
private final DeviceService deviceService;
private final DeviceMapper deviceMapper;
@Autowired
public DeviceFacade(DeviceService deviceService, DeviceMapper deviceMapper) {
this.deviceService = deviceService;
this.deviceMapper = deviceMapper;
}
@Transactional(readOnly = true)
public DeviceDto findById(String id) {
return deviceMapper.toDto(deviceService.findById(id));
}
@Transactional(readOnly = true)
public List<DeviceDto> findAllPageable(Pageable pageable) {
return deviceMapper.toDtoList((List<Device>) deviceService.findAllPageable(pageable));
}
@Transactional(readOnly = true)
public Result<DeviceDto> findAll(int page) {
return deviceMapper.toResult(deviceService.findAllPageableInt(page));
}
@Transactional(readOnly = true)
public List<DeviceDto> findAll() {
return deviceMapper.toDtoList(deviceService.findAll());
}
@Transactional(readOnly = true)
public DeviceDto create(DeviceCreateDto deviceCreateDto) {
return deviceMapper.toDto(deviceService.create(deviceMapper.fromCreateDto(deviceCreateDto)));
}
@Transactional
public DeviceDto delete(String id) {
return deviceMapper.toDto(deviceService.deleteById(id));
}
@Transactional
public DeviceDto update(DeviceUpdateDto deviceUpdateDto, String id) {
return deviceMapper.toDto(deviceService.update(deviceMapper.fromUpdateDto(deviceUpdateDto), id));
}
}
......@@ -11,6 +11,6 @@ import java.util.Optional;
@Repository
public interface DeviceRepository extends JpaRepository<Device, String> {
@Modifying
@Query("UPDATE Device d set d.name= :#{#device.name} where d.id = :#{#device.id}")
Optional<Device> update(@Param("device") Device device);
@Query("UPDATE Device d set d.name= :#{#device.name} where d.id = :#{#id}")
int update(@Param("device") Device device, @Param("id") String id);
}
......@@ -14,8 +14,8 @@ import java.util.Optional;
@Service
public class DeviceService extends DomainService<Device> {
@Getter private final DeviceRepository repository;
@Getter
private final DeviceRepository repository;
@Autowired
public DeviceService(DeviceRepository deviceRepository) {
......@@ -23,17 +23,14 @@ public class DeviceService extends DomainService<Device> {
}
@Transactional(readOnly = true)
public Device findById(String id) {
return repository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Device with '" + id + "' not found"));
public Device update(Device user, String id) {
int result = repository.update(user, id);
if (result != 1) {
throw new EntityNotFoundException("Device '" + id + "' not found.");
}
return findById(id);
}
@Transactional
public Device update(Device device) {
return repository
.update(device)
.orElseThrow(
() -> new EntityNotFoundException("Device with '" + device.getId() + "' not found"));
}
}
package cz.muni.fi.pa165.core.house;
import cz.muni.fi.pa165.core.smartmeter.SmartMeter;
import cz.muni.fi.pa165.core.smartmeter.SmartMeterMapper;
import cz.muni.fi.pa165.core.smartmeter.SmartMeterService;
import cz.muni.fi.pa165.model.dto.house.HouseCreateDto;
import cz.muni.fi.pa165.model.dto.house.HouseDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterCreateDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterDto;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
......@@ -27,34 +22,38 @@ public class HouseController {
this.mapper = mapper;
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get all house pageable",
description = "...")
@Operation(
summary = "Get all houses pageable",
description = "Retrieve a pageable list of all houses."
)
@GetMapping("pageable")
public List<HouseDto> findAllPageable(Pageable pageable) {
return mapper.toDtoList((List<House>) houseService.findAllPageable(pageable));
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get all houses",
description = "...")
@Operation(
summary = "Get all houses",
description = "Retrieve a list of all houses."
)
@CrossOrigin(origins = "*")
@GetMapping // CORS headers needed for JavaScript clients
@GetMapping
public List<HouseDto> findAll() {
return mapper.toDtoList(houseService.findAll());
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get house by id",
description = "...")
@Operation(
summary = "Get a house by ID",
description = "Retrieve a house with the specified ID."
)
@GetMapping("/{id}")
public HouseDto find(@PathVariable String id) {
return mapper.toDto(houseService.findById(id));
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Create house",
description = "...")
@Operation(
summary = "Create a new house",
description = "Create a new house with the specified details."
)
@PostMapping
public HouseDto create(@RequestBody HouseCreateDto houseDto) {
return mapper.toDto(houseService.create(mapper.fromCreateDto(houseDto)));
......
......@@ -17,10 +17,7 @@ public class HouseService extends DomainService<House> {
repository = houseRepository;
}
@Transactional(readOnly = true)
public House findById(String id) {
return repository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("House with '" + id + "' not found"));
}
// TODO: UPDATE
}
package cz.muni.fi.pa165.core.manufacturer;
import cz.muni.fi.pa165.core.house.House;
import cz.muni.fi.pa165.model.dto.house.HouseCreateDto;
import cz.muni.fi.pa165.model.dto.house.HouseDto;
import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerCreateDto;
import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerDto;
import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerUpdateDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Controller for managing manufacturers.
*/
@RestController
@RequestMapping("/api/manufacturer")
public class ManufacturerController {
private final ManufacturerService manufacturerService;
private final ManufacturerMapper mapper;
private final ManufacturerMapper manufacturerMapper;
@Autowired
public ManufacturerController(
ManufacturerService manufacturerService, ManufacturerMapper mapper) {
ManufacturerService manufacturerService,
ManufacturerMapper manufacturerMapper
) {
this.manufacturerService = manufacturerService;
this.mapper = mapper;
this.manufacturerMapper = manufacturerMapper;
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get all manufacturer pageable",
description = "...")
/**
* Returns a pageable list of all manufacturers.
*
* @param pageable pagination parameters
* @return a list of manufacturer DTOs
*/
@Operation(
summary = "Get all manufacturers (paginated)",
description = "Returns a pageable list of all manufacturers.")
@GetMapping("pageable")
public List<ManufacturerDto> findAllPageable(Pageable pageable) {
return mapper.toDtoList((List<Manufacturer>) manufacturerService.findAllPageable(pageable));
}
return manufacturerMapper.toDtoList((List<Manufacturer>) manufacturerService.findAllPageable(pageable)); }
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get all manufacturers",
description = "...")
/**
* Returns a list of all manufacturers.
*
* @return a list of manufacturer DTOs
*/
@Operation(
summary = "Get all manufacturers",
description = "Returns a list of all manufacturers.")
@CrossOrigin(origins = "*")
@GetMapping // CORS headers needed for JavaScript clients
public List<ManufacturerDto> findAll() {
return mapper.toDtoList(manufacturerService.findAll());
return manufacturerMapper.toDtoList(manufacturerService.findAll());
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get manufacturer by id",
description = "...")
/**
* Returns the manufacturer with the specified ID.
*
* @param id the ID of the manufacturer to retrieve
* @return the manufacturer DTO
*/
@Operation(
summary = "Get manufacturer by ID",
description = "Returns the manufacturer with the specified ID.")
@GetMapping("/{id}")
public ManufacturerDto find(@PathVariable String id) {
return mapper.toDto(manufacturerService.findById(id));
public ManufacturerDto find(
@PathVariable @Parameter(description = "The ID of the manufacturer.") String id) {
return manufacturerMapper.toDto(manufacturerService.findById(id));
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Create manufacturer",
description = "...")
/**
* Creates a new manufacturer.
*
* @param manufacturerDto the manufacturer DTO to create
* @return the created manufacturer DTO
*/
@Operation(
summary = "Create manufacturer",
description = "Creates a new manufacturer.")
@PostMapping
public ManufacturerDto create(@RequestBody ManufacturerCreateDto manufacturerDto) {
return mapper.toDto(manufacturerService.create(mapper.fromCreateDto(manufacturerDto)));
public ManufacturerDto create(@RequestBody @Valid ManufacturerCreateDto manufacturerDto) {
return manufacturerMapper.toDto(
manufacturerService.create(manufacturerMapper.fromCreateDto(manufacturerDto)));
}
@Operation(
summary = "Update device",
description = "Updates the device with the specified id.")
@PutMapping("/{id}")
public ManufacturerDto update(@PathVariable @Parameter(description = "The id of the manufacturer.") String id,
@RequestBody @Valid ManufacturerUpdateDto manufacturerUpdateDto) {
return manufacturerMapper.toDto(manufacturerService.update(manufacturerMapper.fromUpdateDto(manufacturerUpdateDto), id));
}
@Operation(
summary = "Delete manufacturer",
description = "Deletes the manufacturer with the specified id.")
@DeleteMapping("/{id}")
public ManufacturerDto delete(
@PathVariable @Parameter(description = "The id of the manufacturer.") String id) {
return manufacturerMapper.toDto(manufacturerService.deleteById(id));
}
}
}
\ No newline at end of file
......@@ -3,10 +3,11 @@ package cz.muni.fi.pa165.core.manufacturer;
import cz.muni.fi.pa165.core.common.DomainMapper;
import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerCreateDto;
import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerDto;
import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerUpdateDto;
import org.mapstruct.Mapper;
@Mapper
public interface ManufacturerMapper extends DomainMapper<Manufacturer, ManufacturerDto> {
Manufacturer fromCreateDto(ManufacturerCreateDto dto);
Manufacturer fromUpdateDto(ManufacturerUpdateDto dto);
}
package cz.muni.fi.pa165.core.manufacturer;
import cz.muni.fi.pa165.core.device.Device;
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 org.springframework.stereotype.Repository;
@Repository
public interface ManufacturerRepository extends JpaRepository<Manufacturer, String> {}
public interface ManufacturerRepository extends JpaRepository<Manufacturer, String> {
@Modifying
@Query("UPDATE Manufacturer m set m.name= :#{#manufacturer.name} where m.id = :#{#id}")
int update(@Param("manufacturer") Manufacturer manufacturer, @Param("id") String id);
}
package cz.muni.fi.pa165.core.manufacturer;
import cz.muni.fi.pa165.core.common.DomainService;
import cz.muni.fi.pa165.core.user.User;
import jakarta.persistence.EntityNotFoundException;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -9,18 +10,23 @@ import org.springframework.transaction.annotation.Transactional;
@Service
public class ManufacturerService extends DomainService<Manufacturer> {
@Getter private final ManufacturerRepository repository;
@Getter
private final ManufacturerRepository repository;
@Autowired
public ManufacturerService(ManufacturerRepository manufacturerRepository) {
repository = manufacturerRepository;
}
@Transactional(readOnly = true)
public Manufacturer findById(String id) {
return repository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Manufacturer with '" + id + "' not found"));
public Manufacturer update(Manufacturer manufacturer, String id) {
int result = repository.update(manufacturer, id);
if (result != 1) {
throw new EntityNotFoundException("Manufacturer '" + id + "' not found.");
}
return findById(id);
}
}
......@@ -3,6 +3,7 @@ package cz.muni.fi.pa165.core.smartmeter;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterCreateDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
......@@ -21,35 +22,31 @@ public class SmartMeterController {
this.mapper = mapper;
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get all smartMeter pageable",
description = "...")
@GetMapping("pageable")
public List<SmartMeterDto> findAllPageable(Pageable pageable) {
@Operation(summary = "Get a pageable list of smart meters", description = "Returns a pageable list of smart meters.")
@GetMapping("/pageable")
@ApiResponse(responseCode = "200", description = "Successfully retrieved the pageable list of smart meters.") public List<SmartMeterDto> findAllPageable(Pageable pageable) {
return mapper.toDtoList((List<SmartMeter>) smartMeterService.findAllPageable(pageable));
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get all smartMeters",
description = "...")
@CrossOrigin(origins = "*")
@GetMapping // CORS headers needed for JavaScript clients
@Operation(summary = "Get all smart meters", description = "Returns a list of all smart meters.")
@GetMapping
@ApiResponse(responseCode = "200", description = "Successfully retrieved the list of smart meters.")
@CrossOrigin(origins = "*") // CORS headers needed for JavaScript clients
public List<SmartMeterDto> findAll() {
return mapper.toDtoList(smartMeterService.findAll());
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Get smartMeter by id",
description = "...")
@Operation(summary = "Get a smart meter by ID", description = "Returns the smart meter with the specified ID.")
@GetMapping("/{id}")
@ApiResponse(responseCode = "200", description = "Successfully retrieved the smart meter.")
@ApiResponse(responseCode = "404", description = "Smart meter not found.")
public SmartMeterDto find(@PathVariable String id) {
return mapper.toDto(smartMeterService.findById(id));
}
@Operation( // metadata for inclusion into OpenAPI document
summary = "Create smartMeter",
description = "...")
@Operation(summary = "Create a new smart meter", description = "Creates a new smart meter.")
@PostMapping
@ApiResponse(responseCode = "201", description = "Successfully created a new smart meter.")
public SmartMeterDto create(@RequestBody SmartMeterCreateDto smartMeterDto) {
return mapper.toDto(smartMeterService.create(mapper.fromCreateDto(smartMeterDto)));
}
......
......@@ -11,16 +11,12 @@ import org.springframework.transaction.annotation.Transactional;
@Service
public class SmartMeterService extends DomainService<SmartMeter> {
@Getter private final SmartMeterRepository repository;
@Getter
private final SmartMeterRepository repository;
public SmartMeterService(SmartMeterRepository smartMeterRepository) {
this.repository = smartMeterRepository;
}
@Transactional(readOnly = true)
public SmartMeter findSmartMeterById(String id) {
return repository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("SmartMeter with '" + id + "' not found"));
}
// TODO: UPDATE
}
......@@ -17,27 +17,6 @@ public class UserService extends DomainService<User> {
this.repository = repository;
}
@Transactional(readOnly = true)
public User findById(String id) {
return repository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("User '" + id + "' not found."));
}
@Transactional(readOnly = true)
public void deleteAll() {
repository.deleteAll();
}
@Transactional(readOnly = true)
public User deleteById(String id) {
User result = findById(id);
if (result == null)
throw new EntityNotFoundException("User '" + id + "' not found.");
repository.deleteById(id);
return result;
}
@Transactional(readOnly = true)
public User update(User user, String id) {
int result = repository.update(user, id);
......
package cz.muni.fi.pa165.model.dto.manufacturer;
import cz.muni.fi.pa165.model.dto.common.DomainObjectDto;
public class ManufacturerUpdateDto extends DomainObjectDto {
public String name;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment