6 Commits

22 changed files with 498 additions and 74 deletions

View File

@@ -73,6 +73,11 @@
<artifactId>jakarta.validation-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -3,6 +3,7 @@ package _11.asktpk.artisanconnectbackend.controller;
import _11.asktpk.artisanconnectbackend.dto.RequestResponseDTO;
import _11.asktpk.artisanconnectbackend.service.ImageService;
import _11.asktpk.artisanconnectbackend.service.NoticeService;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
@@ -63,18 +64,17 @@ public class ImageController {
}
@GetMapping("/list/{id}")
public ResponseEntity<List<String>> getImagesNamesList(@PathVariable("id") Long noticeId) {
if(noticeId == null) {
return ResponseEntity.badRequest().body(Collections.singletonList("Notice ID is invalid or does not exist."));
}
public ResponseEntity<?> getImagesNamesList(@PathVariable("id") Long noticeId) {
List<String> result;
try {
noticeService.getNoticeById(noticeId);
result = imageService.getImagesList(noticeId);
return ResponseEntity.ok(result);
} catch (EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RequestResponseDTO(e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Collections.singletonList(e.getMessage()));
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RequestResponseDTO(e.getMessage()));
}
return ResponseEntity.ok(result);
}
@DeleteMapping("/delete/{filename}")

View File

@@ -0,0 +1,51 @@
package _11.asktpk.artisanconnectbackend.controller;
import _11.asktpk.artisanconnectbackend.dto.*;
import _11.asktpk.artisanconnectbackend.entities.Order;
import _11.asktpk.artisanconnectbackend.service.ClientService;
import _11.asktpk.artisanconnectbackend.service.OrderService;
import _11.asktpk.artisanconnectbackend.service.PaymentService;
import _11.asktpk.artisanconnectbackend.utils.Enums;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/v1/orders")
public class OrderController {
private final OrderService orderService;
private final PaymentService paymentService;
public OrderController(OrderService orderService, PaymentService paymentService) {
this.orderService = orderService;
this.paymentService = paymentService;
}
@PostMapping("/add")
public ResponseEntity addClient(@RequestBody OrderDTO orderDTO) {
return new ResponseEntity<>(orderService.addOrder(orderDTO), HttpStatus.CREATED);
}
@PutMapping("/changeStatus")
public ResponseEntity changeStatus(@RequestBody OrderStatusDTO orderStatusDTO) {
return new ResponseEntity<>(orderService.changeOrderStatus(orderStatusDTO.getId(),orderStatusDTO.getStatus()), HttpStatus.OK);
}
@PostMapping("/token")
public ResponseEntity<?> fetchToken() {
Order order = orderService.getOrderById(1L);
OAuthPaymentResponseDTO authPaymentDTO= paymentService.getOAuthToken();
TransactionPaymentRequestDTO.Payer payer = new TransactionPaymentRequestDTO.Payer(
"patryk@test.pl", "Patryk Test");
String paymentDescription = order.getOrderType() == Enums.OrderType.ACTIVATION ? "Aktywacja ogłoszenia" : "Podbicie ogłoszenia";
paymentDescription += order.getNotice().getTitle();
TransactionPaymentRequestDTO request = new TransactionPaymentRequestDTO(
order.getAmount(), paymentDescription, payer);
String response = paymentService.createTransaction(order,authPaymentDTO.getAccess_token(), request);
System.out.println(response);
return ResponseEntity.ok(authPaymentDTO.getAccess_token());
}
}

View File

