Client controller, and WIP with edits and deletion of notice
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -88,6 +88,11 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.validation</groupId>
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package _11.asktpk.artisanconnectbackend.controller;
|
||||
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/clients")
|
||||
public class ClientController {
|
||||
private final ClientService clientService;
|
||||
|
||||
public ClientController(ClientService clientService, ClientRepository clientRepository) {
|
||||
this.clientService = clientService;
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public List<ClientDTO> getAllClients() {
|
||||
return clientService.getAllClients();
|
||||
}
|
||||
|
||||
// TODO: do zrobienia walidacja danych
|
||||
@PutMapping("/edit/{id}")
|
||||
public ResponseEntity updateClient(@PathVariable("id") long id, @RequestBody ClientDTO clientDTO) {
|
||||
if(clientService.clientExists(id)) {
|
||||
return new ResponseEntity<>(clientService.updateClient(clientDTO),HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ResponseEntity deleteClient(@PathVariable("id") long id) {
|
||||
if(clientService.clientExists(id)) {
|
||||
clientService.deleteClient(id);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package _11.asktpk.artisanconnectbackend.controller;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
@@ -12,25 +11,23 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.web.servlet.function.ServerResponse.status;
|
||||
|
||||
@RequestMapping("/api/v1")
|
||||
@RequestMapping("/api/v1/notices")
|
||||
@RestController
|
||||
public class ArtisanConnectController {
|
||||
public class NoticeController {
|
||||
@Autowired
|
||||
private NoticeService noticeService;
|
||||
|
||||
@Autowired
|
||||
private ClientService clientService;
|
||||
|
||||
@GetMapping("/notices/all")
|
||||
@GetMapping("/all")
|
||||
public List<NoticeDTO> getAllNotices() {
|
||||
return noticeService.getAllNotices();
|
||||
}
|
||||
|
||||
@PostMapping("/notices/add")
|
||||
@PostMapping("/add")
|
||||
public ResponseEntity<String> addNotice(@RequestBody NoticeDTO dto) {
|
||||
if (!clientService.getByID(dto.getClientId())) {
|
||||
if (!clientService.clientExists(dto.getClientId())) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
||||
@@ -43,7 +40,7 @@ public class ArtisanConnectController {
|
||||
|
||||
|
||||
// TODO: zamiast dodawać tutaj pętlą, musi to robić NoticeService, trzeba zaimplementować odpowienią metodę
|
||||
@PostMapping("/notices/bulk_add")
|
||||
@PostMapping("/bulk_add")
|
||||
public ResponseEntity<String> addNotices(@RequestBody List<NoticeDTO> notices_list) {
|
||||
ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.CREATED);
|
||||
List<String> errors = new ArrayList<>();
|
||||
@@ -54,7 +51,7 @@ public class ArtisanConnectController {
|
||||
}
|
||||
|
||||
for (NoticeDTO dto : notices_list) {
|
||||
if (!clientService.getByID(dto.getClientId())) {
|
||||
if (!clientService.clientExists(dto.getClientId())) {
|
||||
isError = true;
|
||||
errors.add(dto.getClientId().toString());
|
||||
} else {
|
||||
@@ -71,8 +68,21 @@ public class ArtisanConnectController {
|
||||
return response;
|
||||
}
|
||||
|
||||
@GetMapping("/clients/all")
|
||||
public List<ClientDTO> getAllClients() {
|
||||
return clientService.getAllClients();
|
||||
@PutMapping("/edit/{id}")
|
||||
public ResponseEntity editNotice(@PathVariable("id") long id, @RequestBody NoticeDTO dto) {
|
||||
if (noticeService.noticeExists(id)) {
|
||||
return new ResponseEntity<>(noticeService.updateNotice(dto), HttpStatus.OK);
|
||||
} else {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Nie znaleziono ogłoszenia o ID: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
// @GetMapping("/check/{id}")
|
||||
// public ResponseEntity<String> checkNotice(@PathVariable("id") long id) {
|
||||
// if (noticeService.noticeExists(id)) {
|
||||
// return ResponseEntity.ok("Ogłoszenie o ID " + id + " istnieje.");
|
||||
// } else {
|
||||
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Nie znaleziono ogłoszenia o ID: " + id);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -1,13 +1,19 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Role;
|
||||
|
||||
@Getter @Setter
|
||||
public class ClientDTO {
|
||||
private Long id;
|
||||
|
||||
@Email
|
||||
@NotBlank
|
||||
private String email;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
@@ -46,8 +46,20 @@ public class ClientService {
|
||||
return clients.stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
public boolean getByID(Long id) {
|
||||
public Client getClientById(Long id) {
|
||||
return clientRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public boolean clientExists(Long id) {
|
||||
return clientRepository.existsById(id);
|
||||
}
|
||||
|
||||
public ClientDTO updateClient(ClientDTO clientDTO) {
|
||||
Client client = fromDto(clientDTO);
|
||||
return toDto(clientRepository.save(client));
|
||||
}
|
||||
|
||||
public void deleteClient(Long id) {
|
||||
clientRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IDatabase {
|
||||
void add(Notice newNotice);
|
||||
List<Notice> get();
|
||||
}
|
||||
@@ -70,4 +70,13 @@ public class NoticeService {
|
||||
public void addNotice(Notice notice) {
|
||||
noticeRepository.save(notice);
|
||||
}
|
||||
|
||||
public boolean noticeExists(Long id) {
|
||||
return noticeRepository.existsById(id);
|
||||
}
|
||||
|
||||
public NoticeDTO updateNotice(NoticeDTO dto) {
|
||||
Notice notice = createFromDTO(dto);
|
||||
return toDTO(noticeRepository.save(notice));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
spring.application.name=ArtisanConnectBackend
|
||||
|
||||
## PostgreSQL
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/default_db
|
||||
spring.datasource.url=jdbc:postgresql://192.168.56.103:5432/default_db
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
|
||||
|
||||
@@ -1,2 +1,7 @@
|
||||
INSERT INTO clients (email, first_name, image, last_name, password, role)
|
||||
VALUES ('dignissim.tempor.arcu@aol.ca', 'Diana', 'null', 'Harrison', 'password', 'USER');
|
||||
VALUES
|
||||
('dignissim.tempor.arcu@aol.ca', 'Diana', 'null', 'Harrison', 'password', 'USER'),
|
||||
('john.doe@example.com', 'John', 'null', 'Doe', 'password123', 'ADMIN'),
|
||||
('jane.smith@example.com', 'Jane', 'null', 'Smith', 'securepass', 'USER'),
|
||||
('michael.brown@example.com', 'Michael', 'null', 'Brown', 'mypassword', 'USER'),
|
||||
('emily.jones@example.com', 'Emily', 'null', 'Jones', 'passw0rd', 'USER');
|
||||
|
||||
Reference in New Issue
Block a user