diff --git a/src/main/java/ru/soune/nocopy/configuration/HandlerConfig.java b/src/main/java/ru/soune/nocopy/configuration/HandlerConfig.java index 40451fa..b2d7862 100644 --- a/src/main/java/ru/soune/nocopy/configuration/HandlerConfig.java +++ b/src/main/java/ru/soune/nocopy/configuration/HandlerConfig.java @@ -21,7 +21,8 @@ public class HandlerConfig { VerifyRegisterUserHandler verifyRegisterUser, AuthRequestHandler authRequestHandler, CompanyHandler companyHandler, - TariffHandler tariffHandler + TariffHandler tariffHandler, + TariffInfoHandler tariffInfoHandler ) { Map 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; } diff --git a/src/main/java/ru/soune/nocopy/dto/MessageCode.java b/src/main/java/ru/soune/nocopy/dto/MessageCode.java index 7ded2ee..db41265 100644 --- a/src/main/java/ru/soune/nocopy/dto/MessageCode.java +++ b/src/main/java/ru/soune/nocopy/dto/MessageCode.java @@ -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; diff --git a/src/main/java/ru/soune/nocopy/dto/tarriff/TariffInfoRequest.java b/src/main/java/ru/soune/nocopy/dto/tarriff/TariffInfoRequest.java new file mode 100644 index 0000000..b464904 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/dto/tarriff/TariffInfoRequest.java @@ -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; +} \ No newline at end of file diff --git a/src/main/java/ru/soune/nocopy/dto/tarriff/TariffInfoResponse.java b/src/main/java/ru/soune/nocopy/dto/tarriff/TariffInfoResponse.java new file mode 100644 index 0000000..87fcea7 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/dto/tarriff/TariffInfoResponse.java @@ -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; +} diff --git a/src/main/java/ru/soune/nocopy/handler/TariffInfoHandler.java b/src/main/java/ru/soune/nocopy/handler/TariffInfoHandler.java new file mode 100644 index 0000000..1b39027 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/handler/TariffInfoHandler.java @@ -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 allTariffInfos = tariffInfoService.getAllTariffInfos(); + + List 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 + ); + } + } +}