Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
</module>
<!-- Проверка длины строки -->
<module name="LineLength">
<property name="max" value="120"/> <!-- Максимальная длина строки-->
<property name="max" value="150"/> <!-- Максимальная длина строки-->
</module>
</module>
28 changes: 16 additions & 12 deletions payment-service-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
<url/>
<properties>
<java.version>21</java.version>
<springdoc.version>3.0.0</springdoc.version>
<lombok.version>1.18.36</lombok.version>
<mapstruct.version>1.6.3</mapstruct.version>
<mapstruct-processor.version>1.6.3</mapstruct-processor.version>
<maven-compiler-plugin.version>3.14.0</maven-compiler-plugin.version>
<maven-checkstyle-plugin.version>3.3.0</maven-checkstyle-plugin.version>
<liquibase-maven-plugin.version>4.32.0</liquibase-maven-plugin.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -34,28 +41,27 @@
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>3.0.0</version>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<!--version>42.7.7</version-->
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.6.3</version>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.6.3</version>
<version>${mapstruct-processor.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.36</version>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
Expand Down Expand Up @@ -86,28 +92,26 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>21</source>
<target>21</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.36</version>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.6.3</version>
<version>${mapstruct-processor.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<version>${maven-checkstyle-plugin.version}</version>
<executions>
<execution>
<phase>verify</phase>
Expand All @@ -125,7 +129,7 @@
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>4.32.0</version>
<version>${liquibase-maven-plugin.version}</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
<outputChangeLogFile>src/main/resources/liquibase-outputChangeLog.xml</outputChangeLogFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,28 @@
import com.iprody.model.PaymentDto;
import com.iprody.service.PaymentService;
import com.iprody.specification.PaymentFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
import java.util.List;
import java.util.UUID;


@RestController
@RequiredArgsConstructor
@RequestMapping("/payments")
public class PaymentController {

private final PaymentService paymentService;

/**
* Use either "withMapper" or "withConverter"
* @param paymentService
*/
@Autowired
public PaymentController(@Qualifier("withMapper") PaymentService paymentService) {
this.paymentService = paymentService;
}

@GetMapping("/search")
public Page<PaymentDto> searchPayments(
@ModelAttribute PaymentFilter filter,
Expand All @@ -49,17 +43,38 @@ public Page<PaymentDto> searchPayments(

@GetMapping
public ResponseEntity<List<PaymentDto>> fetchAll() {
return ResponseEntity.ok().body(this.paymentService.fetchAllPayments());
return ResponseEntity.ok().body(this.paymentService.getPayments());
}

@PostMapping(path = "/add")
public ResponseEntity<PaymentDto> addPayment(@RequestBody PaymentDto paymentDto) {
final PaymentDto savedPayment = this.paymentService.create(paymentDto);
final URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedPayment.getGuid())
.toUri();
return ResponseEntity.created(location).body(savedPayment);
}

@GetMapping(path = "/{id}")
public ResponseEntity<PaymentDto> fetchPayment(@PathVariable UUID id) {
return ResponseEntity.ok().body(this.paymentService.fetchSinglePayment(id));
public ResponseEntity<PaymentDto> getPayment(@PathVariable UUID id) {
return ResponseEntity.ok().body(this.paymentService.get(id));
}

@PostMapping(path = "/addPayment")
public ResponseEntity<PaymentDto> addPayment(@RequestBody PaymentDto paymentDto) {
return ResponseEntity.ok().body(this.paymentService.processPayment(paymentDto));
@PutMapping(path = "/update/{id}")
public ResponseEntity<PaymentDto> updatePayment(@PathVariable UUID id, @RequestBody PaymentDto paymentDto) {
return ResponseEntity.ok().body(this.paymentService.update(id, paymentDto));
}

@PutMapping(path = "/update/{id}/{note}")
public ResponseEntity<PaymentDto> updatePaymentNote(@PathVariable UUID id, @PathVariable String note) {
return ResponseEntity.ok().body(this.paymentService.updateNote(id, note));
}

@DeleteMapping(path = "/delete/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deletePayment(@PathVariable UUID id) {
this.paymentService.delete(id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.iprody.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.UUID;

@Getter
@ResponseStatus(HttpStatus.NOT_FOUND)
public class EntityNotFoundException extends RuntimeException {

private final String operation;
private final UUID entityId;

public EntityNotFoundException(String message, String operation, UUID entityId) {
super(message);
this.operation = operation;
this.entityId = entityId;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.iprody.exception;

import lombok.Getter;

import java.time.Instant;
import java.util.UUID;

@Getter
public class ErrorResponse {

private final String error;
private final Instant timestamp;
private final String operation;
private final UUID entityId;

public ErrorResponse(String error, String operation, UUID entityId) {
this.error = error;
this.timestamp = Instant.now();
this.operation = operation;
this.entityId = entityId;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.iprody.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@ControllerAdvice

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(AppException.class)
Expand All @@ -14,4 +17,16 @@ public ResponseEntity<String> handleAppException(AppException ex) {
.body(ex.getMessage());
}

@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handleNotFound(EntityNotFoundException ex) {
return new ErrorResponse(ex.getMessage(), ex.getOperation(), ex.getEntityId());
}

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleOther(Exception ex) {
return new ErrorResponse(ex.getMessage(), null, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import java.util.UUID;

public interface PaymentService {
List<PaymentDto> fetchAllPayments();
PaymentDto fetchSinglePayment(UUID id);
PaymentDto processPayment(PaymentDto paymentDto);
PaymentDto create(PaymentDto paymentDto);
PaymentDto get(UUID id);
List<PaymentDto> search(PaymentFilter filter);
Page<PaymentDto> searchPaged(PaymentFilter filter, Pageable pageable);
List<PaymentDto> getPayments();
PaymentDto update(UUID id, PaymentDto dto);
PaymentDto updateNote(UUID id, String updatedNote);
void delete(UUID id);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.iprody.service;

import com.iprody.exception.AppException;
import com.iprody.exception.EntityNotFoundException;
import com.iprody.mapper.PaymentMapper;
import com.iprody.model.PaymentDto;
import com.iprody.persistence.PaymentEntity;
Expand All @@ -14,13 +15,14 @@
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;


@Service("withMapper")
@Service
@RequiredArgsConstructor
public class PaymentServiceImpl_withMapper implements PaymentService {
public class PaymentServiceImpl implements PaymentService {

private final PaymentMapper paymentMapper;
private final PaymentRepository paymentRepository;
Expand All @@ -43,27 +45,57 @@ public Page<PaymentDto> searchPaged(PaymentFilter filter, Pageable pageable) {
}

@Override
public List<PaymentDto> fetchAllPayments() {
return paymentRepository.findAll().stream()
public List<PaymentDto> getPayments() {
return paymentRepository
.findAll().stream()
.map(paymentMapper::toPaymentDto)
.toList();
}

@Override
public PaymentDto fetchSinglePayment(UUID id) {
public PaymentDto get(UUID id) {
return paymentRepository.findById(id)
.map(paymentMapper::toPaymentDto)
.orElseThrow(() -> new AppException(
HttpStatus.NOT_FOUND.value(), "Payment with the id '" + id + "' was not found"));
.orElseThrow(() -> new EntityNotFoundException("Платеж не найден", "get", id));
}

@Override
public PaymentDto processPayment(PaymentDto paymentDto) {
public PaymentDto create(PaymentDto paymentDto) {
if (paymentDto.getAmount().doubleValue() <= 0) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), "Payment is not valid");
}
final var paymentEntity = paymentMapper.toPaymentEntity(paymentDto);
return paymentMapper.toPaymentDto(paymentRepository.save(paymentEntity));
}

@Override
public PaymentDto update(UUID id, PaymentDto dto) {
paymentRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Платеж не найден", "update", id));
final PaymentEntity updated = paymentMapper.toPaymentEntity(dto);
updated.setGuid(id);
return paymentMapper.toPaymentDto(paymentRepository.save(updated));
}

@Override
public PaymentDto updateNote(UUID id, String updatedNote) {
return paymentRepository.findById(id)
.map(p -> {
p.setNote(updatedNote);
p.setUpdatedAt(OffsetDateTime.now());
return paymentMapper.toPaymentDto(paymentRepository.save(p));
})
.orElseThrow(() -> new EntityNotFoundException("Платеж не найден", "updateNote", id));
}

@Override
public void delete(UUID id) {
paymentRepository.findById(id)
.map(p -> {
paymentRepository.delete(p);
return id;
})
.orElseThrow(() -> new EntityNotFoundException("Платеж не найден", "delete", id));
}

}
Loading