Commit f2b61cc8 authored by Bazarbay Tulenov's avatar Bazarbay Tulenov

commit add cors filter

parent 64503f77
Pipeline #334 failed with stage
...@@ -32,6 +32,7 @@ dependencies { ...@@ -32,6 +32,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server' implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springframework.cloud:spring-cloud-starter-feign:1.4.7.RELEASE' implementation 'org.springframework.cloud:spring-cloud-starter-feign:1.4.7.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-amqp:3.2.0'
runtimeOnly 'org.postgresql:postgresql:42.6.0' runtimeOnly 'org.postgresql:postgresql:42.6.0'
implementation 'org.springframework.boot:spring-boot-starter-freemarker' implementation 'org.springframework.boot:spring-boot-starter-freemarker'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
......
package kz.project.printedFormsService.config;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.CrossOrigin;
import java.io.IOException;
//@CrossOrigin(origins = "")
@WebFilter("/*")
public class CORSFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Access-Control-Allow-Origin", "*");
httpResponse.setHeader("Access-Control-Allow-Methods", "*");
httpResponse.setHeader("Access-Control-Allow-Headers", "*");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Настройка не требуется.
}
@Override
public void destroy() {
// Освобождение ресурсов не требуется.
}
}
package kz.project.printedFormsService.consumer;
import com.fasterxml.jackson.core.JsonProcessingException;
import kz.project.printedFormsService.exception.ValidationException;
import kz.project.printedFormsService.service.DocumentsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
@Slf4j
public class Consumer {
private final DocumentsService documentsService;
@RabbitListener(queues = "${rabbitmq.document_queue}")
public void handleMessage(Long id) {
try {
documentsService.saveDocument(id);
} catch (ValidationException e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} catch (JsonProcessingException e) {
e.printStackTrace();
log.error(e.getMessage(),e);
}
}
}
package kz.project.printedFormsService.controller; package kz.project.printedFormsService.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
...@@ -9,11 +10,13 @@ import io.swagger.v3.oas.annotations.tags.Tag; ...@@ -9,11 +10,13 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import kz.project.printedFormsService.config.SecurityContextUtils; import kz.project.printedFormsService.config.SecurityContextUtils;
import kz.project.printedFormsService.data.dto.DocumentByDateDto; import kz.project.printedFormsService.data.dto.DocumentByDateDto;
import kz.project.printedFormsService.data.dto.DocumentByTemplateDto; import kz.project.printedFormsService.data.dto.DocumentByTemplateDto;
import kz.project.printedFormsService.data.dto.TemplateDto;
import kz.project.printedFormsService.exception.ValidationException; import kz.project.printedFormsService.exception.ValidationException;
import kz.project.printedFormsService.service.DocumentsService; import kz.project.printedFormsService.service.DocumentsService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -34,6 +37,7 @@ public class DocumentsController { ...@@ -34,6 +37,7 @@ public class DocumentsController {
public static final String BASE_PATH = "/api/documents"; public static final String BASE_PATH = "/api/documents";
public static final String BY_DAY = BASE_PATH + "/by-day"; public static final String BY_DAY = BASE_PATH + "/by-day";
public static final String BY_TEMPLATE = BASE_PATH + "/by-template"; public static final String BY_TEMPLATE = BASE_PATH + "/by-template";
private static final String UPDATE_DOCUMENT = BASE_PATH+"/saveDocument";
@GetMapping(BY_DAY) @GetMapping(BY_DAY)
@Operation(description = "Метод для получения данных по общему количеству генерации по всем доступным пользователю шаблонам") @Operation(description = "Метод для получения данных по общему количеству генерации по всем доступным пользователю шаблонам")
......
...@@ -18,6 +18,7 @@ import lombok.RequiredArgsConstructor; ...@@ -18,6 +18,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -30,6 +31,7 @@ import java.util.Map; ...@@ -30,6 +31,7 @@ import java.util.Map;
@RequestMapping(TemplateController.BASE_PATH) @RequestMapping(TemplateController.BASE_PATH)
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
@CrossOrigin
@Tag(name = "Template Controller", description = "API TemplateService") @Tag(name = "Template Controller", description = "API TemplateService")
public class TemplateController { public class TemplateController {
...@@ -41,6 +43,7 @@ public class TemplateController { ...@@ -41,6 +43,7 @@ public class TemplateController {
public static final String DELETE = "/delete/{code}"; public static final String DELETE = "/delete/{code}";
public static final String GET_ALL = "/all"; public static final String GET_ALL = "/all";
public static final String GET_ALL_BY_CODE = "/allVersion/{code}"; public static final String GET_ALL_BY_CODE = "/allVersion/{code}";
public static final String GET_PROGECT_CODE = "get/prjectCode/";
private final TemplateService service; private final TemplateService service;
...@@ -253,4 +256,6 @@ public class TemplateController { ...@@ -253,4 +256,6 @@ public class TemplateController {
return service.getAllTemplateByCode(code, PageRequest.of(page, size)); return service.getAllTemplateByCode(code, PageRequest.of(page, size));
} }
} }
package kz.project.printedFormsService.data.repository; package kz.project.printedFormsService.data.repository;
import kz.project.printedFormsService.data.entity.DocumentEntity;
import kz.project.printedFormsService.data.entity.dict.DocumentCountByTemplate; import kz.project.printedFormsService.data.entity.dict.DocumentCountByTemplate;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
...@@ -8,6 +9,7 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; ...@@ -8,6 +9,7 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -82,4 +84,10 @@ public class DocumentJdbcRepository { ...@@ -82,4 +84,10 @@ public class DocumentJdbcRepository {
.count(rs.getInt("count")) .count(rs.getInt("count"))
.build()); .build());
} }
public void saveDocument(DocumentEntity entity){
Map<String, Object> template_id = Map.of("template_id", entity.getTemplate().getId(), "project", entity.getProject(), "created_at", entity.getCreatedAt());
String sql = "INSERT INTO template_schema.document (template_id, project, created_at) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, template_id);
}
} }
package kz.project.printedFormsService.data.repository; package kz.project.printedFormsService.data.repository;
import kz.project.printedFormsService.data.entity.DocumentEntity; import kz.project.printedFormsService.data.entity.DocumentEntity;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import java.time.LocalDate; import java.util.Optional;
import java.util.Map;
import java.util.Set;
@Repository @Repository
public interface DocumentRepository extends JpaRepository<DocumentEntity, Long> { public interface DocumentRepository extends JpaRepository<DocumentEntity, Long> {
} }
package kz.project.printedFormsService.service; package kz.project.printedFormsService.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import kz.project.printedFormsService.data.dto.DocumentByDateDto; import kz.project.printedFormsService.data.dto.DocumentByDateDto;
import kz.project.printedFormsService.data.dto.DocumentByTemplateDto; import kz.project.printedFormsService.data.dto.DocumentByTemplateDto;
import kz.project.printedFormsService.exception.ValidationException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.time.LocalDate; import java.time.LocalDate;
...@@ -14,4 +16,5 @@ public interface DocumentsService { ...@@ -14,4 +16,5 @@ public interface DocumentsService {
@NotNull @NotNull
DocumentByTemplateDto getDocumentCountsByTemplate(@NotNull LocalDate startDate, @NotNull LocalDate endDate, @NotNull Set<String> projects); DocumentByTemplateDto getDocumentCountsByTemplate(@NotNull LocalDate startDate, @NotNull LocalDate endDate, @NotNull Set<String> projects);
void saveDocument(Long templateId) throws ValidationException, JsonProcessingException;
} }
...@@ -3,6 +3,7 @@ package kz.project.printedFormsService.service; ...@@ -3,6 +3,7 @@ package kz.project.printedFormsService.service;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import kz.project.printedFormsService.data.dto.TemplateResponseDto; import kz.project.printedFormsService.data.dto.TemplateResponseDto;
import kz.project.printedFormsService.data.dto.TemplateShortDto; import kz.project.printedFormsService.data.dto.TemplateShortDto;
import kz.project.printedFormsService.data.entity.TemplateEntity;
import kz.project.printedFormsService.exception.ValidationException; import kz.project.printedFormsService.exception.ValidationException;
import kz.project.printedFormsService.data.dto.TemplateDto; import kz.project.printedFormsService.data.dto.TemplateDto;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
...@@ -27,4 +28,8 @@ public interface TemplateService { ...@@ -27,4 +28,8 @@ public interface TemplateService {
TemplateDto getTemplateData(Long id) throws ValidationException; TemplateDto getTemplateData(Long id) throws ValidationException;
Page<TemplateDto> getAllTemplateByCode(String code, Pageable pageable) throws ValidationException; Page<TemplateDto> getAllTemplateByCode(String code, Pageable pageable) throws ValidationException;
TemplateEntity getTemplateEntity(Long id) throws ValidationException;
} }
package kz.project.printedFormsService.service.impl; package kz.project.printedFormsService.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import kz.project.printedFormsService.data.dto.DocumentByDateDto; import kz.project.printedFormsService.data.dto.DocumentByDateDto;
import kz.project.printedFormsService.data.dto.DocumentByDateDto.DocumentCount; import kz.project.printedFormsService.data.dto.DocumentByDateDto.DocumentCount;
import kz.project.printedFormsService.data.dto.DocumentByTemplateDto; import kz.project.printedFormsService.data.dto.DocumentByTemplateDto;
import kz.project.printedFormsService.data.entity.DocumentEntity;
import kz.project.printedFormsService.data.entity.TemplateEntity;
import kz.project.printedFormsService.data.repository.DocumentJdbcRepository; import kz.project.printedFormsService.data.repository.DocumentJdbcRepository;
import kz.project.printedFormsService.data.repository.TemplateRepository;
import kz.project.printedFormsService.exception.ValidationException;
import kz.project.printedFormsService.service.DocumentsService; import kz.project.printedFormsService.service.DocumentsService;
import kz.project.printedFormsService.service.TemplateService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
...@@ -18,6 +25,7 @@ import java.util.Set; ...@@ -18,6 +25,7 @@ import java.util.Set;
public class DocumentsServiceImpl implements DocumentsService { public class DocumentsServiceImpl implements DocumentsService {
private final DocumentJdbcRepository documentJdbcRepository; private final DocumentJdbcRepository documentJdbcRepository;
private final TemplateService templateService;
@Override @Override
public @NotNull DocumentByDateDto getDocumentCountsByDate(@NotNull LocalDate startDate, @NotNull LocalDate endDate, @NotNull Set<String> projects) { public @NotNull DocumentByDateDto getDocumentCountsByDate(@NotNull LocalDate startDate, @NotNull LocalDate endDate, @NotNull Set<String> projects) {
...@@ -60,4 +68,16 @@ public class DocumentsServiceImpl implements DocumentsService { ...@@ -60,4 +68,16 @@ public class DocumentsServiceImpl implements DocumentsService {
.build() .build()
); );
} }
@Override
public void saveDocument(Long templateId) throws ValidationException, JsonProcessingException {
TemplateEntity templateEntity = templateService.getTemplateEntity(templateId);
DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setTemplate(templateEntity);
documentEntity.setProject(templateEntity.getProject());
documentEntity.setCreatedAt(LocalDateTime.now());
documentJdbcRepository.saveDocument(documentEntity);
}
} }
...@@ -119,6 +119,11 @@ public class TemplateServiceImpl implements TemplateService { ...@@ -119,6 +119,11 @@ public class TemplateServiceImpl implements TemplateService {
throw new ValidationException("Не переданы обязательные поля", 13); throw new ValidationException("Не переданы обязательные поля", 13);
} }
@Override
public TemplateEntity getTemplateEntity(Long id) throws ValidationException {
return repository.findById(id).orElseThrow(() -> new ValidationException("Не найден шаблон с таким идентификатором", 13));
}
private TemplateEntity createTemplateEntity(TemplateDto dto, List<MultipartFile> files) throws IOException, ValidationException { private TemplateEntity createTemplateEntity(TemplateDto dto, List<MultipartFile> files) throws IOException, ValidationException {
TemplateEntity templateEntity = new TemplateEntity(); TemplateEntity templateEntity = new TemplateEntity();
if (dto.getTemplateId() != null) { if (dto.getTemplateId() != null) {
......
...@@ -39,8 +39,16 @@ spring: ...@@ -39,8 +39,16 @@ spring:
# enabled: ${FLYWAY_ENABLED:true} # enabled: ${FLYWAY_ENABLED:true}
# baseline-on-migrate: ${FLYWAY_BASELINE:true} # baseline-on-migrate: ${FLYWAY_BASELINE:true}
# out-of-order: ${FLYWAY_OUT_OF_ORDER:true} # out-of-order: ${FLYWAY_OUT_OF_ORDER:true}
rabbitmq:
host: ${RABBITMQ_HOST:localhost}
port: ${RABBITMQ_PORT:5672}
username: ${RABBITMQ_USERNAME:guest}
password: ${RABBITMQ_PASSWORD:guest}
rabbitmq:
document_queue: ${RABBITMQ_DOCUMENT_QUEUE:queue_document_save}
server: server:
port: 8081 port: 8081
...@@ -71,4 +79,4 @@ management: ...@@ -71,4 +79,4 @@ management:
liquibase: liquibase:
enabled: false enabled: false
beans: beans:
enabled: false enabled: false
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment