proper data flow

This commit is contained in:
2025-04-18 09:55:59 +02:00
parent 8fc8473962
commit b25bd3dbe9
4 changed files with 21 additions and 10 deletions

View File

@@ -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
@PutMapping("/edit/{id}")
public ResponseEntity updateClient(@PathVariable("id") long id, @RequestBody ClientDTO clientDTO) {

View File

@@ -4,7 +4,6 @@ import _11.asktpk.artisanconnectbackend.service.ClientService;
import _11.asktpk.artisanconnectbackend.service.NoticeService;
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@@ -45,7 +44,7 @@ public class NoticeController {
.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.");
}
@@ -68,7 +67,7 @@ public class NoticeController {
errors.add(dto.getClientId().toString());
} else {
if(!isError){
noticeService.addNotice(noticeService.fromDTO(dto));
noticeService.addNotice(dto);
}
}
}

View File

@@ -16,7 +16,7 @@ public class ClientService {
this.clientRepository = clientRepository;
}
public ClientDTO toDto(Client client) {
private ClientDTO toDto(Client client) {
ClientDTO dto = new ClientDTO();
dto.setId(client.getId());
@@ -29,7 +29,7 @@ public class ClientService {
return dto;
}
public Client fromDto(ClientDTO dto) {
private Client fromDto(ClientDTO dto) {
Client client = new Client();
client.setId(dto.getId());
@@ -55,6 +55,10 @@ public class ClientService {
return clientRepository.existsById(id);
}
public ClientDTO addClient(ClientDTO clientDTO) {
return toDto(clientRepository.save(fromDto(clientDTO)));
}
public ClientDTO updateClient(long id, ClientDTO clientDTO) {
Client existingClient = clientRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));

View File

@@ -22,7 +22,7 @@ public class NoticeService {
this.clientRepository = clientRepository;
}
public Notice fromDTO(NoticeDTO dto) {
private Notice fromDTO(NoticeDTO dto) {
Notice notice = new Notice();
notice.setTitle(dto.getTitle());
notice.setDescription(dto.getDescription());
@@ -40,8 +40,7 @@ public class NoticeService {
return notice;
}
// Metoda do konwersji Notice na DTO
public NoticeDTO toDTO(Notice notice) {
private NoticeDTO toDTO(Notice notice) {
NoticeDTO dto = new NoticeDTO();
dto.setNoticeId(notice.getIdNotice());
dto.setTitle(notice.getTitle());
@@ -71,8 +70,8 @@ public class NoticeService {
return toDTO(notice);
}
public void addNotice(Notice notice) {
noticeRepository.save(notice);
public void addNotice(NoticeDTO dto) {
noticeRepository.save(fromDTO(dto));
}
public boolean noticeExists(Long id) {