Few improvements
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -83,6 +83,11 @@
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.9.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.DTO;
|
||||
|
||||
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;
|
||||
import java.util.List;
|
||||
|
||||
public class NoticeDTO {
|
||||
private String title;
|
||||
private Long clientId;
|
||||
private String description;
|
||||
private Double price;
|
||||
private Enums.Category category;
|
||||
private List<String> images;
|
||||
private Enums.Status status;
|
||||
private LocalDate publishDate;
|
||||
private List<AttributesNotice> attributesNotices;
|
||||
private List<Orders> orders;
|
||||
private List<Payments> payments;
|
||||
|
||||
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.clientId = clientId;
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
this.category = category;
|
||||
this.images = images;
|
||||
this.status = status;
|
||||
this.publishDate = publishDate;
|
||||
this.attributesNotices = attributesNotices;
|
||||
this.orders = orders;
|
||||
this.payments = payments;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Enums.Category getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(Enums.Category category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public List<String> getImages() {
|
||||
return images;
|
||||
}
|
||||
|
||||
public void setImages(List<String> images) {
|
||||
this.images = images;
|
||||
}
|
||||
|
||||
public Enums.Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Enums.Status status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public LocalDate getPublishDate() {
|
||||
return publishDate;
|
||||
}
|
||||
|
||||
public void setPublishDate(LocalDate publishDate) {
|
||||
this.publishDate = publishDate;
|
||||
}
|
||||
|
||||
public List<AttributesNotice> getAttributesNotices() {
|
||||
return attributesNotices;
|
||||
}
|
||||
|
||||
public void setAttributesNotices(List<AttributesNotice> attributesNotices) {
|
||||
this.attributesNotices = attributesNotices;
|
||||
}
|
||||
|
||||
public List<Orders> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<Orders> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
public List<Payments> getPayments() {
|
||||
return payments;
|
||||
}
|
||||
|
||||
public void setPayments(List<Payments> payments) {
|
||||
this.payments = payments;
|
||||
}
|
||||
|
||||
public Long getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setClientId(Long clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
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,143 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Utils.Enums.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "notice")
|
||||
public class Notice {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idNotice;
|
||||
|
||||
private String title;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "client_id")
|
||||
private Client client;
|
||||
|
||||
private String description;
|
||||
private Double price;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Category category;
|
||||
|
||||
@ElementCollection
|
||||
private List<String> images;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Status status;
|
||||
|
||||
private LocalDate publishDate;
|
||||
|
||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
private List<AttributesNotice> attributesNotices;
|
||||
|
||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
private List<Orders> orders;
|
||||
|
||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
private List<Payments> payments;
|
||||
|
||||
public Long getIdNotice() {
|
||||
return idNotice;
|
||||
}
|
||||
|
||||
public void setIdNotice(Long idNotice) {
|
||||
this.idNotice = idNotice;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Client getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public void setClient(Client client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Category getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(Category category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public List<String> getImages() {
|
||||
return images;
|
||||
}
|
||||
|
||||
public void setImages(List<String> images) {
|
||||
this.images = images;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Status status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public LocalDate getPublishDate() {
|
||||
return publishDate;
|
||||
}
|
||||
|
||||
public void setPublishDate(LocalDate publishDate) {
|
||||
this.publishDate = publishDate;
|
||||
}
|
||||
|
||||
public List<AttributesNotice> getAttributesNotices() {
|
||||
return attributesNotices;
|
||||
}
|
||||
|
||||
public void setAttributesNotices(List<AttributesNotice> attributesNotices) {
|
||||
this.attributesNotices = attributesNotices;
|
||||
}
|
||||
|
||||
public List<Orders> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<Orders> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
public List<Payments> getPayments() {
|
||||
return payments;
|
||||
}
|
||||
|
||||
public void setPayments(List<Payments> payments) {
|
||||
this.payments = payments;
|
||||
}
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.Service;
|
||||
|
||||
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 jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class NoticeService {
|
||||
|
||||
@Autowired
|
||||
private NoticeRepository noticeRepository;
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
public void addNotice(Notice notice) {
|
||||
noticeRepository.save(notice);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
package _11.asktpk.artisanconnectbackend.Controller;
|
||||
package _11.asktpk.artisanconnectbackend.controller;
|
||||
|
||||
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 _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -24,7 +23,7 @@ public class ArtisanConnectController {
|
||||
private ClientRepository clientRepository;
|
||||
|
||||
@GetMapping("/notices/all")
|
||||
public List<Notice> getAllNotices() {
|
||||
public List<NoticeDTO> getAllNotices() {
|
||||
return noticeService.getAllNotices();
|
||||
}
|
||||
|
||||
@@ -35,14 +34,16 @@ public class ArtisanConnectController {
|
||||
.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) {
|
||||
public ResponseEntity<String> addNotices(@RequestBody List<NoticeDTO> notices_list) {
|
||||
List<String> errors = new ArrayList<>();
|
||||
boolean isError = false;
|
||||
if (notices_list.isEmpty()) {
|
||||
@@ -0,0 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
public class ClientDTO {
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.entities.AttributesNotice;
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Getter @Setter
|
||||
public class NoticeDTO {
|
||||
private long noticeId;
|
||||
|
||||
private String title;
|
||||
|
||||
private Long clientId;
|
||||
|
||||
private String description;
|
||||
|
||||
private Double price;
|
||||
|
||||
private Enums.Category category;
|
||||
|
||||
private List<String> images;
|
||||
|
||||
private Enums.Status status;
|
||||
|
||||
private LocalDate publishDate;
|
||||
|
||||
private List<AttributesNotice> attributesNotices;
|
||||
|
||||
public NoticeDTO() {
|
||||
|
||||
}
|
||||
|
||||
public NoticeDTO(Long noticeId, String title, Long clientId, String description, Double price,
|
||||
Enums.Category category, List<String> images, Enums.Status status,
|
||||
LocalDate publishDate, List<AttributesNotice> attributesNotices) {
|
||||
this.noticeId = noticeId;
|
||||
this.title = title;
|
||||
this.clientId = clientId;
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
this.category = category;
|
||||
this.images = images;
|
||||
this.status = status;
|
||||
this.publishDate = publishDate;
|
||||
this.attributesNotices = attributesNotices;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Role;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "clients")
|
||||
@Getter @Setter
|
||||
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;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Table(name = "notice")
|
||||
@Getter @Setter
|
||||
public class Notice {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idNotice;
|
||||
|
||||
private String title;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "client_id")
|
||||
private Client client;
|
||||
|
||||
private String description;
|
||||
|
||||
private Double price;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Category category;
|
||||
|
||||
@ElementCollection
|
||||
private List<String> images;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Status status;
|
||||
|
||||
private LocalDate publishDate;
|
||||
|
||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
private List<AttributesNotice> attributesNotices;
|
||||
|
||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
private List<Orders> orders;
|
||||
|
||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
private List<Payments> payments;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Utils.Enums.Status;
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Status;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package _11.asktpk.artisanconnectbackend.Entities;
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Utils.Enums.Status;
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Status;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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;
|
||||
|
||||
public interface ClientRepository extends JpaRepository<Client, Long> {
|
||||
@@ -1,6 +1,6 @@
|
||||
package _11.asktpk.artisanconnectbackend.Repository;
|
||||
package _11.asktpk.artisanconnectbackend.repository;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package _11.asktpk.artisanconnectbackend.Service;
|
||||
package _11.asktpk.artisanconnectbackend.service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package _11.asktpk.artisanconnectbackend.service;
|
||||
|
||||
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 jakarta.persistence.EntityNotFoundException;
|
||||
import lombok.Builder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class NoticeService {
|
||||
|
||||
private final NoticeRepository noticeRepository;
|
||||
private final ClientRepository clientRepository;
|
||||
|
||||
public NoticeService(NoticeRepository noticeRepository, ClientRepository clientRepository) {
|
||||
this.noticeRepository = noticeRepository;
|
||||
this.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());
|
||||
|
||||
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) {
|
||||
NoticeDTO dto = new NoticeDTO();
|
||||
dto.setNoticeId(notice.getIdNotice());
|
||||
dto.setTitle(notice.getTitle());
|
||||
dto.setClientId(notice.getClient().getId());
|
||||
dto.setDescription(notice.getDescription());
|
||||
dto.setPrice(notice.getPrice());
|
||||
dto.setCategory(notice.getCategory());
|
||||
dto.setImages(notice.getImages());
|
||||
dto.setStatus(notice.getStatus());
|
||||
dto.setPublishDate(notice.getPublishDate());
|
||||
dto.setAttributesNotices(notice.getAttributesNotices());
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
public List<NoticeDTO> getAllNotices() {
|
||||
List<NoticeDTO> result = new ArrayList<>();
|
||||
for (Notice notice : noticeRepository.findAll()) {
|
||||
result.add(toDTO(notice));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void addNotice(Notice notice) {
|
||||
noticeRepository.save(notice);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package _11.asktpk.artisanconnectbackend.Utils;
|
||||
package _11.asktpk.artisanconnectbackend.utils;
|
||||
|
||||
public class Enums {
|
||||
public enum Role {
|
||||
@@ -5,5 +5,9 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/default_db
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
|
||||
spring.sql.init.data-locations=classpath:sql/data.sql
|
||||
spring.sql.init.mode=always
|
||||
spring.jpa.defer-datasource-initialization=true
|
||||
|
||||
# create and drop table, good for testing, production set to none or comment it
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
Reference in New Issue
Block a user