Quite good implementation of notice additions
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package _11.asktpk.artisanconnectbackend.controller;
|
package _11.asktpk.artisanconnectbackend.controller;
|
||||||
|
|
||||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -19,8 +19,9 @@ import static org.springframework.web.servlet.function.ServerResponse.status;
|
|||||||
public class ArtisanConnectController {
|
public class ArtisanConnectController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private NoticeService noticeService;
|
private NoticeService noticeService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ClientRepository clientRepository;
|
private ClientService clientService;
|
||||||
|
|
||||||
@GetMapping("/notices/all")
|
@GetMapping("/notices/all")
|
||||||
public List<NoticeDTO> getAllNotices() {
|
public List<NoticeDTO> getAllNotices() {
|
||||||
@@ -29,7 +30,7 @@ public class ArtisanConnectController {
|
|||||||
|
|
||||||
@PostMapping("/notices/add")
|
@PostMapping("/notices/add")
|
||||||
public ResponseEntity<String> addNotice(@RequestBody NoticeDTO dto) {
|
public ResponseEntity<String> addNotice(@RequestBody NoticeDTO dto) {
|
||||||
if (!clientRepository.existsById(dto.getClientId())) {
|
if (!clientService.getByID(dto.getClientId())) {
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.status(HttpStatus.BAD_REQUEST)
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
.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ę
|
// TODO: zamiast dodawać tutaj pętlą, musi to robić NoticeService, trzeba zaimplementować odpowienią metodę
|
||||||
@PostMapping("/notices/bulk_add")
|
@PostMapping("/notices/bulk_add")
|
||||||
public ResponseEntity<String> addNotices(@RequestBody List<NoticeDTO> notices_list) {
|
public ResponseEntity<String> addNotices(@RequestBody List<NoticeDTO> notices_list) {
|
||||||
|
ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.CREATED);
|
||||||
List<String> errors = new ArrayList<>();
|
List<String> errors = new ArrayList<>();
|
||||||
boolean isError = false;
|
boolean isError = false;
|
||||||
|
|
||||||
if (notices_list.isEmpty()) {
|
if (notices_list.isEmpty()) {
|
||||||
return ResponseEntity
|
return response.status(HttpStatus.BAD_REQUEST).body("Lista ogłoszeń jest pusta.");
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(NoticeDTO dto : notices_list) {
|
for (NoticeDTO dto : notices_list) {
|
||||||
noticeService.addNotice(noticeService.createFromDTO(dto));
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,16 @@
|
|||||||
package _11.asktpk.artisanconnectbackend.dto;
|
package _11.asktpk.artisanconnectbackend.dto;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.utils.Enums.Role;
|
||||||
|
|
||||||
|
@Getter @Setter
|
||||||
public class ClientDTO {
|
public class ClientDTO {
|
||||||
|
private Long id;
|
||||||
|
private String email;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private String image;
|
||||||
|
private Role role;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package _11.asktpk.artisanconnectbackend.repository;
|
package _11.asktpk.artisanconnectbackend.repository;
|
||||||
|
|
||||||
import _11.asktpk.artisanconnectbackend.entities.Client;
|
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface ClientRepository extends JpaRepository<Client, Long> {
|
public interface ClientRepository extends JpaRepository<Client, Long> {
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.service;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||||
|
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ClientService {
|
||||||
|
private final ClientRepository clientRepository;
|
||||||
|
|
||||||
|
public ClientService(ClientRepository clientRepository) {
|
||||||
|
this.clientRepository = clientRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientDTO toDto(Client client) {
|
||||||
|
ClientDTO dto = new ClientDTO();
|
||||||
|
|
||||||
|
dto.setId(client.getId());
|
||||||
|
dto.setFirstName(client.getFirstName());
|
||||||
|
dto.setLastName(client.getLastName());
|
||||||
|
dto.setEmail(client.getEmail());
|
||||||
|
dto.setRole(client.getRole());
|
||||||
|
dto.setImage(client.getImage());
|
||||||
|
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Client fromDto(ClientDTO dto) {
|
||||||
|
Client client = new Client();
|
||||||
|
|
||||||
|
client.setId(dto.getId());
|
||||||
|
client.setFirstName(dto.getFirstName());
|
||||||
|
client.setLastName(dto.getLastName());
|
||||||
|
client.setEmail(dto.getEmail());
|
||||||
|
client.setRole(dto.getRole());
|
||||||
|
client.setImage(dto.getImage());
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ClientDTO> getAllClients() {
|
||||||
|
List<Client> clients = clientRepository.findAll();
|
||||||
|
return clients.stream().map(this::toDto).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getByID(Long id) {
|
||||||
|
return clientRepository.existsById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user