Bulk add of notices working.
A basic functionality with DTO implemented TO-DO: zamiast dodawać tutaj pętlą, musi to robić NoticeService, trzeba zaimplementować odpowienią metodę
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
INSERT INTO Clients (email, first_name, image, last_name, password, role)
|
||||
VALUES(
|
||||
'dignissim.tempor.arcu@aol.ca', 'Diana', 'null', 'Harrison', 'password', 'USER'
|
||||
)
|
||||
@@ -1,29 +1,70 @@
|
||||
package _11.asktpk.artisanconnectbackend.Controller;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.Service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.DTO.NoticeDTO;
|
||||
import okhttp3.Response;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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")
|
||||
@RestController
|
||||
public class ArtisanConnectController {
|
||||
@Autowired
|
||||
private NoticeService noticeService;
|
||||
@Autowired
|
||||
private ClientRepository clientRepository;
|
||||
|
||||
@GetMapping("/notices/all")
|
||||
public List<Notice> getAllNotices() {
|
||||
return noticeService.getAllNotices();
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@PostMapping("/notices/add")
|
||||
public void addNotice(@RequestBody List<Notice> notices_list) {
|
||||
for (Notice notice : notices_list) {
|
||||
noticeService.addNotice(notice);
|
||||
public ResponseEntity<String> addNotice(@RequestBody NoticeDTO dto) {
|
||||
if (!clientRepository.existsById(dto.getClientId())) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
||||
}
|
||||
noticeService.addNotice(noticeService.createFromDTO(dto));
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("Dodano ogłoszenie.");
|
||||
}
|
||||
|
||||
|
||||
// TODO: zamiast dodawać tutaj pętlą, musi to robić NoticeService, trzeba zaimplementować odpowienią metodę
|
||||
@PostMapping("/notices/bulk_add")
|
||||
public ResponseEntity<String> addNotice(@RequestBody List<NoticeDTO> notices_list) {
|
||||
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());
|
||||
}
|
||||
|
||||
for(NoticeDTO dto : notices_list) {
|
||||
noticeService.addNotice(noticeService.createFromDTO(dto));
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("Dodano ogłoszenia.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
package _11.asktpk.artisanconnectbackend.DTO;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.AttributesNotice;
|
||||
import _11.asktpk.artisanconnectbackend.Model.Client;
|
||||
import _11.asktpk.artisanconnectbackend.Model.Orders;
|
||||
import _11.asktpk.artisanconnectbackend.Model.Payments;
|
||||
import _11.asktpk.artisanconnectbackend.Entities.AttributesNotice;
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Orders;
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Payments;
|
||||
import _11.asktpk.artisanconnectbackend.Utils.Enums;
|
||||
|
||||
import java.time.LocalDate;
|
||||
@@ -11,7 +10,7 @@ import java.util.List;
|
||||
|
||||
public class NoticeDTO {
|
||||
private String title;
|
||||
private Client client;
|
||||
private Long clientId;
|
||||
private String description;
|
||||
private Double price;
|
||||
private Enums.Category category;
|
||||
@@ -22,12 +21,12 @@ public class NoticeDTO {
|
||||
private List<Orders> orders;
|
||||
private List<Payments> payments;
|
||||
|
||||
public NoticeDTO(String title, Client client, String description, Double price,
|
||||
public NoticeDTO(String title, Long clientId, String description, Double price,
|
||||
Enums.Category category, List<String> images, Enums.Status status,
|
||||
LocalDate publishDate, List<AttributesNotice> attributesNotices,
|
||||
List<Orders> orders, List<Payments> payments) {
|
||||
this.title = title;
|
||||
this.client = client;
|
||||
this.clientId = clientId;
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
this.category = category;
|
||||
@@ -47,14 +46,6 @@ public class NoticeDTO {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Client getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public void setClient(Client client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
@@ -126,4 +117,12 @@ public class NoticeDTO {
|
||||
public void setPayments(List<Payments> payments) {
|
||||
this.payments = payments;
|
||||
}
|
||||
|
||||
public Long getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setClientId(Long clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Utils.Enums.Role;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "clients")
|
||||
public class Client {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String email;
|
||||
private String password;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String image; // Optional field
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Role role;
|
||||
|
||||
// @OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
|
||||
// private List<Notice> notices;
|
||||
|
||||
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
|
||||
private List<Orders> orders;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long idUser) {
|
||||
this.id = idUser;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public Role getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(Role role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
// public List<Notice> getNotices() {
|
||||
// return notices;
|
||||
// }
|
||||
//
|
||||
// public void setNotices(List<Notice> notices) {
|
||||
// this.notices = notices;
|
||||
// }
|
||||
|
||||
public List<Orders> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<Orders> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Utils.Enums.Status;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Utils.Enums.Status;
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Utils.Enums.Role;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "clients")
|
||||
public class Client {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idUser;
|
||||
|
||||
private String email;
|
||||
private String password;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String image; // Optional field
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Role role;
|
||||
|
||||
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
|
||||
private List<Notice> notices;
|
||||
|
||||
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
|
||||
private List<Orders> orders;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package _11.asktpk.artisanconnectbackend.Repository;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Client;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ClientRepository extends JpaRepository<Client, Long> {
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package _11.asktpk.artisanconnectbackend.Repository;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Notice;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package _11.asktpk.artisanconnectbackend.Service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Notice;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package _11.asktpk.artisanconnectbackend.Service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Client;
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.Repository.NoticeRepository;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
import _11.asktpk.artisanconnectbackend.DTO.NoticeDTO;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -14,17 +17,45 @@ public class NoticeService {
|
||||
@Autowired
|
||||
private NoticeRepository noticeRepository;
|
||||
|
||||
// private NoticeDTO mapToDTO(Notice notice) {
|
||||
// return new NoticeDTO(
|
||||
//
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// private Notice mapToEntity(NoticeDTO productDTO) {
|
||||
// return new Notice(
|
||||
//
|
||||
// );
|
||||
// }
|
||||
@Autowired
|
||||
private ClientRepository clientRepository;
|
||||
|
||||
public Notice createFromDTO(NoticeDTO dto) {
|
||||
Notice notice = new Notice();
|
||||
notice.setTitle(dto.getTitle());
|
||||
notice.setDescription(dto.getDescription());
|
||||
notice.setPrice(dto.getPrice());
|
||||
notice.setCategory(dto.getCategory());
|
||||
notice.setImages(dto.getImages());
|
||||
notice.setStatus(dto.getStatus());
|
||||
notice.setPublishDate(dto.getPublishDate());
|
||||
notice.setAttributesNotices(dto.getAttributesNotices());
|
||||
notice.setOrders(dto.getOrders());
|
||||
notice.setPayments(dto.getPayments());
|
||||
|
||||
Client client = clientRepository.findById(dto.getClientId())
|
||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono klienta o ID: " + dto.getClientId()));
|
||||
notice.setClient(client);
|
||||
|
||||
return notice;
|
||||
}
|
||||
|
||||
// Metoda do konwersji Notice na DTO
|
||||
public NoticeDTO toDTO(Notice notice) {
|
||||
return new NoticeDTO(
|
||||
notice.getTitle(),
|
||||
notice.getClient().getId(),
|
||||
notice.getDescription(),
|
||||
notice.getPrice(),
|
||||
notice.getCategory(),
|
||||
notice.getImages(),
|
||||
notice.getStatus(),
|
||||
notice.getPublishDate(),
|
||||
notice.getAttributesNotices(),
|
||||
notice.getOrders(),
|
||||
notice.getPayments()
|
||||
);
|
||||
}
|
||||
|
||||
public List<Notice> getAllNotices() {
|
||||
return noticeRepository.findAll();
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.Service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Repository.NoticeRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class PostgresDatabase implements IDatabase{
|
||||
@Autowired
|
||||
private NoticeRepository noticeRepository;
|
||||
|
||||
@Override
|
||||
public void add(Notice newNotice) {
|
||||
noticeRepository.save(newNotice);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Notice> get() {
|
||||
return noticeRepository.findAll();
|
||||
}
|
||||
}
|
||||
2
src/main/resources/sql/data.sql
Normal file
2
src/main/resources/sql/data.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
INSERT INTO clients (email, first_name, image, last_name, password, role)
|
||||
VALUES ('dignissim.tempor.arcu@aol.ca', 'Diana', 'null', 'Harrison', 'password', 'USER');
|
||||
@@ -1,23 +1,13 @@
|
||||
package _11.asktpk.artisanconnectbackend;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Service.PostgresDatabase;
|
||||
import okhttp3.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@SpringBootTest
|
||||
class ArtisanConnectBackendApplicationTests {
|
||||
private static final Logger logger = LogManager.getLogger(ArtisanConnectBackendApplicationTests.class);
|
||||
|
||||
@Autowired
|
||||
PostgresDatabase postgresDatabase;
|
||||
|
||||
// @Test
|
||||
// void testPostgresDatabase() {
|
||||
// postgresDatabase.add(new Notice("Test Notice", "Username", "Test Description"));
|
||||
|
||||
Reference in New Issue
Block a user