dev add tariffinfo endpoints
Test Workflow / test (push) Successful in 4s

This commit is contained in:
vladp
2026-02-02 15:47:58 +07:00
parent 3ce8edd7e1
commit c38b86e1b9
5 changed files with 382 additions and 2 deletions
@@ -21,7 +21,8 @@ public class HandlerConfig {
VerifyRegisterUserHandler verifyRegisterUser,
AuthRequestHandler authRequestHandler,
CompanyHandler companyHandler,
TariffHandler tariffHandler
TariffHandler tariffHandler,
TariffInfoHandler tariffInfoHandler
) {
Map<Integer, RequestHandler> map = new HashMap<>();
map.put(20001, login);
@@ -34,6 +35,7 @@ public class HandlerConfig {
map.put(20009, verifyRegisterUser);
map.put(30000, companyHandler);
map.put(30001, tariffHandler);
map.put(30002, tariffInfoHandler);
return map;
}
@@ -38,7 +38,8 @@ public enum MessageCode {
RESOURCE_NOT_FOUND(4, "Resource not found"),
INTERNAL_ERROR(4, "Internal server error"),
COMPANY_NOT_FOUND(4, "Company not found"),
COMPANY_ALREADY_EXISTS(2, "Company already exists");
COMPANY_ALREADY_EXISTS(2, "Company already exists"),
ERROR_TARIFF_INFO(2, "Erorr with tariff info");
private final Integer code;
@@ -0,0 +1,35 @@
package ru.soune.nocopy.dto.tarriff;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TariffInfoRequest {
private String id;
@NotNull(message = "Action is required")
@JsonProperty("action")
private String action;
@JsonProperty("status")
private String status;
@JsonProperty("start_tariff")
private LocalDateTime startTariff;
@JsonProperty("end_tariff")
private LocalDateTime endTariff;
@JsonProperty("tariff_id")
private Long tariffId;
@JsonProperty("tariff_type")
private String tariffType;
}
@@ -0,0 +1,33 @@
package ru.soune.nocopy.dto.tarriff;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TariffInfoResponse {
@JsonProperty("id")
private String id;
@JsonProperty("status")
private String status;
@JsonProperty("start_tariff")
private LocalDateTime startTariff;
@JsonProperty("end_tariff")
private LocalDateTime endTariff;
@JsonProperty("tariff_id")
private Long tariffId;
@JsonProperty("tariff_name")
private String tariffName;
}
@@ -0,0 +1,309 @@
package ru.soune.nocopy.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import ru.soune.nocopy.dto.BaseRequest;
import ru.soune.nocopy.dto.BaseResponse;
import ru.soune.nocopy.dto.MessageCode;
import ru.soune.nocopy.dto.tarriff.TariffInfoDTO;
import ru.soune.nocopy.dto.tarriff.TariffInfoRequest;
import ru.soune.nocopy.dto.tarriff.TariffInfoResponse;
import ru.soune.nocopy.entity.tarif.TariffStatus;
import ru.soune.nocopy.entity.tarif.TariffType;
import ru.soune.nocopy.service.tariff.TariffInfoService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
@Slf4j
@RequiredArgsConstructor
public class TariffInfoHandler implements RequestHandler {
private final ObjectMapper objectMapper;
private final TariffInfoService tariffInfoService;
@Override
public BaseResponse handle(BaseRequest request) throws Exception {
try {
TariffInfoRequest tariffInfoRequest = objectMapper.convertValue(request.getMessageBody(), TariffInfoRequest.class);
String action = tariffInfoRequest.getAction();
switch (action) {
case "create":
return handleCreateTariffInfo(request, tariffInfoRequest);
case "create_by_type":
return handleCreateTariffInfoByType(request, tariffInfoRequest);
case "update":
return handleUpdateTariffInfo(request, tariffInfoRequest);
case "delete":
return handleDeleteTariffInfo(request, tariffInfoRequest);
case "get_by_id":
return handleGetTariffInfoById(request, tariffInfoRequest);
case "get_all":
return handleGetAllTariffInfos(request, tariffInfoRequest);
default:
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Unknown action: " + action,
null
);
}
} catch (Exception e) {
log.error("Error processing tariff info request", e);
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Error processing request: " + e.getMessage(),
null
);
}
}
private BaseResponse handleCreateTariffInfo(BaseRequest request, TariffInfoRequest tariffInfoRequest) {
try {
TariffInfoDTO tariffInfoDTO = new TariffInfoDTO(
null,
tariffInfoRequest.getStatus() != null ? tariffInfoRequest.getStatus() : TariffStatus.ACTIVE.name(),
tariffInfoRequest.getStartTariff() != null ? tariffInfoRequest.getStartTariff() : LocalDateTime.now(),
tariffInfoRequest.getEndTariff() != null ? tariffInfoRequest.getEndTariff() : LocalDateTime.now().plusDays(30),
tariffInfoRequest.getTariffId(),
null
);
TariffInfoDTO createdTariffInfo = tariffInfoService.createTariffInfo(tariffInfoDTO);
TariffInfoResponse response = TariffInfoResponse.builder()
.id(createdTariffInfo.getId())
.status(createdTariffInfo.getStatus())
.startTariff(createdTariffInfo.getStartTariff())
.endTariff(createdTariffInfo.getEndTariff())
.tariffId(createdTariffInfo.getTariffId())
.tariffName(createdTariffInfo.getTariffName())
.build();
return new BaseResponse(
request.getMsgId(),
MessageCode.SUCCESS.getCode(),
"Tariff info created successfully",
response
);
} catch (Exception e) {
log.error("Error creating tariff info", e);
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Failed to create tariff info: " + e.getMessage(),
null
);
}
}
private BaseResponse handleCreateTariffInfoByType(BaseRequest request, TariffInfoRequest tariffInfoRequest) {
try {
if (tariffInfoRequest.getTariffType() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Tariff type is required for create_by_type action",
null
);
}
tariffInfoService.createTariffInfo(
TariffType.valueOf(tariffInfoRequest.getTariffType().toUpperCase())
);
return new BaseResponse(
request.getMsgId(),
MessageCode.SUCCESS.getCode(),
"Tariff info created successfully by type: " + tariffInfoRequest.getTariffType(),
null
);
} catch (IllegalArgumentException e) {
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Invalid tariff type: " + tariffInfoRequest.getTariffType(),
null
);
} catch (Exception e) {
log.error("Error creating tariff info by type", e);
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Failed to create tariff info by type: " + e.getMessage(),
null
);
}
}
private BaseResponse handleUpdateTariffInfo(BaseRequest request, TariffInfoRequest tariffInfoRequest) {
try {
if (tariffInfoRequest.getId() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Tariff info ID is required for update",
null
);
}
TariffInfoDTO tariffInfoDTO = new TariffInfoDTO(
tariffInfoRequest.getId(),
tariffInfoRequest.getStatus(),
tariffInfoRequest.getStartTariff(),
tariffInfoRequest.getEndTariff(),
tariffInfoRequest.getTariffId(),
null
);
TariffInfoDTO updatedTariffInfo = tariffInfoService.updateTariffInfo(
tariffInfoRequest.getId(),
tariffInfoDTO
);
TariffInfoResponse response = TariffInfoResponse.builder()
.id(updatedTariffInfo.getId())
.status(updatedTariffInfo.getStatus())
.startTariff(updatedTariffInfo.getStartTariff())
.endTariff(updatedTariffInfo.getEndTariff())
.tariffId(updatedTariffInfo.getTariffId())
.tariffName(updatedTariffInfo.getTariffName())
.build();
return new BaseResponse(
request.getMsgId(),
MessageCode.SUCCESS.getCode(),
"Tariff info updated successfully",
response
);
} catch (Exception e) {
log.error("Error updating tariff info", e);
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Failed to update tariff info: " + e.getMessage(),
null
);
}
}
private BaseResponse handleDeleteTariffInfo(BaseRequest request, TariffInfoRequest tariffInfoRequest) {
try {
if (tariffInfoRequest.getId() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Tariff info ID is required for delete",
null
);
}
TariffInfoDTO tariffInfo = tariffInfoService.getTariffInfoById(tariffInfoRequest.getId());
tariffInfoService.deleteTariffInfo(tariffInfoRequest.getId());
TariffInfoResponse response = TariffInfoResponse.builder()
.id(tariffInfo.getId())
.status(tariffInfo.getStatus())
.startTariff(tariffInfo.getStartTariff())
.endTariff(tariffInfo.getEndTariff())
.tariffId(tariffInfo.getTariffId())
.tariffName(tariffInfo.getTariffName())
.build();
return new BaseResponse(
request.getMsgId(),
MessageCode.SUCCESS.getCode(),
"Tariff info deleted successfully",
response
);
} catch (Exception e) {
log.error("Error deleting tariff info", e);
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Failed to delete tariff info: " + e.getMessage(),
null
);
}
}
private BaseResponse handleGetTariffInfoById(BaseRequest request, TariffInfoRequest tariffInfoRequest) {
try {
if (tariffInfoRequest.getId() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Tariff info ID is required",
null
);
}
TariffInfoDTO tariffInfo = tariffInfoService.getTariffInfoById(tariffInfoRequest.getId());
TariffInfoResponse response = TariffInfoResponse.builder()
.id(tariffInfo.getId())
.status(tariffInfo.getStatus())
.startTariff(tariffInfo.getStartTariff())
.endTariff(tariffInfo.getEndTariff())
.tariffId(tariffInfo.getTariffId())
.tariffName(tariffInfo.getTariffName())
.build();
return new BaseResponse(
request.getMsgId(),
MessageCode.SUCCESS.getCode(),
"Tariff info retrieved successfully",
response
);
} catch (Exception e) {
log.error("Error getting tariff info by ID", e);
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Failed to get tariff info: " + e.getMessage(),
null
);
}
}
private BaseResponse handleGetAllTariffInfos(BaseRequest request, TariffInfoRequest tariffInfoRequest) {
try {
List<TariffInfoDTO> allTariffInfos = tariffInfoService.getAllTariffInfos();
List<TariffInfoResponse> responses = allTariffInfos.stream()
.map(info -> TariffInfoResponse.builder()
.id(info.getId())
.status(info.getStatus())
.startTariff(info.getStartTariff())
.endTariff(info.getEndTariff())
.tariffId(info.getTariffId())
.tariffName(info.getTariffName())
.build())
.collect(Collectors.toList());
return new BaseResponse(
request.getMsgId(),
MessageCode.SUCCESS.getCode(),
"Tariff infos retrieved successfully",
Map.of("tariff_infos", responses, "count", responses.size())
);
} catch (Exception e) {
log.error("Error getting all tariff infos", e);
return new BaseResponse(
request.getMsgId(),
MessageCode.ERROR_TARIFF_INFO.getCode(),
"Failed to get tariff infos: " + e.getMessage(),
null
);
}
}
}