Compare commits
8 Commits
fdee6ecc84
...
f17b97eaf9
| Author | SHA1 | Date | |
|---|---|---|---|
| f17b97eaf9 | |||
| b0f47b475a | |||
| 4fa138203d | |||
| b826a01a10 | |||
| 686e580f03 | |||
| 4c6d3f9452 | |||
| 01d531aa23 | |||
| f65b10b302 |
10
pom.xml
10
pom.xml
@@ -83,6 +83,16 @@
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.9.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<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>
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.Controller;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.Repository.NoticeRepository;
|
||||
import _11.asktpk.artisanconnectbackend.Service.PostgresDatabase;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping("/api/v1")
|
||||
@RestController
|
||||
public class ArtisanConnectController {
|
||||
@Autowired
|
||||
private PostgresDatabase postgresDatabase;
|
||||
|
||||
@GetMapping("/notices/all")
|
||||
public List<Notice> getAllNotices() {
|
||||
return postgresDatabase.get();
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@PostMapping("/notices/add")
|
||||
public void addNotice(@RequestBody Notice notice) {
|
||||
postgresDatabase.add(notice);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.Model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.sql.Blob;
|
||||
|
||||
@Entity
|
||||
public class Notice {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private String title;
|
||||
|
||||
@Column
|
||||
private String username;
|
||||
|
||||
@Column
|
||||
private String description;
|
||||
|
||||
public Notice() {
|
||||
this.title = "";
|
||||
this.username = "";
|
||||
this.description = "";
|
||||
}
|
||||
|
||||
public Notice(String nTitle, String nUsername, String nDescription) {
|
||||
this.title = nTitle;
|
||||
this.username = nUsername;
|
||||
this.description = nDescription;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String user) {
|
||||
this.username = user;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.Service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IDatabase {
|
||||
void add(Notice newNotice);
|
||||
List<Notice> get();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
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(id, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package _11.asktpk.artisanconnectbackend.controller;
|
||||
|
||||
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.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping("/api/v1/notices")
|
||||
@RestController
|
||||
public class NoticeController {
|
||||
@Autowired
|
||||
private NoticeService noticeService;
|
||||
|
||||
@Autowired
|
||||
private ClientService clientService;
|
||||
|
||||
@GetMapping("/all")
|
||||
public List<NoticeDTO> getAllNotices() {
|
||||
return noticeService.getAllNotices();
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public ResponseEntity<String> addNotice(@RequestBody NoticeDTO dto) {
|
||||
if (!clientService.clientExists(dto.getClientId())) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
||||
}
|
||||
|
||||
noticeService.addNotice(noticeService.fromDTO(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("/bulk_add")
|
||||
public ResponseEntity<String> addNotices(@RequestBody List<NoticeDTO> notices_list) {
|
||||
ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.CREATED);
|
||||
List<String> errors = new ArrayList<>();
|
||||
boolean isError = false;
|
||||
|
||||
if (notices_list.isEmpty()) {
|
||||
return response.status(HttpStatus.BAD_REQUEST).body("Lista ogłoszeń jest pusta.");
|
||||
}
|
||||
|
||||
for (NoticeDTO dto : notices_list) {
|
||||
if (!clientService.clientExists(dto.getClientId())) {
|
||||
isError = true;
|
||||
errors.add(dto.getClientId().toString());
|
||||
} else {
|
||||
if(!isError){
|
||||
noticeService.addNotice(noticeService.fromDTO(dto));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(isError) {
|
||||
return response.status(HttpStatus.BAD_REQUEST).body("Nie znaleziono klientów: " + errors);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@PutMapping("/edit/{id}")
|
||||
public ResponseEntity<Object> editNotice(@PathVariable("id") long id, @RequestBody NoticeDTO dto) {
|
||||
if (noticeService.noticeExists(id)) {
|
||||
try {
|
||||
return new ResponseEntity<>(noticeService.updateNotice(id, dto), HttpStatus.OK);
|
||||
} catch (EntityNotFoundException e) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Nie znaleziono ogłoszenia o ID: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ResponseEntity deleteNotice(@PathVariable("id") long id) {
|
||||
if(noticeService.noticeExists(id)) {
|
||||
noticeService.deleteNotice(id);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// @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);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
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;
|
||||
private String image;
|
||||
private Role role;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "attribute_values")
|
||||
public class AttributeValues {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_attribute")
|
||||
private Attributes attribute;
|
||||
|
||||
private String value;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "attributes")
|
||||
public class Attributes {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idAttribute;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "attribute", cascade = CascadeType.ALL)
|
||||
private List<AttributeValues> attributeValues;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "attributes_notice")
|
||||
public class AttributesNotice {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_notice")
|
||||
private Notice notice;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_value")
|
||||
private AttributeValues attributeValue;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "global_variables")
|
||||
public class GlobalVariables {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private String value;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Status;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class Orders {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idOrder;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_user")
|
||||
private Client client;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_notice")
|
||||
private Notice notice;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Status status;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Status;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "payments")
|
||||
public class Payments {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idPayment;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_order")
|
||||
private Orders order;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_notice")
|
||||
private Notice notice;
|
||||
|
||||
private Double noticePublishPrice;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Status status;
|
||||
|
||||
private String sessionId;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
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;
|
||||
package _11.asktpk.artisanconnectbackend.repository;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.Model.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package _11.asktpk.artisanconnectbackend.service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ClientService {
|
||||
private final ClientRepository clientRepository;
|
||||
|
||||
public ClientService(ClientRepository clientRepository) {
|
||||
this.clientRepository = clientRepository;
|
||||
}
|
||||
|
||||
public ClientDTO toDto(Client client) {
|
||||
ClientDTO dto = new ClientDTO();
|
||||
|
||||
dto.setId(client.getId());
|
||||
dto.setFirstName(client.getFirstName());
|
||||
dto.setLastName(client.getLastName());
|
||||
dto.setEmail(client.getEmail());
|
||||
dto.setRole(client.getRole());
|
||||
dto.setImage(client.getImage());
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
public Client fromDto(ClientDTO dto) {
|
||||
Client client = new Client();
|
||||
|
||||
client.setId(dto.getId());
|
||||
client.setFirstName(dto.getFirstName());
|
||||
client.setLastName(dto.getLastName());
|
||||
client.setEmail(dto.getEmail());
|
||||
client.setRole(dto.getRole());
|
||||
client.setImage(dto.getImage());
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
public List<ClientDTO> getAllClients() {
|
||||
List<Client> clients = clientRepository.findAll();
|
||||
return clients.stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
public Client getClientById(Long id) {
|
||||
return clientRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public boolean clientExists(Long id) {
|
||||
return clientRepository.existsById(id);
|
||||
}
|
||||
|
||||
public ClientDTO updateClient(long id, ClientDTO clientDTO) {
|
||||
Client existingClient = clientRepository.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));
|
||||
|
||||
existingClient.setEmail(clientDTO.getEmail());
|
||||
existingClient.setFirstName(clientDTO.getFirstName());
|
||||
existingClient.setLastName(clientDTO.getLastName());
|
||||
existingClient.setImage(clientDTO.getImage());
|
||||
existingClient.setRole(clientDTO.getRole());
|
||||
|
||||
return toDto(clientRepository.save(existingClient));
|
||||
}
|
||||
|
||||
public void deleteClient(Long id) {
|
||||
clientRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
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.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 fromDTO(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);
|
||||
}
|
||||
|
||||
public boolean noticeExists(Long id) {
|
||||
return noticeRepository.existsById(id);
|
||||
}
|
||||
|
||||
public NoticeDTO updateNotice(Long id, NoticeDTO dto) {
|
||||
Notice existingNotice = noticeRepository.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));
|
||||
|
||||
existingNotice.setTitle(dto.getTitle());
|
||||
existingNotice.setDescription(dto.getDescription());
|
||||
existingNotice.setPrice(dto.getPrice());
|
||||
existingNotice.setCategory(dto.getCategory());
|
||||
existingNotice.setImages(dto.getImages());
|
||||
existingNotice.setStatus(dto.getStatus());
|
||||
existingNotice.setAttributesNotices(dto.getAttributesNotices());
|
||||
|
||||
if (dto.getClientId() != null && !dto.getClientId().equals(existingNotice.getClient().getId())) {
|
||||
Client client = clientRepository.findById(dto.getClientId())
|
||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono klienta o ID: " + dto.getClientId()));
|
||||
existingNotice.setClient(client);
|
||||
}
|
||||
|
||||
return toDTO(noticeRepository.save(existingNotice));
|
||||
}
|
||||
|
||||
public void deleteNotice(Long id) {
|
||||
if (noticeExists(id)) {
|
||||
noticeRepository.deleteById(id);
|
||||
} else {
|
||||
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package _11.asktpk.artisanconnectbackend.utils;
|
||||
|
||||
public class Enums {
|
||||
public enum Role {
|
||||
ADMIN, USER
|
||||
}
|
||||
|
||||
public enum Category {
|
||||
Electronics, Artwork, Kitchen, Buildings, Home, Fashion // Replace with actual categories
|
||||
}
|
||||
|
||||
public enum Status {
|
||||
ACTIVE, INACTIVE // Replace with actual statuses
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
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
|
||||
|
||||
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
|
||||
7
src/main/resources/sql/data.sql
Normal file
7
src/main/resources/sql/data.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
INSERT INTO clients (email, first_name, image, last_name, password, role)
|
||||
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');
|
||||
@@ -1,43 +1,33 @@
|
||||
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"));
|
||||
Boolean isRecordAvailable = postgresDatabase.get().size() > 0;
|
||||
if(isRecordAvailable) {
|
||||
logger.info("The record is available in the database");
|
||||
} else {
|
||||
logger.error("The record is not available in the database");
|
||||
}
|
||||
assert isRecordAvailable;
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllNotices() throws IOException {
|
||||
OkHttpClient client = new OkHttpClient().newBuilder()
|
||||
.build();
|
||||
MediaType mediaType = MediaType.parse("text/plain");
|
||||
Request request = new Request.Builder()
|
||||
.url("http://localhost:8080/api/v1/notices/all")
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
}
|
||||
// @Test
|
||||
// void testPostgresDatabase() {
|
||||
// postgresDatabase.add(new Notice("Test Notice", "Username", "Test Description"));
|
||||
// Boolean isRecordAvailable = postgresDatabase.get().size() > 0;
|
||||
// if(isRecordAvailable) {
|
||||
// logger.info("The record is available in the database");
|
||||
// } else {
|
||||
// logger.error("The record is not available in the database");
|
||||
// }
|
||||
// assert isRecordAvailable;
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void getAllNotices() throws IOException {
|
||||
// OkHttpClient client = new OkHttpClient().newBuilder()
|
||||
// .build();
|
||||
// MediaType mediaType = MediaType.parse("text/plain");
|
||||
// Request request = new Request.Builder()
|
||||
// .url("http://localhost:8080/api/v1/notices/all")
|
||||
// .build();
|
||||
// Response response = client.newCall(request).execute();
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user