get one notice by id

This commit is contained in:
2025-04-17 12:00:36 +02:00
parent f17b97eaf9
commit ba7e82e90c
2 changed files with 39 additions and 26 deletions

View File

@@ -21,11 +21,20 @@ public class NoticeController {
@Autowired
private ClientService clientService;
@GetMapping("/all")
@GetMapping("/get/all")
public List<NoticeDTO> getAllNotices() {
return noticeService.getAllNotices();
}
@GetMapping("/get/{id}")
public ResponseEntity getNoticeById(@PathVariable long id) {
if (noticeService.noticeExists(id)) {
return ResponseEntity.ok(noticeService.getNoticeById(id));
} else {
return ResponseEntity.notFound().build();
}
}
@PostMapping("/add")
public ResponseEntity<String> addNotice(@RequestBody NoticeDTO dto) {
if (!clientService.clientExists(dto.getClientId())) {

View File

@@ -65,6 +65,12 @@ public class NoticeService {
return result;
}
public NoticeDTO getNoticeById(Long id) {
Notice notice = noticeRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));
return toDTO(notice);
}
public void addNotice(Notice notice) {
noticeRepository.save(notice);
}
@@ -73,7 +79,7 @@ public class NoticeService {
return noticeRepository.existsById(id);
}
public NoticeDTO updateNotice(Long id, NoticeDTO dto) {
public NoticeDTO updateNotice(Long id, NoticeDTO dto) {
Notice existingNotice = noticeRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));
@@ -92,15 +98,13 @@ public NoticeDTO updateNotice(Long id, NoticeDTO dto) {
}
return toDTO(noticeRepository.save(existingNotice));
}
}
public void deleteNotice(Long id) {
public void deleteNotice(Long id) {
if (noticeExists(id)) {
noticeRepository.deleteById(id);
} else {
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
}
}
}
}