Compare commits
10 Commits
initNotice
...
0d32b4a495
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d32b4a495 | |||
| 293be1d46e | |||
|
|
9b64dc8da8 | ||
|
|
12cb37127b | ||
|
|
281cc627de | ||
|
|
6363f966f6 | ||
|
|
c642f6f87b | ||
|
|
65524d0f25 | ||
|
|
71fdf1640a | ||
| 8fae9f1e55 |
31
pom.xml
31
pom.xml
@@ -73,6 +73,37 @@
|
|||||||
<artifactId>jakarta.validation-api</artifactId>
|
<artifactId>jakarta.validation-api</artifactId>
|
||||||
<version>3.1.0</version>
|
<version>3.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-api</artifactId>
|
||||||
|
<version>0.11.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-impl</artifactId>
|
||||||
|
<version>0.11.5</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-jackson</artifactId>
|
||||||
|
<version>0.11.5</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security</groupId>
|
||||||
|
<artifactId>spring-security-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class AppConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.config;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.security.JwtRequestFilter;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||||
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
|
public class SecurityConfig {
|
||||||
|
|
||||||
|
private final JwtRequestFilter jwtRequestFilter;
|
||||||
|
|
||||||
|
public SecurityConfig(JwtRequestFilter jwtRequestFilter) {
|
||||||
|
this.jwtRequestFilter = jwtRequestFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
|
http
|
||||||
|
.cors(cors -> cors.configure(http))
|
||||||
|
.csrf(AbstractHttpConfigurer::disable)
|
||||||
|
.authorizeHttpRequests(auth -> auth
|
||||||
|
.requestMatchers("/api/v1/auth/**").permitAll()
|
||||||
|
.requestMatchers("/api/v1/admin/**").hasRole("ADMIN")
|
||||||
|
.anyRequest().authenticated())
|
||||||
|
.sessionManagement(session -> session
|
||||||
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
||||||
|
|
||||||
|
http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|
||||||
|
return http.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.controller;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.*;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||||
|
import _11.asktpk.artisanconnectbackend.security.JwtUtil;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/auth")
|
||||||
|
public class AuthController {
|
||||||
|
|
||||||
|
private final ClientService clientService;
|
||||||
|
private final JwtUtil jwtUtil;
|
||||||
|
|
||||||
|
public AuthController(ClientService clientService, JwtUtil jwtUtil) {
|
||||||
|
this.clientService = clientService;
|
||||||
|
this.jwtUtil = jwtUtil;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
public ResponseEntity<AuthResponseDTO> login(@RequestBody AuthRequestDTO authRequestDTO) {
|
||||||
|
if (clientService.checkClientCredentials(authRequestDTO)) {
|
||||||
|
Client client = clientService.getClientByEmail(authRequestDTO.getEmail());
|
||||||
|
Long userId = client.getId();
|
||||||
|
String userRole = client.getRole().getRole();
|
||||||
|
|
||||||
|
String token = jwtUtil.generateToken(client.getEmail(), userRole, userId);
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.OK)
|
||||||
|
.body(new AuthResponseDTO(userId, userRole, token));
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/register")
|
||||||
|
public ResponseEntity<AuthResponseDTO> register(@RequestBody ClientRegistrationDTO clientDTO) {
|
||||||
|
if (clientService.getClientByEmail(clientDTO.getEmail()) != null) {
|
||||||
|
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientDTO savedClient = clientService.registerClient(clientDTO);
|
||||||
|
|
||||||
|
String token = jwtUtil.generateToken(
|
||||||
|
savedClient.getEmail(),
|
||||||
|
savedClient.getRole().getRole(),
|
||||||
|
savedClient.getId()
|
||||||
|
);
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED)
|
||||||
|
.body(new AuthResponseDTO(
|
||||||
|
savedClient.getId(),
|
||||||
|
savedClient.getRole().getRole(),
|
||||||
|
token
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/logout")
|
||||||
|
public ResponseEntity<RequestResponseDTO> logout(HttpServletRequest request) {
|
||||||
|
String authHeader = request.getHeader("Authorization");
|
||||||
|
|
||||||
|
if (authHeader != null && authHeader.startsWith("Bearer ")) {
|
||||||
|
String token = authHeader.substring(7);
|
||||||
|
jwtUtil.blacklistToken(token);
|
||||||
|
return ResponseEntity.ok(new RequestResponseDTO("Successfully logged out"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RequestResponseDTO("Invalid token"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package _11.asktpk.artisanconnectbackend.controller;
|
|||||||
import _11.asktpk.artisanconnectbackend.dto.RequestResponseDTO;
|
import _11.asktpk.artisanconnectbackend.dto.RequestResponseDTO;
|
||||||
import _11.asktpk.artisanconnectbackend.service.ImageService;
|
import _11.asktpk.artisanconnectbackend.service.ImageService;
|
||||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@@ -63,18 +64,17 @@ public class ImageController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/list/{id}")
|
@GetMapping("/list/{id}")
|
||||||
public ResponseEntity<List<String>> getImagesNamesList(@PathVariable("id") Long noticeId) {
|
public ResponseEntity<?> getImagesNamesList(@PathVariable("id") Long noticeId) {
|
||||||
if(noticeId == null) {
|
|
||||||
return ResponseEntity.badRequest().body(Collections.singletonList("Notice ID is invalid or does not exist."));
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> result;
|
List<String> result;
|
||||||
try {
|
try {
|
||||||
|
noticeService.getNoticeById(noticeId);
|
||||||
result = imageService.getImagesList(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) {
|
} 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}")
|
@DeleteMapping("/delete/{filename}")
|
||||||
|
|||||||
@@ -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(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.controller;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||||
|
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.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.util.DigestUtils;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/payments")
|
||||||
|
public class PaymentController {
|
||||||
|
|
||||||
|
@Value("${tpay.securityCode}")
|
||||||
|
private String sellerSecurityCode;
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(PaymentController.class);
|
||||||
|
|
||||||
|
private final PaymentRepository paymentRepository;
|
||||||
|
|
||||||
|
public PaymentController(PaymentRepository paymentRepository) {
|
||||||
|
this.paymentRepository = paymentRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = "/notification", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||||
|
public ResponseEntity<String> handleTpayNotification(@RequestParam Map<String, String> params) {
|
||||||
|
log.info("=== ODEBRANO NOTYFIKACJĘ Tpay ===");
|
||||||
|
log.info("Parametry:\n{}", paramsToLogString(params));
|
||||||
|
|
||||||
|
String id = params.get("id");
|
||||||
|
String trId = params.get("tr_id");
|
||||||
|
String trAmount = params.get("tr_amount");
|
||||||
|
String trCrc = params.get("tr_crc");
|
||||||
|
String md5sum = params.get("md5sum");
|
||||||
|
String trStatus = params.get("tr_status");
|
||||||
|
|
||||||
|
String expectedMd5 = DigestUtils.md5DigestAsHex(
|
||||||
|
(id + trId + trAmount + trCrc + sellerSecurityCode).getBytes()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!expectedMd5.equals(md5sum)) {
|
||||||
|
log.warn("❌ Błędna suma kontrolna! Otrzymano: {}, Oczekiwano: {}", md5sum, expectedMd5);
|
||||||
|
return ResponseEntity.status(400).body("INVALID CHECKSUM");
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<Payment> optionalPayment = paymentRepository.findByTransactionId(trId);
|
||||||
|
if (optionalPayment.isPresent()) {
|
||||||
|
Payment payment = optionalPayment.get();
|
||||||
|
|
||||||
|
if ("true".equalsIgnoreCase(trStatus) || "PAID".equalsIgnoreCase(trStatus)) {
|
||||||
|
log.info("✅ Transakcja opłacona: tr_id={}, kwota={}", trId, params.get("tr_paid"));
|
||||||
|
payment.setStatus(Enums.PaymentStatus.CORRECT);
|
||||||
|
|
||||||
|
if (payment.getOrder() != null) {
|
||||||
|
Order order = payment.getOrder();
|
||||||
|
order.setStatus(Enums.OrderStatus.COMPLETED);
|
||||||
|
Notice notice = order.getNotice();
|
||||||
|
if (order.getOrderType() == Enums.OrderType.ACTIVATION) {
|
||||||
|
notice.setStatus(Enums.Status.ACTIVE);
|
||||||
|
} else if (order.getOrderType() == Enums.OrderType.BOOST) {
|
||||||
|
notice.setPublishDate(LocalDateTime.now());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if ("false".equalsIgnoreCase(trStatus)) {
|
||||||
|
log.warn("❌ Transakcja nieudana: {}", trId);
|
||||||
|
payment.setStatus(Enums.PaymentStatus.INCORRECT);
|
||||||
|
|
||||||
|
if (payment.getOrder() != null) {
|
||||||
|
payment.getOrder().setStatus(Enums.OrderStatus.CANCELLED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
paymentRepository.save(payment);
|
||||||
|
} else {
|
||||||
|
log.warn("⚠️ Brak płatności o tr_id={}", trId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok("TRUE");
|
||||||
|
}
|
||||||
|
|
||||||
|
private String paramsToLogString(Map<String, String> params) {
|
||||||
|
return params.entrySet().stream()
|
||||||
|
.map(e -> e.getKey() + " = " + e.getValue())
|
||||||
|
.collect(Collectors.joining("\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,6 @@ import java.util.Map;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/vars")
|
@RequestMapping("/api/v1/vars")
|
||||||
public class VariablesController {
|
public class VariablesController {
|
||||||
|
|
||||||
@GetMapping("/categories")
|
@GetMapping("/categories")
|
||||||
public List<CategoriesDTO> getAllVariables() {
|
public List<CategoriesDTO> getAllVariables() {
|
||||||
List<CategoriesDTO> categoriesDTOList = new ArrayList<>();
|
List<CategoriesDTO> categoriesDTOList = new ArrayList<>();
|
||||||
@@ -31,10 +30,4 @@ public class VariablesController {
|
|||||||
public List<Enums.Status> getAllStatuses() {
|
public List<Enums.Status> getAllStatuses() {
|
||||||
return List.of(Enums.Status.values());
|
return List.of(Enums.Status.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/roles")
|
|
||||||
public List<Enums.Role> getAllRoles() {
|
|
||||||
return List.of(Enums.Role.values());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.dto;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter @Setter
|
||||||
|
public class AuthRequestDTO {
|
||||||
|
private String email;
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter @Setter @AllArgsConstructor
|
||||||
|
public class AuthResponseDTO {
|
||||||
|
private Long user_id;
|
||||||
|
private String user_role;
|
||||||
|
private String token;
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import lombok.Setter;
|
|||||||
|
|
||||||
import jakarta.validation.constraints.Email;
|
import jakarta.validation.constraints.Email;
|
||||||
|
|
||||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Role;
|
import _11.asktpk.artisanconnectbackend.entities.Role;
|
||||||
|
|
||||||
@Getter @Setter
|
@Getter @Setter
|
||||||
public class ClientDTO {
|
public class ClientDTO {
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter @Setter
|
||||||
|
public class ClientRegistrationDTO {
|
||||||
|
@Email
|
||||||
|
@NotBlank
|
||||||
|
private String email;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package _11.asktpk.artisanconnectbackend.entities;
|
package _11.asktpk.artisanconnectbackend.entities;
|
||||||
|
|
||||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Role;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.hibernate.annotations.CreationTimestamp;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@@ -24,14 +24,15 @@ public class Client {
|
|||||||
|
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
private String image; // Optional field
|
private String image;
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
@ManyToOne(cascade = CascadeType.ALL)
|
||||||
|
@JoinColumn(name = "role_id", referencedColumnName = "id")
|
||||||
private Role role;
|
private Role role;
|
||||||
|
|
||||||
// @OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
|
|
||||||
// private List<Notice> notices;
|
|
||||||
|
|
||||||
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
|
||||||
private List<Orders> orders;
|
private List<Order> orders;
|
||||||
|
|
||||||
|
@CreationTimestamp
|
||||||
|
private Date createdAt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -39,8 +39,8 @@ public class Notice {
|
|||||||
private List<AttributesNotice> attributesNotices;
|
private List<AttributesNotice> attributesNotices;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||||
private List<Orders> orders;
|
private List<Order> orders;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
// @OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||||
private List<Payments> payments;
|
// private List<Payment> payment;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.entities;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "roles")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class Role {
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
@Column(name="rolename")
|
||||||
|
private String role;
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
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;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface ClientRepository extends JpaRepository<Client, Long> {
|
public interface ClientRepository extends JpaRepository<Client, Long> {
|
||||||
|
Client findByEmail(String email);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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> {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.repository;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Payment;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface PaymentRepository extends JpaRepository<Payment, Long> {
|
||||||
|
Optional<Payment> findByTransactionId(String transactionId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Role;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface RolesRepository extends JpaRepository<Role, String> {
|
||||||
|
Role findRoleById(Long id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.security;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class JwtRequestFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
private final JwtUtil jwtUtil;
|
||||||
|
|
||||||
|
public JwtRequestFilter(JwtUtil jwtUtil) {
|
||||||
|
this.jwtUtil = jwtUtil;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain chain)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
|
||||||
|
final String authorizationHeader = request.getHeader("Authorization");
|
||||||
|
|
||||||
|
String email = null;
|
||||||
|
String jwt = null;
|
||||||
|
|
||||||
|
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||||
|
jwt = authorizationHeader.substring(7);
|
||||||
|
|
||||||
|
if (jwtUtil.isBlacklisted(jwt)) {
|
||||||
|
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
email = jwtUtil.extractEmail(jwt);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (email != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
||||||
|
String role = jwtUtil.extractRole(jwt);
|
||||||
|
|
||||||
|
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
|
||||||
|
email, null, Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + role)));
|
||||||
|
|
||||||
|
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||||
|
SecurityContextHolder.getContext().setAuthentication(authToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
// logger.info("Token of user " + jwtUtil.extractEmail(jwt) + (jwtUtil.isTokenExpired(jwt) ? " is expired" : " is not expired"));
|
||||||
|
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.security;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class JwtUtil {
|
||||||
|
|
||||||
|
@Value("${jwt.secret:defaultSecretKeyNeedsToBeAtLeast32BytesLong}")
|
||||||
|
private String secret;
|
||||||
|
|
||||||
|
@Value("${jwt.expiration}")
|
||||||
|
private long expiration;
|
||||||
|
|
||||||
|
// sterowanie tokenami wygasnietymi
|
||||||
|
private final Set<String> blacklistedTokens = ConcurrentHashMap.newKeySet();
|
||||||
|
|
||||||
|
public void blacklistToken(String token) {
|
||||||
|
blacklistedTokens.add(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBlacklisted(String token) {
|
||||||
|
return blacklistedTokens.contains(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private SecretKey getSigningKey() {
|
||||||
|
return Keys.hmacShaKeyFor(secret.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String generateToken(String email, String role, Long userId) {
|
||||||
|
Map<String, Object> claims = new HashMap<>();
|
||||||
|
claims.put("role", role);
|
||||||
|
claims.put("userId", userId);
|
||||||
|
return createToken(claims, email);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createToken(Map<String, Object> claims, String subject) {
|
||||||
|
return Jwts.builder()
|
||||||
|
.setClaims(claims)
|
||||||
|
.setSubject(subject)
|
||||||
|
.setIssuedAt(new Date())
|
||||||
|
.setExpiration(new Date(System.currentTimeMillis() + expiration))
|
||||||
|
.signWith(getSigningKey(), SignatureAlgorithm.HS256)
|
||||||
|
.compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String extractEmail(String token) {
|
||||||
|
return extractClaim(token, Claims::getSubject);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String extractRole(String token) {
|
||||||
|
return extractAllClaims(token).get("role", String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
|
||||||
|
final Claims claims = extractAllClaims(token);
|
||||||
|
return claimsResolver.apply(claims);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Claims extractAllClaims(String token) {
|
||||||
|
return Jwts.parserBuilder()
|
||||||
|
.setSigningKey(getSigningKey())
|
||||||
|
.build()
|
||||||
|
.parseClaimsJws(token)
|
||||||
|
.getBody();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
package _11.asktpk.artisanconnectbackend.service;
|
package _11.asktpk.artisanconnectbackend.service;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.AuthRequestDTO;
|
||||||
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.ClientRegistrationDTO;
|
||||||
import _11.asktpk.artisanconnectbackend.entities.Client;
|
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||||
|
import _11.asktpk.artisanconnectbackend.repository.RolesRepository;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -11,9 +15,13 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class ClientService {
|
public class ClientService {
|
||||||
private final ClientRepository clientRepository;
|
private final ClientRepository clientRepository;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
private final RolesRepository rolesRepository;
|
||||||
|
|
||||||
public ClientService(ClientRepository clientRepository) {
|
public ClientService(ClientRepository clientRepository, PasswordEncoder passwordEncoder, RolesRepository rolesRepository) {
|
||||||
this.clientRepository = clientRepository;
|
this.clientRepository = clientRepository;
|
||||||
|
this.passwordEncoder = passwordEncoder;
|
||||||
|
this.rolesRepository = rolesRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ClientDTO toDto(Client client) {
|
private ClientDTO toDto(Client client) {
|
||||||
@@ -42,6 +50,16 @@ public class ClientService {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Client fromDto(ClientRegistrationDTO dto) {
|
||||||
|
Client client = new Client();
|
||||||
|
|
||||||
|
client.setFirstName(dto.getFirstName());
|
||||||
|
client.setLastName(dto.getLastName());
|
||||||
|
client.setEmail(dto.getEmail());
|
||||||
|
client.setPassword(dto.getPassword());
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
public List<ClientDTO> getAllClients() {
|
public List<ClientDTO> getAllClients() {
|
||||||
List<Client> clients = clientRepository.findAll();
|
List<Client> clients = clientRepository.findAll();
|
||||||
return clients.stream().map(this::toDto).toList();
|
return clients.stream().map(this::toDto).toList();
|
||||||
@@ -75,4 +93,26 @@ public class ClientService {
|
|||||||
public void deleteClient(Long id) {
|
public void deleteClient(Long id) {
|
||||||
clientRepository.deleteById(id);
|
clientRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// И замените метод checkClientCredentials на:
|
||||||
|
public boolean checkClientCredentials(AuthRequestDTO dto) {
|
||||||
|
Client cl = clientRepository.findByEmail(dto.getEmail());
|
||||||
|
if (cl == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return passwordEncoder.matches(dto.getPassword(), cl.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
// При создании нового пользователя не забудьте шифровать пароль:
|
||||||
|
public ClientDTO registerClient(ClientRegistrationDTO clientDTO) {
|
||||||
|
Client client = fromDto(clientDTO);
|
||||||
|
client.setRole(rolesRepository.findRoleById(1L));
|
||||||
|
client.setPassword(passwordEncoder.encode(client.getPassword()));
|
||||||
|
return toDto(clientRepository.save(client));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Client getClientByEmail(String email) {
|
||||||
|
return clientRepository.findByEmail(email);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ import _11.asktpk.artisanconnectbackend.entities.Notice;
|
|||||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||||
import _11.asktpk.artisanconnectbackend.repository.NoticeRepository;
|
import _11.asktpk.artisanconnectbackend.repository.NoticeRepository;
|
||||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||||
//import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
@@ -16,15 +18,21 @@ import java.util.Optional;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class NoticeService {
|
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 NoticeRepository noticeRepository;
|
||||||
private final ClientRepository clientRepository;
|
private final ClientRepository clientRepository;
|
||||||
private final WishlistService wishlistService;
|
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.noticeRepository = noticeRepository;
|
||||||
this.clientRepository = clientRepository;
|
this.clientRepository = clientRepository;
|
||||||
this.wishlistService = wishlistService;
|
this.wishlistService = wishlistService;
|
||||||
|
this.imageService = imageService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Notice fromDTO(NoticeDTO dto) {
|
public Notice fromDTO(NoticeDTO dto) {
|
||||||
@@ -116,6 +124,22 @@ public class NoticeService {
|
|||||||
public void deleteNotice(Long id) {
|
public void deleteNotice(Long id) {
|
||||||
if (noticeExists(id)) {
|
if (noticeExists(id)) {
|
||||||
noticeRepository.deleteById(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 {
|
} else {
|
||||||
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
|
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -43,4 +43,20 @@ public class Enums {
|
|||||||
public enum Status {
|
public enum Status {
|
||||||
ACTIVE, INACTIVE
|
ACTIVE, INACTIVE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum OrderType {
|
||||||
|
ACTIVATION,
|
||||||
|
BOOST
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public enum OrderStatus {
|
||||||
|
PENDING, COMPLETED, CANCELLED
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum PaymentStatus{
|
||||||
|
PENDING, CORRECT, INCORRECT
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,4 +16,17 @@ spring.jpa.hibernate.ddl-auto=create-drop
|
|||||||
|
|
||||||
file.upload-dir=/Users/andsol/Desktop/uploads
|
file.upload-dir=/Users/andsol/Desktop/uploads
|
||||||
spring.servlet.multipart.max-file-size=10MB
|
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
|
||||||
|
tpay.securityCode = )IY7E)YSM!A)Q6O-GN#U7U_33s9qObk8
|
||||||
|
|
||||||
|
#jwt settings
|
||||||
|
jwt.secret=DIXLsOs3FKmCAQwISd0SKsHMXJrPl3IKIRkVlkOvYW7kEcdUTbxh8zFe1B3eZWkY
|
||||||
|
jwt.expiration=300000
|
||||||
|
|
||||||
|
logging.file.name=logs/payment-notifications.log
|
||||||
|
logging.level.TpayLogger=INFO
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
INSERT INTO clients (email, first_name, image, last_name, password, role)
|
INSERT INTO roles (id, rolename)
|
||||||
VALUES
|
VALUES
|
||||||
('dignissim.tempor.arcu@aol.ca', 'Diana', 'null', 'Harrison', 'password', 'USER'),
|
(1, 'USER'),
|
||||||
('john.doe@example.com', 'John', 'null', 'Doe', 'password123', 'ADMIN'),
|
(2, 'ADMIN');
|
||||||
('jane.smith@example.com', 'Jane', 'null', 'Smith', 'securepass', 'USER'),
|
|
||||||
('michael.brown@example.com', 'Michael', 'null', 'Brown', 'mypassword', 'USER'),
|
INSERT INTO clients (email, first_name, last_name, password, role_id)
|
||||||
('emily.jones@example.com', 'Emily', 'null', 'Jones', 'passw0rd', 'USER');
|
VALUES
|
||||||
|
('dignissim.tempor.arcu@aol.ca', 'Diana', 'Harrison', 'password', 1),
|
||||||
|
('john.doe@example.com', 'John', 'Doe', 'password123', 2),
|
||||||
|
('jane.smith@example.com', 'Jane', 'Smith', 'securepass', 1),
|
||||||
|
('michael.brown@example.com', 'Michael', 'Brown', 'mypassword', 1),
|
||||||
|
('emily.jones@example.com', 'Emily', 'Jones', 'passw0rd', 1);
|
||||||
|
|
||||||
|
|
||||||
INSERT INTO notice (title, description, client_id, price, category, status, publish_date) VALUES
|
INSERT INTO notice (title, description, client_id, price, category, status, publish_date) VALUES
|
||||||
|
|||||||
Reference in New Issue
Block a user