@@ -24,10 +24,10 @@ public class WishlistController {
this.noticeService = noticeService;
}
@PostMapping("/toggle")
public ResponseEntity<RequestResponseDTO> toggleWishlist(@RequestBody WishlistDTO wishlistDTO) {
Long noticeId = wishlistDTO.getNoticeId();
Long clientId = wishlistDTO.getClientId();
@PostMapping("/toggle/{noticeId}")
public ResponseEntity<RequestResponseDTO> toggleWishlist(@PathVariable Long noticeId) {
Long clientId = 1L;
NoticeDTO noticeDTO = noticeService.getNoticeById(noticeId);
if (noticeDTO == null) {
return ResponseEntity.badRequest().body(new RequestResponseDTO("Notice not found"));

View File

@@ -0,0 +1,15 @@
package _11.asktpk.artisanconnectbackend.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class OAuthPaymentResponseDTO {
private long issued_at;
private String scope;
private String token_type;
private int expires_in;
private String client_id;
private String access_token;
}

View File

@@ -0,0 +1,13 @@
package _11.asktpk.artisanconnectbackend.dto;
import _11.asktpk.artisanconnectbackend.utils.Enums;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class OrderDTO {
private Long clientId;
private Long noticeId;
private Enums.OrderType orderType;
}

View File

@@ -0,0 +1,12 @@
package _11.asktpk.artisanconnectbackend.dto;
import _11.asktpk.artisanconnectbackend.utils.Enums;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class OrderStatusDTO {
public long id;
public Enums.OrderStatus status;
}

View File

@@ -0,0 +1,25 @@
package _11.asktpk.artisanconnectbackend.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class TransactionPaymentRequestDTO {
private double amount;
private String description;
private Payer payer;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class Payer {
private String email;
private String name;
}
}

View File

@@ -0,0 +1,59 @@
package _11.asktpk.artisanconnectbackend.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class TransactionPaymentResponseDTO {
private String result;
private String requestId;
private String transactionId;
private String title;
private String posId;
private String status;
private DateInfo date;
private double amount;
private String currency;
private String description;
private String hiddenDescription;
private Payer payer;
private Payments payments;
private String transactionPaymentUrl;
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public static class DateInfo {
private String creation;
private String realization;
}
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Payer {
private String payerId;
private String email;
private String name;
private String phone;
private String address;
private String city;
private String country;
private String postalCode;
}
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Payments {
private String status;
private String method;
private double amountPaid;
private DateInfo date;
}
}

View File

@@ -33,5 +33,5 @@ public class Client {
// private List<Notice> notices;
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
private List<Orders> orders;
private List<Order> orders;
}

View File

@@ -39,8 +39,8 @@ public class Notice {
private List<AttributesNotice> attributesNotices;
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
private List<Orders> orders;
private List<Order> orders;
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
private List<Payments> payments;
// @OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
// private List<Payment> payment;
}

View File

@@ -0,0 +1,43 @@
package _11.asktpk.artisanconnectbackend.entities;
import _11.asktpk.artisanconnectbackend.utils.Enums;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
@Entity
@Table(name = "orders")
@Getter
@Setter
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "id_client")
private Client client;
@ManyToOne
@JoinColumn(name = "id_notice")
private Notice notice;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Enums.OrderType orderType;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Enums.OrderStatus status;
@Column(nullable = false)
private Double amount;
@Column(nullable = false)
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}

View File

@@ -1,26 +0,0 @@
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
}

View File

@@ -0,0 +1,30 @@
package _11.asktpk.artisanconnectbackend.entities;
import _11.asktpk.artisanconnectbackend.utils.Enums;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
@Entity
@Table(name = "payment")
@Getter @Setter
public class Payment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPayment;
@ManyToOne
@JoinColumn(name = "id_order")
private Order order;
private Double amount;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Enums.PaymentStatus status;
private String transactionPaymentUrl;
private String transactionId;
}

View File

@@ -1,30 +0,0 @@
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
}

View File

@@ -0,0 +1,11 @@
package _11.asktpk.artisanconnectbackend.repository;
import _11.asktpk.artisanconnectbackend.entities.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
}

View File

@@ -0,0 +1,10 @@
package _11.asktpk.artisanconnectbackend.repository;
import _11.asktpk.artisanconnectbackend.entities.Payment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PaymentRepository extends JpaRepository<Payment, Long> {
}

View File

