proper data flow
This commit is contained in:
@@ -32,6 +32,15 @@ public class ClientController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/add")
|
||||||
|
public ResponseEntity addClient(@RequestBody ClientDTO clientDTO) {
|
||||||
|
if(clientService.clientExists(clientDTO.getId())) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||||
|
} else {
|
||||||
|
return new ResponseEntity<>(clientService.addClient(clientDTO), HttpStatus.CREATED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: do zrobienia walidacja danych
|
// TODO: do zrobienia walidacja danych
|
||||||
@PutMapping("/edit/{id}")
|
@PutMapping("/edit/{id}")
|
||||||
public ResponseEntity updateClient(@PathVariable("id") long id, @RequestBody ClientDTO clientDTO) {
|
public ResponseEntity updateClient(@PathVariable("id") long id, @RequestBody ClientDTO clientDTO) {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ 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 jakarta.persistence.EntityNotFoundException;
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@@ -45,7 +44,7 @@ public class NoticeController {
|
|||||||
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
||||||
}
|
}
|
||||||
|
|
||||||
noticeService.addNotice(noticeService.fromDTO(dto));
|
noticeService.addNotice(dto);
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body("Dodano ogłoszenie.");
|
return ResponseEntity.status(HttpStatus.CREATED).body("Dodano ogłoszenie.");
|
||||||
}
|
}
|
||||||
@@ -68,7 +67,7 @@ public class NoticeController {
|
|||||||
errors.add(dto.getClientId().toString());
|
errors.add(dto.getClientId().toString());
|
||||||
} else {
|
} else {
|
||||||
if(!isError){
|
if(!isError){
|
||||||
noticeService.addNotice(noticeService.fromDTO(dto));
|
noticeService.addNotice(dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class ClientService {
|
|||||||
this.clientRepository = clientRepository;
|
this.clientRepository = clientRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClientDTO toDto(Client client) {
|
private ClientDTO toDto(Client client) {
|
||||||
ClientDTO dto = new ClientDTO();
|
ClientDTO dto = new ClientDTO();
|
||||||
|
|
||||||
dto.setId(client.getId());
|
dto.setId(client.getId());
|
||||||
@@ -29,7 +29,7 @@ public class ClientService {
|
|||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Client fromDto(ClientDTO dto) {
|
private Client fromDto(ClientDTO dto) {
|
||||||
Client client = new Client();
|
Client client = new Client();
|
||||||
|
|
||||||
client.setId(dto.getId());
|
client.setId(dto.getId());
|
||||||
@@ -55,6 +55,10 @@ public class ClientService {
|
|||||||
return clientRepository.existsById(id);
|
return clientRepository.existsById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ClientDTO addClient(ClientDTO clientDTO) {
|
||||||
|
return toDto(clientRepository.save(fromDto(clientDTO)));
|
||||||
|
}
|
||||||
|
|
||||||
public ClientDTO updateClient(long id, ClientDTO clientDTO) {
|
public ClientDTO updateClient(long id, ClientDTO clientDTO) {
|
||||||
Client existingClient = clientRepository.findById(id)
|
Client existingClient = clientRepository.findById(id)
|
||||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));
|
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class NoticeService {
|
|||||||
this.clientRepository = clientRepository;
|
this.clientRepository = clientRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Notice fromDTO(NoticeDTO dto) {
|
private Notice fromDTO(NoticeDTO dto) {
|
||||||
Notice notice = new Notice();
|
Notice notice = new Notice();
|
||||||
notice.setTitle(dto.getTitle());
|
notice.setTitle(dto.getTitle());
|
||||||
notice.setDescription(dto.getDescription());
|
notice.setDescription(dto.getDescription());
|
||||||
@@ -40,8 +40,7 @@ public class NoticeService {
|
|||||||
return notice;
|
return notice;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metoda do konwersji Notice na DTO
|
private NoticeDTO toDTO(Notice notice) {
|
||||||
public NoticeDTO toDTO(Notice notice) {
|
|
||||||
NoticeDTO dto = new NoticeDTO();
|
NoticeDTO dto = new NoticeDTO();
|
||||||
dto.setNoticeId(notice.getIdNotice());
|
dto.setNoticeId(notice.getIdNotice());
|
||||||
dto.setTitle(notice.getTitle());
|
dto.setTitle(notice.getTitle());
|
||||||
@@ -71,8 +70,8 @@ public class NoticeService {
|
|||||||
return toDTO(notice);
|
return toDTO(notice);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addNotice(Notice notice) {
|
public void addNotice(NoticeDTO dto) {
|
||||||
noticeRepository.save(notice);
|
noticeRepository.save(fromDTO(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean noticeExists(Long id) {
|
public boolean noticeExists(Long id) {
|
||||||
|
|||||||
Reference in New Issue
Block a user