diff --git a/core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java b/core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java index 88aa38ec69fb25c071996e71362ca3a08c80702b..59b34a5f47fbe475260bacd4a16777d5a5d292a2 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java @@ -1,5 +1,6 @@ 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; + } } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/device/Device.java b/core/src/main/java/cz/muni/fi/pa165/core/device/Device.java index 8c78ad911ca9b164067608b6785c5ec814397b9d..376bc6dc66250528dedd3e78d5e161496d0b0c26 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/device/Device.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/device/Device.java @@ -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; diff --git a/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceController.java b/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceController.java index 933e9fe281ceceac63bc926e78c81c2889ced29b..53cbbdf9f6903449d97d7af43c4ca65832733730 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceController.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceController.java @@ -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); } } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceFacade.java b/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceFacade.java new file mode 100644 index 0000000000000000000000000000000000000000..ff3b57ee9dbf46983b9f81b50531d73483832991 --- /dev/null +++ b/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceFacade.java @@ -0,0 +1,62 @@ +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)); + + } + +} diff --git a/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceRepository.java b/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceRepository.java index f9d3ec85b1c97f34b231feef8c316205b6a7b858..7a5e42125884c4f5d06bb0c1396b58d19790f830 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceRepository.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceRepository.java @@ -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); } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceService.java b/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceService.java index 9a346d6e33b4263f81d9b4bfb8888276ecd173a1..dddb273c56708079cf0a00effac179d1008109b2 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/device/DeviceService.java @@ -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")); - } + + } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/house/HouseController.java b/core/src/main/java/cz/muni/fi/pa165/core/house/HouseController.java index 6e3a3cd0185733abe5ce1292ee8a6c8af86e4854..a1ad00d1525d2229a92fe1d0bf4f1e8607287ed8 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/house/HouseController.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/house/HouseController.java @@ -1,12 +1,7 @@ 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))); diff --git a/core/src/main/java/cz/muni/fi/pa165/core/house/HouseService.java b/core/src/main/java/cz/muni/fi/pa165/core/house/HouseService.java index 713710c525bbd79ca6ba10f6d50e15d6a756c0b0..e4a8990ba05e92aff91214c3aff2d292125577c0 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/house/HouseService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/house/HouseService.java @@ -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 + } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerController.java b/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerController.java index fec1c1dbd6a12cb83e4083fa14b61e9b728d67ab..cfec0028163d4bb35669ab28a61fa742f4d6618f 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerController.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerController.java @@ -1,61 +1,106 @@ 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 diff --git a/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerMapper.java b/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerMapper.java index 34081e24aa59a5a4f43f28a9081ae8dd01b77fa2..4408fb66eef213832842ad82127ca5afb21037ab 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerMapper.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerMapper.java @@ -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); } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerRepository.java b/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerRepository.java index 1369520c2a6ef400cc4b74c0512907b0527c7b04..cd91be497b91c15c1c9a76896c615a8a94bf3dd7 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerRepository.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerRepository.java @@ -1,7 +1,15 @@ 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); +} diff --git a/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerService.java b/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerService.java index 4068b76a7a0a09b3df31efc14d22e614e5a5885d..89e77157a452ab45443ad47e10c592dc4061d854 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/manufacturer/ManufacturerService.java @@ -1,6 +1,7 @@ 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); } + + } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterController.java b/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterController.java index ce6f5a345d146c074897b6c0799ea6500de574b4..c2a852b981d90fa58a4d5a06cf46c9f70be5f411 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterController.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterController.java @@ -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))); } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterService.java b/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterService.java index 9b5fb1f0c3840fc573ebe2801ec245d5d3037ed4..0f2458bd1bba6a8c76074216c8d823a0de0522b2 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterService.java @@ -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 } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java b/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java index 96e8cc525f2dadd3520f70cc98dba62b61c62e53..1f5f22d1c53c56bc1db55371b18ac4d99922b276 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java @@ -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); diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/manufacturer/ManufacturerUpdateDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/manufacturer/ManufacturerUpdateDto.java new file mode 100644 index 0000000000000000000000000000000000000000..e0e2be1f1b7f131c885bda2d956a37df8eb27c71 --- /dev/null +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/manufacturer/ManufacturerUpdateDto.java @@ -0,0 +1,7 @@ +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; +}