@@ -5,8 +5,10 @@ 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.service.WishlistService;
import jakarta.persistence.EntityNotFoundException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@@ -16,15 +18,21 @@ import java.util.Optional;
@Service
public class NoticeService {
private static final Logger logger = LogManager.getLogger(NoticeService.class);
@Value("${file.upload-dir}")
private String uploadDir;
private final NoticeRepository noticeRepository;
private final ClientRepository clientRepository;
private final WishlistService wishlistService;
private final ImageService imageService;
public NoticeService(NoticeRepository noticeRepository, ClientRepository clientRepository, WishlistService wishlistService) {
public NoticeService(NoticeRepository noticeRepository, ClientRepository clientRepository, WishlistService wishlistService, ImageService imageService) {
this.noticeRepository = noticeRepository;
this.clientRepository = clientRepository;
this.wishlistService = wishlistService;
this.imageService = imageService;
}
public Notice fromDTO(NoticeDTO dto) {
@@ -116,6 +124,22 @@ public class NoticeService {
public void deleteNotice(Long id) {
if (noticeExists(id)) {
noticeRepository.deleteById(id);
List<String> imagesList = new ArrayList<>();
try {
imagesList = imageService.getImagesList(id);
} catch (Exception e) {
logger.info("There weren't any images for notice with ID: " + id + ". Skipping deletion of images. Message: " + e.getMessage());
}
try {
for (String imageName : imagesList) {
imageService.deleteImage(uploadDir, imageName);
}
} catch (Exception e) {
logger.info("There were some issues while deleting images for notice with ID: " + id + ". Message: " + e.getMessage());
}
} else {
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
}

View File

@@ -0,0 +1,79 @@
package _11.asktpk.artisanconnectbackend.service;
import _11.asktpk.artisanconnectbackend.dto.OrderDTO;
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.repository.OrderRepository;
import _11.asktpk.artisanconnectbackend.utils.Enums;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import _11.asktpk.artisanconnectbackend.entities.Order;
import java.time.LocalDateTime;
@Service
public class OrderService {
private final OrderRepository orderRepository;
private final ClientRepository clientRepository;
private final NoticeRepository noticeRepository;
@Autowired
public OrderService(OrderRepository orderRepository, ClientRepository clientRepository, NoticeRepository noticeRepository) {
this.orderRepository = orderRepository;
this.clientRepository = clientRepository;
this.noticeRepository = noticeRepository;
}
public Order fromDTO(OrderDTO orderDTO) {
Order order = new Order();
order.setOrderType(orderDTO.getOrderType());
order.setStatus(Enums.OrderStatus.PENDING);
if(orderDTO.getOrderType() == Enums.OrderType.ACTIVATION){
order.setAmount(10.00);
}else{
order.setAmount(8.00);
}
order.setCreatedAt(LocalDateTime.now()
);
order.setUpdatedAt(LocalDateTime.now()
);
Client client = clientRepository.findById(orderDTO.getClientId())
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono klienta o ID: " + orderDTO.getClientId()));
order.setClient(client);
Notice notice = noticeRepository.findById(orderDTO.getNoticeId())
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + orderDTO.getNoticeId()));
order.setNotice(notice);
return order;
}
public Long addOrder(OrderDTO orderDTO) {
Order order = fromDTO(orderDTO);
return orderRepository.save(order).getId();
}
public Long changeOrderStatus(Long id, Enums.OrderStatus status) {
Order order = orderRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Nie znaleziono zamówienia o ID: " + id));
order.setStatus(status);
order = orderRepository.save(order);
return order.getId();
}
public Order getOrderById(Long id) {
return orderRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Nie znaleziono zamówienia o ID: " + id));
}
}

View File

@@ -0,0 +1,81 @@
package _11.asktpk.artisanconnectbackend.service;
import _11.asktpk.artisanconnectbackend.dto.OAuthPaymentResponseDTO;
import _11.asktpk.artisanconnectbackend.dto.TransactionPaymentRequestDTO;
import _11.asktpk.artisanconnectbackend.dto.TransactionPaymentResponseDTO;
import _11.asktpk.artisanconnectbackend.entities.Order;
import _11.asktpk.artisanconnectbackend.entities.Payment;
import _11.asktpk.artisanconnectbackend.repository.PaymentRepository;
import _11.asktpk.artisanconnectbackend.utils.Enums;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Service
public class PaymentService {
private final WebClient webClient;
private final String clientId;
private final String clientSecret;
private final String authUrl;
private final String transactionUrl;
private final PaymentRepository paymentRepository;
public PaymentService(
WebClient.Builder webClientBuilder,
@Value("${tpay.clientId}") String clientId,
@Value("${tpay.clientSecret}") String clientSecret,
@Value("${tpay.authUrl}") String authUrl,
@Value("${tpay.transactionUrl}") String transactionUrl,
PaymentRepository paymentRepository
) {
this.webClient = webClientBuilder.baseUrl(authUrl).build();
this.clientId = clientId;
this.clientSecret = clientSecret;
this.authUrl = authUrl;
this.transactionUrl = transactionUrl;
this.paymentRepository = paymentRepository;
}
public OAuthPaymentResponseDTO getOAuthToken() {
return webClient.post()
.uri("")
.contentType(MediaType.MULTIPART_FORM_DATA)
.header("accept", "application/json")
.body(BodyInserters.fromMultipartData("client_id", clientId)
.with("client_secret", clientSecret))
.retrieve()
.bodyToMono(OAuthPaymentResponseDTO.class)
.block();
}
public String createTransaction(Order order, String accessToken, TransactionPaymentRequestDTO transactionPaymentRequestDTO) {
TransactionPaymentResponseDTO response = webClient.post()
.uri(transactionUrl)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(transactionPaymentRequestDTO)
.retrieve()
.bodyToMono(TransactionPaymentResponseDTO.class)
.block();
if (response != null && "success".equalsIgnoreCase(response.getResult())) {
Payment payment = new Payment();
payment.setOrder(order);
payment.setAmount(response.getAmount());
payment.setStatus(Enums.PaymentStatus.PENDING);
payment.setTransactionId(response.getTransactionId());
payment.setTransactionPaymentUrl(response.getTransactionPaymentUrl());
paymentRepository.save(payment);
return response.getTransactionPaymentUrl();
}
return null;
}
}

View File

@@ -43,4 +43,20 @@ public class Enums {
public enum Status {
ACTIVE, INACTIVE
}
public enum OrderType {
ACTIVATION,
BOOST
}
public enum OrderStatus {
PENDING, COMPLETED, CANCELLED
}
public enum PaymentStatus{
PENDING, CORRECT, INCORRECT
}
}

View File

@@ -16,4 +16,10 @@ spring.jpa.hibernate.ddl-auto=create-drop
file.upload-dir=/Users/andsol/Desktop/uploads
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.max-request-size=10MB
tpay.clientId = 01JQKC048X62ST9V59HNRSXD92-01JQKC2CQHPYXQFSFX8BKC24BX
tpay.clientSecret = 44898642be53381cdcc47f3e44bf5a15e592f5d270fc3a6cf6fb81a8b8ebffb9
tpay.authUrl = https://openapi.sandbox.tpay.com/oauth/auth
tpay.transactionUrl = https://openapi.sandbox.tpay.com/transactions