lr5
This commit is contained in:
1 parent
8d21930dd4
commit
a5d46d500b
54 files changed
+2318
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum Codes {
|
||||
|
||||
SUCCESS("success"),
|
||||
FAILED("failed");
|
||||
|
||||
private final String name;
|
||||
|
||||
Codes(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum ErrorCodes {
|
||||
|
||||
EMPTY(""),
|
||||
VALIDATION_EXCEPTION("ValidationException"),
|
||||
UNKNOWN_EXCEPTION("UnknownException"),
|
||||
UNSUPPORTED_EXCEPTION("UnsupportedException");
|
||||
|
||||
private final String name;
|
||||
|
||||
ErrorCodes(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum ErrorMessages {
|
||||
|
||||
EMPTY(""),
|
||||
VALIDATION("Ошибка валидации"),
|
||||
UNKNOWN("Не поддерживаемая ошибка");
|
||||
|
||||
private final String description;
|
||||
|
||||
ErrorMessages(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getName() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum Positions {
|
||||
DEV(2.2, false),
|
||||
HR(1.2, false),
|
||||
TL(2.6, true),
|
||||
PO(3.0, true),
|
||||
TPM(2.8, true),
|
||||
CTO(4.0, true);
|
||||
|
||||
private final double positionCoefficient;
|
||||
private final boolean isManager;
|
||||
|
||||
Positions(double positionCoefficient, boolean isManager) {
|
||||
this.positionCoefficient = positionCoefficient;
|
||||
this.isManager = isManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Request {
|
||||
//уникальный идентификатор сообщения
|
||||
@NotBlank(message = "UID не может быть пустым")
|
||||
@Size(max = 32, message = "UID не должно быть больше 32 символов")
|
||||
private String uid;
|
||||
|
||||
//уникальный идентификатор операции
|
||||
@Size(max = 32, message = "operationUid не должно быть больше 32 символов")
|
||||
private String operationUid;
|
||||
|
||||
//имя системы
|
||||
private Systems systemName;
|
||||
|
||||
//время создания сообщения
|
||||
private String systemTime;
|
||||
|
||||
//источник запроса
|
||||
private String source;
|
||||
//должность
|
||||
private Positions position;
|
||||
//оклад
|
||||
private Double salary;
|
||||
//бонус
|
||||
private Double bonus;
|
||||
//количество отработанных дней в году
|
||||
private Integer workDays;
|
||||
|
||||
//идентификатор коммуникации
|
||||
@NotNull
|
||||
@Min(1)
|
||||
@Max(100000)
|
||||
private Integer communicationId;
|
||||
|
||||
//идентификатор шаблона
|
||||
private Integer templateId;
|
||||
//код продукта
|
||||
private Integer productCode;
|
||||
//код смс
|
||||
private Integer smsCode;
|
||||
|
||||
//время запроса
|
||||
private LocalDateTime requestTime;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{" +
|
||||
"uid='" + uid + '\'' +
|
||||
", operationUid='" + operationUid + '\'' +
|
||||
", systemName='" + systemName + '\'' +
|
||||
", systemTime='" + systemTime + '\'' +
|
||||
", source='" + source + '\'' +
|
||||
", position=" + position +
|
||||
", salary=" + salary +
|
||||
", bonus=" + bonus +
|
||||
", workDays=" + workDays +
|
||||
", communicationId=" + communicationId +
|
||||
", templateId=" + templateId +
|
||||
", productCode=" + productCode +
|
||||
", smsCode=" + smsCode +
|
||||
", requestTime=" + requestTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class Response {
|
||||
//уникальный идентификатор сообщения
|
||||
private String uid;
|
||||
//уникальный идентификатор операции
|
||||
private String operationUid;
|
||||
//время создания сообщения
|
||||
private String systemTime;
|
||||
//код запроса (error/success)
|
||||
private Codes code;
|
||||
//премия годовая или квартальная в зависимости от должности
|
||||
private Double annualBonus;
|
||||
//код ошибки
|
||||
private ErrorCodes errorCode;
|
||||
//сообщение ошибки
|
||||
private ErrorMessages errorMessage;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum Systems {
|
||||
ERP("Enterprise Resource Planning"),
|
||||
CRM("Customer Relationship Management"),
|
||||
WMS("Warehouse Management System"),
|
||||
SERVICE1("Service 1");
|
||||
|
||||
private final String description;
|
||||
|
||||
Systems(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public static Systems fromValue(String value) {
|
||||
for (Systems system : values()) {
|
||||
if (system.name().equalsIgnoreCase(value) || system.description.equalsIgnoreCase(value)) {
|
||||
return system;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown systemName: " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.testapp.SecondAppV2;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SecondAppV2Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SecondAppV2Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.testapp.SecondAppV2.controller;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Codes;
|
||||
import com.testapp.SecondAppV2.Model.ErrorCodes;
|
||||
import com.testapp.SecondAppV2.Model.ErrorMessages;
|
||||
import com.testapp.SecondAppV2.Model.Request;
|
||||
import com.testapp.SecondAppV2.Model.Response;
|
||||
import com.testapp.SecondAppV2.exception.ValidationFailedException;
|
||||
import com.testapp.SecondAppV2.service.AnnualBonusService;
|
||||
import com.testapp.SecondAppV2.service.ModifyRequestService;
|
||||
import com.testapp.SecondAppV2.service.ModifyResponseService;
|
||||
import com.testapp.SecondAppV2.service.ValidationService;
|
||||
import com.testapp.SecondAppV2.util.DateTimeUtil;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import java.util.Date;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class MyController {
|
||||
private final ValidationService validationService;
|
||||
private final ModifyResponseService modifyResponseService;
|
||||
private final ModifyRequestService modifyRequestService;
|
||||
private final AnnualBonusService annualBonusService;
|
||||
|
||||
@Autowired
|
||||
public MyController(ValidationService validationService, @Qualifier("ModifySystemTimeResponseService") ModifyResponseService modifyResponseService, ModifyRequestService modifyRequestService, AnnualBonusService annualBonusService) {
|
||||
this.validationService = validationService;
|
||||
this.modifyResponseService = modifyResponseService;
|
||||
this.modifyRequestService = modifyRequestService;
|
||||
this.annualBonusService = annualBonusService;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/feedback")
|
||||
public ResponseEntity<Response> feedback(@Valid @RequestBody Request request, BindingResult bindingResult) {
|
||||
log.info("request: {}", request);
|
||||
Response response = buildSuccessResponse(request);
|
||||
try {
|
||||
validationService.isValid(bindingResult);
|
||||
double annualBonus = annualBonusService.calculate(request.getPosition(), request.getSalary(), request.getBonus(), request.getWorkDays());
|
||||
response.setAnnualBonus(annualBonus);
|
||||
modifyRequestService.modify(request);
|
||||
} catch (ValidationFailedException e) {
|
||||
log.error("validation failed: {}", e.getMessage());
|
||||
return buildErrorResponse(response, Codes.FAILED, ErrorCodes.VALIDATION_EXCEPTION, ErrorMessages.VALIDATION, HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception e) {
|
||||
log.error("unknown exception: {}", e.getMessage());
|
||||
return buildErrorResponse(response, Codes.FAILED, ErrorCodes.UNKNOWN_EXCEPTION, ErrorMessages.UNKNOWN, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
log.info("response: {}", response);
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
|
||||
private Response buildSuccessResponse(Request request) {
|
||||
return Response.builder()
|
||||
.uid(request.getUid())
|
||||
.operationUid(request.getOperationUid())
|
||||
.systemTime(DateTimeUtil.getCustomFormat().format(new Date(0)))
|
||||
.code(Codes.SUCCESS)
|
||||
.errorCode(ErrorCodes.EMPTY)
|
||||
.errorMessage(ErrorMessages.EMPTY)
|
||||
.build();
|
||||
}
|
||||
|
||||
private ResponseEntity<Response> buildErrorResponse(Response response, Codes code, ErrorCodes errorCode, ErrorMessages errorMessage, HttpStatus status) {
|
||||
response.setCode(code);
|
||||
response.setErrorCode(errorCode);
|
||||
response.setErrorMessage(errorMessage);
|
||||
return new ResponseEntity<>(response, status);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.testapp.SecondAppV2.exception;
|
||||
|
||||
public class UnsupportedCodeException extends Exception {
|
||||
public UnsupportedCodeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.testapp.SecondAppV2.exception;
|
||||
|
||||
public class ValidationFailedException extends Exception {
|
||||
public ValidationFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Positions;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public interface AnnualBonusService {
|
||||
double calculate(Positions positions, double salary, double bonus, int workDays);
|
||||
|
||||
double calculateQuarterlyBonus(Positions positions, double salary, double bonus, int workDays);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Positions;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
|
||||
@Service
|
||||
public class AnnualBonusServiceImpl implements AnnualBonusService {
|
||||
@Override
|
||||
public double calculate(Positions positions, double salary, double bonus, int workDays) {
|
||||
int daysInYear = Year.now().length();
|
||||
return salary * bonus * daysInYear * positions.getPositionCoefficient() / workDays;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double calculateQuarterlyBonus(Positions positions, double salary, double bonus, int workDays) {
|
||||
if (!positions.isManager()) {
|
||||
throw new IllegalArgumentException("премия начисляется менеджерам: " + positions);
|
||||
}
|
||||
LocalDate now = LocalDate.now();
|
||||
int quarter = (now.getMonthValue() - 1) / 3;
|
||||
boolean leap = Year.isLeap(now.getYear());
|
||||
int daysInQuarter = 0;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
daysInQuarter += Month.of(quarter * 3 + 1 + i).length(leap);
|
||||
}
|
||||
return salary * bonus * daysInQuarter * positions.getPositionCoefficient() / workDays;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Response;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Qualifier("ModifyOperationUidResponseService")
|
||||
public class ModifyOperationUidResponseService implements ModifyResponseService {
|
||||
@Override
|
||||
public Response modify(Response response) {
|
||||
log.info("response before: {}", response);
|
||||
UUID uuid = UUID.randomUUID();
|
||||
response.setOperationUid(uuid.toString());
|
||||
log.info("response after: {}", response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Request;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public interface ModifyRequestService {
|
||||
void modify(Request request);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.testapp.SecondAppV2.Model.Response;
|
||||
|
||||
@Service
|
||||
public interface ModifyResponseService {
|
||||
Response modify(Response response);
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Request;
|
||||
import com.testapp.SecondAppV2.Model.Systems;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
public class ModifySystemNameRequestService implements ModifyRequestService {
|
||||
@Override
|
||||
public void modify(Request request) {
|
||||
request.setSystemName(Systems.SERVICE1);
|
||||
request.setSource("source service 1");
|
||||
|
||||
// текущее время перед отправкой запроса
|
||||
request.setRequestTime(LocalDateTime.now());
|
||||
|
||||
HttpEntity<Request> httpEntity = new HttpEntity<>(request);
|
||||
|
||||
new RestTemplate().exchange("http://localhost:8084/feedback",
|
||||
HttpMethod.POST,
|
||||
httpEntity,
|
||||
new ParameterizedTypeReference<>() {
|
||||
});
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Response;
|
||||
import com.testapp.SecondAppV2.util.DateTimeUtil;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Qualifier("ModifySystemTimeResponseService")
|
||||
public class ModifySystemTimeResponseService implements ModifyResponseService {
|
||||
@Override
|
||||
public Response modify(Response response) {
|
||||
log.info("response before: {}", response);
|
||||
response.setSystemTime(DateTimeUtil.getCustomFormat().format(new Date(0)));
|
||||
log.info("response after: {}", response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import com.testapp.SecondAppV2.exception.ValidationFailedException;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class RequestValidationService implements ValidationService {
|
||||
|
||||
@Override
|
||||
public void isValid(BindingResult bindingResult) throws ValidationFailedException {
|
||||
if (bindingResult.hasErrors()) {
|
||||
log.error("bindingResult: {}", bindingResult.getAllErrors());
|
||||
throw new ValidationFailedException(bindingResult.getFieldError().toString());
|
||||
}
|
||||
log.info("validation passed");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
||||
import com.testapp.SecondAppV2.exception.ValidationFailedException;
|
||||
|
||||
@Service
|
||||
public interface ValidationService {
|
||||
void isValid(BindingResult bindingResult) throws ValidationFailedException;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.testapp.SecondAppV2.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DateTimeUtil {
|
||||
public static SimpleDateFormat getCustomFormat() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
spring.application.name=SecondApp
|
||||
server.port=8082
|
||||
|
||||
logging.level.org.springframework=ERROR
|
||||
logging.level.com.testapp=DEBUG
|
||||
Reference in new issue
Block a user