Quite good implementation of notice additions
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package _11.asktpk.artisanconnectbackend.controller;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -19,8 +19,9 @@ import static org.springframework.web.servlet.function.ServerResponse.status;
|
||||
public class ArtisanConnectController {
|
||||
@Autowired
|
||||
private NoticeService noticeService;
|
||||
|
||||
@Autowired
|
||||
private ClientRepository clientRepository;
|
||||
private ClientService clientService;
|
||||
|
||||
@GetMapping("/notices/all")
|
||||
public List<NoticeDTO> getAllNotices() {
|
||||
@@ -29,7 +30,7 @@ public class ArtisanConnectController {
|
||||
|
||||
@PostMapping("/notices/add")
|
||||
public ResponseEntity<String> addNotice(@RequestBody NoticeDTO dto) {
|
||||
if (!clientRepository.existsById(dto.getClientId())) {
|
||||
if (!clientService.getByID(dto.getClientId())) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
||||
@@ -44,28 +45,34 @@ public class ArtisanConnectController {
|
||||
// TODO: zamiast dodawać tutaj pętlą, musi to robić NoticeService, trzeba zaimplementować odpowienią metodę
|
||||
@PostMapping("/notices/bulk_add")
|
||||
public ResponseEntity<String> addNotices(@RequestBody List<NoticeDTO> notices_list) {
|
||||
ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.CREATED);
|
||||
List<String> errors = new ArrayList<>();
|
||||
boolean isError = false;
|
||||
|
||||
if (notices_list.isEmpty()) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body("Lista ogłoszeń jest pusta.");
|
||||
}
|
||||
for (NoticeDTO dto : notices_list) {
|
||||
if (!clientRepository.existsById(dto.getClientId())) {
|
||||
isError = true;
|
||||
errors.add("Nie znaleziono klienta o ID: " + dto.getClientId());
|
||||
}
|
||||
}
|
||||
if(isError) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body(errors.toString());
|
||||
return response.status(HttpStatus.BAD_REQUEST).body("Lista ogłoszeń jest pusta.");
|
||||
}
|
||||
|
||||
for(NoticeDTO dto : notices_list) {
|
||||
noticeService.addNotice(noticeService.createFromDTO(dto));
|
||||
for (NoticeDTO dto : notices_list) {
|
||||
if (!clientService.getByID(dto.getClientId())) {
|
||||
isError = true;
|
||||
errors.add(dto.getClientId().toString());
|
||||
} else {
|
||||
if(!isError){
|
||||
noticeService.addNotice(noticeService.createFromDTO(dto));
|
||||
}
|
||||
}
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("Dodano ogłoszenia.");
|
||||
|
||||
if(isError) {
|
||||
return response.status(HttpStatus.BAD_REQUEST).body("Nie znaleziono klientów: " + errors);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@GetMapping("/clients/all")
|
||||
public List<ClientDTO> getAllClients() {
|
||||
return clientService.getAllClients();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user