diff --git a/main/api-swagger/paths/api.v1.organizations.yaml b/main/api-swagger/paths/api.v1.organizations.yaml index 7d18073..8f58e73 100644 --- a/main/api-swagger/paths/api.v1.organizations.yaml +++ b/main/api-swagger/paths/api.v1.organizations.yaml @@ -3,7 +3,7 @@ paths: get: responses: '200': - description: OK + description: Получение перечня организаций content: application/json: schema: diff --git a/main/api-swagger/paths/api.v1.positions.yaml b/main/api-swagger/paths/api.v1.positions.yaml index 0331dad..77fb9f0 100644 --- a/main/api-swagger/paths/api.v1.positions.yaml +++ b/main/api-swagger/paths/api.v1.positions.yaml @@ -1,15 +1,32 @@ paths: api.v1.positions: get: - summary: repo.find() Возвращает полный список должностей - description: Для разных организаций должности могут повторяться. Предусмотреть пагинацию при получении должностей, список может быть огромным + summary: |- + repo.findByOrganization() + description: |- + __Возвращает перечень должностей по организации__ + - можно получить из объекта Page + + Для разных организаций должности могут повторяться. + - Не отправляет параметры с запросом. + parameters: + - name: id + in: query + required: false + schema: + type: integer + format: int64 + default: 154 responses: '200': description: Получение списка должностей content: application/json: schema: + type: array $ref: "../components.yaml#/components/schemas/position" + 400: + description: не верные параметры запроса post: summary: repo.create() Создает в базе запись о новой должности description: Предварительно должна существовать организация. Должность привязываетс для каждой организации, для разных организаций должности могут повторяться. diff --git a/main/pom.xml b/main/pom.xml index 55037da..04a67bc 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -62,7 +62,7 @@ org.springframework.boot spring-boot-starter-data-jpa - + org.springframework.boot spring-boot-starter-test diff --git a/main/src/main/java/gsp/technologies/main/api/organization/OrganizationController.java b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationApiController.java similarity index 75% rename from main/src/main/java/gsp/technologies/main/api/organization/OrganizationController.java rename to main/src/main/java/gsp/technologies/main/api/organization/OrganizationApiController.java index 3ff2177..aba4da2 100644 --- a/main/src/main/java/gsp/technologies/main/api/organization/OrganizationController.java +++ b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationApiController.java @@ -13,21 +13,23 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import gsp.technologies.main.api.position.PositionEntity; - @RestController @RequestMapping(path = "/api/v1/organizations") -public class OrganizationController { - private static final Logger log = LoggerFactory.getLogger(OrganizationController.class); +public class OrganizationApiController { + private static final Logger log = LoggerFactory.getLogger(OrganizationApiController.class); @Autowired - private OrganizationRepository organizationRepo; + private OrganizationService organizationService; + + public OrganizationApiController(OrganizationService organizationService) { + this.organizationService = organizationService; + } @CrossOrigin @GetMapping("") public ResponseEntity getOrganizations() { log.info("GET /organizations"); - return new ResponseEntity<>(organizationRepo.findAll(), HttpStatus.OK); + return new ResponseEntity<>(organizationService.findAll(), HttpStatus.OK); } @CrossOrigin @@ -36,7 +38,7 @@ public class OrganizationController { produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity saveOrganization(@RequestBody OrganizationEntity organization) { log.info("POST /organization, organization: {}", organization); - organizationRepo.save(organization); + organizationService.save(organization); return new ResponseEntity<>(organization, HttpStatus.CREATED); } diff --git a/main/src/main/java/gsp/technologies/main/api/organization/OrganizationEntity.java b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationEntity.java index f839741..092b641 100644 --- a/main/src/main/java/gsp/technologies/main/api/organization/OrganizationEntity.java +++ b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationEntity.java @@ -1,5 +1,6 @@ package gsp.technologies.main.api.organization; +import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -18,5 +19,7 @@ public class OrganizationEntity { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; + + @Column(unique = true) private String name; //наименование организации } diff --git a/main/src/main/java/gsp/technologies/main/api/organization/OrganizationFaceController.java b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationFaceController.java new file mode 100644 index 0000000..fe5989c --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationFaceController.java @@ -0,0 +1,23 @@ +package gsp.technologies.main.api.organization; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping(path = "/organizations") +public class OrganizationFaceController { + private static final Logger log = LoggerFactory.getLogger(OrganizationFaceController.class); + + /** + * Главная страница для работы с перечнем организаций + * @return + */ + @GetMapping("") + public String organizations() { + log.info("GET /organizations"); + return "organizations/main"; + } +} diff --git a/main/src/main/java/gsp/technologies/main/api/organization/OrganizationService.java b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationService.java index aa30a98..6963000 100644 --- a/main/src/main/java/gsp/technologies/main/api/organization/OrganizationService.java +++ b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationService.java @@ -1,5 +1,7 @@ package gsp.technologies.main.api.organization; +import java.util.Collection; + import org.springframework.stereotype.Service; @Service @@ -9,4 +11,12 @@ public class OrganizationService { public OrganizationService(OrganizationRepository repo) { this.repo = repo; } + + public Collection findAll() { + return repo.findAll(); + } + + public OrganizationEntity save(OrganizationEntity organization) { + return repo.save(organization); + } } diff --git a/main/src/main/java/gsp/technologies/main/api/position/PositionController.java b/main/src/main/java/gsp/technologies/main/api/position/PositionController.java index 6cadbfb..fce7cf6 100644 --- a/main/src/main/java/gsp/technologies/main/api/position/PositionController.java +++ b/main/src/main/java/gsp/technologies/main/api/position/PositionController.java @@ -3,6 +3,10 @@ package gsp.technologies.main.api.position; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +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.MediaType; import org.springframework.http.ResponseEntity; @@ -11,6 +15,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -18,22 +23,58 @@ import org.springframework.web.bind.annotation.RestController; public class PositionController { private static final Logger log = LoggerFactory.getLogger(PositionController.class); @Autowired - private PositionRepository positionRepo; + private PositionService positionService; + public PositionController(PositionService positionService) { + this.positionService = positionService; + } + + /** + * Получение перечня должностей + * @param number + * @param size + * @param sortBy + * @return + */ @CrossOrigin @GetMapping("") - public ResponseEntity getPositions() { + public ResponseEntity getPositions( + @RequestParam(name = "id", required = false, defaultValue = "0") Long id, + @RequestParam(name = "number", required = false, defaultValue = "0") int number, + @RequestParam(name = "size", required = false, defaultValue = "10") int size, + @RequestParam(name = "sortBy", required = false, defaultValue = "name") String sortBy) { log.info("GET /positions"); - return new ResponseEntity<>(positionRepo.findAll(), HttpStatus.OK); + + Sort sort = Sort.by(sortBy); + Pageable of = PageRequest.of(number, size, sort); + + if (id != 0) { + log.info("GET /positions, organizationId: {}", id); + Page page = positionService.findByOrganizationId(of, id); + return new ResponseEntity<>(page, HttpStatus.OK); + // return new ResponseEntity>>(assembler.toModel(page), HttpStatus.OK); + } + Page page = positionService.findAll(of); + return new ResponseEntity<>(page, HttpStatus.OK); } + /** + * Добавление новой должности + * @param position + * @return + */ @CrossOrigin @PostMapping(path = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity savePosition(@RequestBody PositionEntity position) { - log.info("POST /position, position: {}", position); - positionRepo.save(position); + log.info("POST /positions, position: {}", position); + try { + positionService.save(position); + } catch (Exception e) { + log.error("POST /positions", e.getMessage()); + return new ResponseEntity<>(e.getMessage(), HttpStatus.OK); + } return new ResponseEntity<>(position, HttpStatus.CREATED); } } diff --git a/main/src/main/java/gsp/technologies/main/api/position/PositionEntity.java b/main/src/main/java/gsp/technologies/main/api/position/PositionEntity.java index eac24e6..5b50520 100644 --- a/main/src/main/java/gsp/technologies/main/api/position/PositionEntity.java +++ b/main/src/main/java/gsp/technologies/main/api/position/PositionEntity.java @@ -6,6 +6,7 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToOne; import lombok.AllArgsConstructor; import lombok.Builder; @@ -29,7 +30,7 @@ public class PositionEntity { private Long id; private String name; //наименование должности работника - @OneToOne(fetch = FetchType.EAGER, optional = false) + @ManyToOne(fetch = FetchType.EAGER, optional = false) @JoinColumn(name = "organization", referencedColumnName = "id") @OnDelete(action = OnDeleteAction.CASCADE) private OrganizationEntity organization; diff --git a/main/src/main/java/gsp/technologies/main/api/position/PositionRepository.java b/main/src/main/java/gsp/technologies/main/api/position/PositionRepository.java index ee019f7..d33286c 100644 --- a/main/src/main/java/gsp/technologies/main/api/position/PositionRepository.java +++ b/main/src/main/java/gsp/technologies/main/api/position/PositionRepository.java @@ -1,9 +1,13 @@ package gsp.technologies.main.api.position; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface PositionRepository extends JpaRepository { + Page findByOrganizationId(Pageable of, long organizationId); + } diff --git a/main/src/main/java/gsp/technologies/main/api/position/PositionService.java b/main/src/main/java/gsp/technologies/main/api/position/PositionService.java index 8a31ccb..b4e1310 100644 --- a/main/src/main/java/gsp/technologies/main/api/position/PositionService.java +++ b/main/src/main/java/gsp/technologies/main/api/position/PositionService.java @@ -1,5 +1,7 @@ package gsp.technologies.main.api.position; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @Service @@ -9,4 +11,16 @@ public class PositionService { public PositionService(PositionRepository repo) { this.repo = repo; } + + public Page findByOrganizationId(Pageable of, Long id) { + return repo.findByOrganizationId(of, id); + } + + public Page findAll(Pageable of) { + return repo.findAll(of); + } + + public void save(PositionEntity position) { + repo.save(position); + } } diff --git a/main/src/main/java/gsp/technologies/main/mainframe/MainframeController.java b/main/src/main/java/gsp/technologies/main/mainframe/MainframeController.java index f3c97fb..e7928e1 100644 --- a/main/src/main/java/gsp/technologies/main/mainframe/MainframeController.java +++ b/main/src/main/java/gsp/technologies/main/mainframe/MainframeController.java @@ -5,9 +5,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; - -import jakarta.servlet.ServletException; import org.springframework.web.bind.annotation.GetMapping; diff --git a/main/src/main/resources/static/hello/pictures/usecase.png b/main/src/main/resources/static/hello/pictures/usecase.png index 8e3e921..e468152 100644 Binary files a/main/src/main/resources/static/hello/pictures/usecase.png and b/main/src/main/resources/static/hello/pictures/usecase.png differ diff --git a/main/src/main/resources/templates/organizations/main.html b/main/src/main/resources/templates/organizations/main.html new file mode 100644 index 0000000..42e4ec6 --- /dev/null +++ b/main/src/main/resources/templates/organizations/main.html @@ -0,0 +1,19 @@ + + + + + + exam-main-hello + + + +
+
+ +

organizations

+ + + \ No newline at end of file diff --git a/out/docs/usecase/usecase.png b/out/docs/usecase/usecase.png index 8e3e921..e468152 100644 Binary files a/out/docs/usecase/usecase.png and b/out/docs/usecase/usecase.png differ