From ff3e485a7654e010bd32dd5f56cc2be267586a50 Mon Sep 17 00:00:00 2001 From: esoe Date: Thu, 14 Nov 2024 15:20:45 +0300 Subject: [PATCH] qq --- .../main/account/AccountController.java | 75 ---------- .../main/account/dto/AccountDTO.java | 22 --- .../main/account/dto/OrganizationDTO.java | 17 --- .../main/account/dto/PositionDTO.java | 18 --- .../main/api/account/AccountEntity.java | 36 +++++ .../api/account/AccountFaceController.java | 131 ++++++++++++++++++ .../main/api/account/AccountRepository.java | 9 ++ .../main/api/account/AccountService.java | 20 +++ .../main/api/course/CourseEntity.java | 24 ++++ .../main/api/course/CourseFaceController.java | 53 +++++++ .../main/api/course/CourseRepository.java | 9 ++ .../main/api/course/CourseService.java | 23 +++ .../OrganizationFaceController.java | 77 +++++++++- .../api/organization/OrganizationService.java | 8 ++ ...roller.java => PositionApiController.java} | 11 +- .../main/api/position/PositionEntity.java | 3 +- .../api/position/PositionFaceController.java | 79 +++++++++++ .../main/api/position/PositionRepository.java | 4 + .../main/api/position/PositionService.java | 16 +++ .../main/{ => api}/quiz/QuizController.java | 10 +- .../main/api/target/TargetEntity.java | 46 ++++++ .../main/api/target/TargetFaceController.java | 91 ++++++++++++ .../main/api/target/TargetRepository.java | 9 ++ .../main/api/target/TargetService.java | 30 ++++ .../main/course/CourseController.java | 36 ----- .../main/course/dto/CourseDTO.java | 19 --- .../main/mainframe/MainframeController.java | 37 ++++- .../src/main/resources/templates/account.html | 2 +- main/src/main/resources/templates/course.html | 6 +- .../templates/courses/fragments/main.html | 58 ++++++++ .../resources/templates/courses/main.html | 25 ++++ main/src/main/resources/templates/error.html | 26 ++++ .../templates/fragments/account.html | 7 +- .../templates/fragments/controls.html | 39 +++--- .../main/resources/templates/mainframe.html | 34 ++++- .../templates/organizations/edit.html | 23 +++ .../organizations/fragments/main.html | 57 ++++++++ .../templates/organizations/main.html | 10 +- .../templates/positions/fragments/main.html | 64 +++++++++ .../resources/templates/positions/main.html | 25 ++++ .../templates/targets/fragments/main.html | 104 ++++++++++++++ .../resources/templates/targets/main.html | 56 ++++++++ 42 files changed, 1216 insertions(+), 233 deletions(-) delete mode 100644 main/src/main/java/gsp/technologies/main/account/AccountController.java delete mode 100644 main/src/main/java/gsp/technologies/main/account/dto/AccountDTO.java delete mode 100644 main/src/main/java/gsp/technologies/main/account/dto/OrganizationDTO.java delete mode 100644 main/src/main/java/gsp/technologies/main/account/dto/PositionDTO.java create mode 100644 main/src/main/java/gsp/technologies/main/api/account/AccountEntity.java create mode 100644 main/src/main/java/gsp/technologies/main/api/account/AccountFaceController.java create mode 100644 main/src/main/java/gsp/technologies/main/api/account/AccountRepository.java create mode 100644 main/src/main/java/gsp/technologies/main/api/account/AccountService.java create mode 100644 main/src/main/java/gsp/technologies/main/api/course/CourseEntity.java create mode 100644 main/src/main/java/gsp/technologies/main/api/course/CourseFaceController.java create mode 100644 main/src/main/java/gsp/technologies/main/api/course/CourseRepository.java create mode 100644 main/src/main/java/gsp/technologies/main/api/course/CourseService.java rename main/src/main/java/gsp/technologies/main/api/position/{PositionController.java => PositionApiController.java} (89%) create mode 100644 main/src/main/java/gsp/technologies/main/api/position/PositionFaceController.java rename main/src/main/java/gsp/technologies/main/{ => api}/quiz/QuizController.java (69%) create mode 100644 main/src/main/java/gsp/technologies/main/api/target/TargetEntity.java create mode 100644 main/src/main/java/gsp/technologies/main/api/target/TargetFaceController.java create mode 100644 main/src/main/java/gsp/technologies/main/api/target/TargetRepository.java create mode 100644 main/src/main/java/gsp/technologies/main/api/target/TargetService.java delete mode 100644 main/src/main/java/gsp/technologies/main/course/CourseController.java delete mode 100644 main/src/main/java/gsp/technologies/main/course/dto/CourseDTO.java create mode 100644 main/src/main/resources/templates/courses/fragments/main.html create mode 100644 main/src/main/resources/templates/courses/main.html create mode 100644 main/src/main/resources/templates/error.html create mode 100644 main/src/main/resources/templates/organizations/edit.html create mode 100644 main/src/main/resources/templates/organizations/fragments/main.html create mode 100644 main/src/main/resources/templates/positions/fragments/main.html create mode 100644 main/src/main/resources/templates/positions/main.html create mode 100644 main/src/main/resources/templates/targets/fragments/main.html create mode 100644 main/src/main/resources/templates/targets/main.html diff --git a/main/src/main/java/gsp/technologies/main/account/AccountController.java b/main/src/main/java/gsp/technologies/main/account/AccountController.java deleted file mode 100644 index 4eaffb1..0000000 --- a/main/src/main/java/gsp/technologies/main/account/AccountController.java +++ /dev/null @@ -1,75 +0,0 @@ -package gsp.technologies.main.account; - -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.context.request.RequestContextHolder; - -import gsp.technologies.main.account.dto.AccountDTO; -import gsp.technologies.main.course.dto.CourseDTO; -import gsp.technologies.main.account.dto.OrganizationDTO; -import gsp.technologies.main.account.dto.PositionDTO; -import gsp.technologies.main.code.Code; - -/** - * Контроллер формы аккаунта - * возвращает thymeleaf шаблон - */ -@Controller -@RequestMapping(path = "/account") -public class AccountController { - private static final Logger log = LoggerFactory.getLogger(AccountController.class); - - @GetMapping("") - public String account(Model model) { - log.info("GET /account"); - //данные аккаунта (id, organization, position) - String id = 1 + ""; - String code = Code.encode(id); - OrganizationDTO organization = OrganizationDTO.builder() - .id(1 + "") - .name("ГСП-Т") - .build(); - PositionDTO position = PositionDTO.builder() - .id(1 + "") - .name("Сварщик") - .organization(organization) - .build(); - String sessionid = RequestContextHolder.currentRequestAttributes().getSessionId(); - List courses = List.of( - CourseDTO.builder() - .id(1 + "") - .name("Охрана труда") - .passed(true) - .build(), - CourseDTO.builder() - .id(2 + "") - .name("Работы на высоте") - .passed(false) - .build(), - CourseDTO.builder() - .id(3 + "") - .name("Первая помощь") - .passed(true) - .build() - ); - AccountDTO account = AccountDTO.builder() - .id(id) - .code(code) - .position(position) - .sessionid(sessionid) - .courses(courses) - .build(); - - // log.info("account: {}", account); - model.addAttribute("account", account); - log.info("Акаунт добавлен в модель: {}", account); - - return "account"; - } -} diff --git a/main/src/main/java/gsp/technologies/main/account/dto/AccountDTO.java b/main/src/main/java/gsp/technologies/main/account/dto/AccountDTO.java deleted file mode 100644 index 1b48b31..0000000 --- a/main/src/main/java/gsp/technologies/main/account/dto/AccountDTO.java +++ /dev/null @@ -1,22 +0,0 @@ -package gsp.technologies.main.account.dto; - -import java.io.Serializable; -import java.util.List; - -import gsp.technologies.main.course.dto.CourseDTO; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Builder -@NoArgsConstructor -@AllArgsConstructor -@Data -public class AccountDTO implements Serializable { - private String id; - private String code; - private PositionDTO position; - private String sessionid; - private List courses; -} diff --git a/main/src/main/java/gsp/technologies/main/account/dto/OrganizationDTO.java b/main/src/main/java/gsp/technologies/main/account/dto/OrganizationDTO.java deleted file mode 100644 index 20e92c2..0000000 --- a/main/src/main/java/gsp/technologies/main/account/dto/OrganizationDTO.java +++ /dev/null @@ -1,17 +0,0 @@ -package gsp.technologies.main.account.dto; - -import java.io.Serializable; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -@AllArgsConstructor -@Builder -@Data -public class OrganizationDTO implements Serializable { - private String id; - private String name; //наименование организации -} diff --git a/main/src/main/java/gsp/technologies/main/account/dto/PositionDTO.java b/main/src/main/java/gsp/technologies/main/account/dto/PositionDTO.java deleted file mode 100644 index 7f9c586..0000000 --- a/main/src/main/java/gsp/technologies/main/account/dto/PositionDTO.java +++ /dev/null @@ -1,18 +0,0 @@ -package gsp.technologies.main.account.dto; - -import java.io.Serializable; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -@AllArgsConstructor -@Builder -@Data -public class PositionDTO implements Serializable { - private String id; - private String name; - private OrganizationDTO organization; -} \ No newline at end of file diff --git a/main/src/main/java/gsp/technologies/main/api/account/AccountEntity.java b/main/src/main/java/gsp/technologies/main/api/account/AccountEntity.java new file mode 100644 index 0000000..1cf0396 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/account/AccountEntity.java @@ -0,0 +1,36 @@ +package gsp.technologies.main.api.account; + +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +import gsp.technologies.main.api.position.PositionEntity; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Entity +public class AccountEntity { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Long id; + + private String code; + + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @JoinColumn(name = "position", referencedColumnName = "id") + @OnDelete(action = OnDeleteAction.CASCADE) + private PositionEntity position; + +} diff --git a/main/src/main/java/gsp/technologies/main/api/account/AccountFaceController.java b/main/src/main/java/gsp/technologies/main/api/account/AccountFaceController.java new file mode 100644 index 0000000..f6d5edb --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/account/AccountFaceController.java @@ -0,0 +1,131 @@ +package gsp.technologies.main.api.account; + +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import gsp.technologies.main.api.course.CourseEntity; +import gsp.technologies.main.api.organization.OrganizationService; +import gsp.technologies.main.api.position.PositionEntity; +import gsp.technologies.main.api.position.PositionService; +import gsp.technologies.main.api.target.TargetService; +import gsp.technologies.main.code.Code; + +/** + * Контроллер формы аккаунта + * возвращает thymeleaf шаблон + */ +@Controller +@RequestMapping(path = "/account") +public class AccountFaceController { + private static final Logger log = LoggerFactory.getLogger(AccountFaceController.class); + @Autowired + private OrganizationService organizationService; + + @Autowired + private PositionService positionService; + + @Autowired + private AccountService accountService; + + @Autowired + private TargetService targetService; + + public AccountFaceController( + PositionService positionService + , OrganizationService organizationService + , AccountService accountService + , TargetService targetService + ) { + this.positionService = positionService; + this.organizationService = organizationService; + this.accountService = accountService; + this.targetService = targetService; + } + + /** + * Форма аккаунта: + * - данные аккаунта (id, organization, position) + * - список курсов аккаунта + * + * @param model + * @param accountid + * @param positionid + * @return + */ + @GetMapping("") + public String account(Model model, + @RequestParam(name = "accountid", required = false) String accountid, + @RequestParam(name = "positionid", required = false) String positionid + ) { + log.info("GET /account"); + //данные аккаунта (id, organization, position) + log.info("accountid: " + accountid); + log.info("positionid: " + positionid); + + if (accountid != null) { + log.info("accountid: " + accountid); + try{ + //передаем в модель данные аккаунта + AccountEntity account = accountService.findById(Long.valueOf(accountid)); + model.addAttribute("account", account); + + //передаем в модель перечень доступных курсов + Collection courses = targetService.findAllByPosition(account.getPosition()); + model.addAttribute("courses", courses); + //передаем в модель статусы прохождения курсов + + return "account"; + } catch (Exception e) { + StringBuilder msg = new StringBuilder(); + msg.append("Не удалось получить данные аккаунта: " + e.getMessage()); + log.error(msg.toString()); + model.addAttribute("error", msg.toString()); + return "error"; + } + } + + if (positionid != null) { + try{ + log.info("positionid: " + positionid); + PositionEntity position = positionService.findById(Long.valueOf(positionid)); + //обработать вход нового пользователя + AccountEntity account = AccountEntity.builder() + .position(position) + .build(); + + //сохранить в базе данных + account = accountService.save(account); + log.info("сохраняем account: " + account); + + //получить id аккаунта + String code = Code.encode(account.getId() + ""); + account.setCode(code); + //обновили аккаунт в базе данных + account = accountService.save(account); + + model.addAttribute("account", account); + return "account"; + } catch (Exception e) { + StringBuilder msg = new StringBuilder(); + msg.append("Не удалось получить данные аккаунта: " + e.getMessage()); + log.error(msg.toString()); + model.addAttribute("error", msg.toString()); + return "error"; + } + } + //вернуть страницу ошибки + StringBuilder msg = new StringBuilder(); + msg.append("Не указаны данные для доступа к аккаунту."); + log.error(msg.toString()); + model.addAttribute("error", msg.toString()); + return "error"; + } +} diff --git a/main/src/main/java/gsp/technologies/main/api/account/AccountRepository.java b/main/src/main/java/gsp/technologies/main/api/account/AccountRepository.java new file mode 100644 index 0000000..f0b6fbc --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/account/AccountRepository.java @@ -0,0 +1,9 @@ +package gsp.technologies.main.api.account; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AccountRepository extends JpaRepository { + +} diff --git a/main/src/main/java/gsp/technologies/main/api/account/AccountService.java b/main/src/main/java/gsp/technologies/main/api/account/AccountService.java new file mode 100644 index 0000000..87ec70f --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/account/AccountService.java @@ -0,0 +1,20 @@ +package gsp.technologies.main.api.account; + +import org.springframework.stereotype.Service; + +@Service +public class AccountService { + private final AccountRepository repo; + + public AccountService(AccountRepository repo) { + this.repo = repo; + } + + public AccountEntity save(AccountEntity account) { + return repo.save(account); + } + + public AccountEntity findById(Long id) { + return repo.findById(id).orElse(null); + } +} diff --git a/main/src/main/java/gsp/technologies/main/api/course/CourseEntity.java b/main/src/main/java/gsp/technologies/main/api/course/CourseEntity.java new file mode 100644 index 0000000..90c9779 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/course/CourseEntity.java @@ -0,0 +1,24 @@ +package gsp.technologies.main.api.course; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Entity +public class CourseEntity { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Long id; + + private String name; + +} diff --git a/main/src/main/java/gsp/technologies/main/api/course/CourseFaceController.java b/main/src/main/java/gsp/technologies/main/api/course/CourseFaceController.java new file mode 100644 index 0000000..fa75e73 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/course/CourseFaceController.java @@ -0,0 +1,53 @@ +package gsp.technologies.main.api.course; + +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import org.springframework.web.bind.annotation.PostMapping; + +@Controller +@RequestMapping(path = "/courses") +public class CourseFaceController { + + private static final Logger log = LoggerFactory.getLogger(CourseFaceController.class); + + @Autowired + private CourseService courseService; + public CourseFaceController(CourseService courseService) { + this.courseService = courseService; + } + + @GetMapping("") + public String course(Model model) { + log.info("GET /courses"); + + Collection courses = courseService.findAll(); + model.addAttribute("courses", courses); + return "courses/main"; + } + + /** + * Создание нового курса + */ + @PostMapping("") + public String create(@RequestParam(name = "name", required = true) String name) { + log.info("POST /courses"); + CourseEntity entity = new CourseEntity(); + entity.setName(name); + + courseService.save(entity); + + return "redirect:/courses"; + } + + + +} diff --git a/main/src/main/java/gsp/technologies/main/api/course/CourseRepository.java b/main/src/main/java/gsp/technologies/main/api/course/CourseRepository.java new file mode 100644 index 0000000..5eb4acf --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/course/CourseRepository.java @@ -0,0 +1,9 @@ +package gsp.technologies.main.api.course; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CourseRepository extends JpaRepository { + +} diff --git a/main/src/main/java/gsp/technologies/main/api/course/CourseService.java b/main/src/main/java/gsp/technologies/main/api/course/CourseService.java new file mode 100644 index 0000000..f5bd6e2 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/course/CourseService.java @@ -0,0 +1,23 @@ +package gsp.technologies.main.api.course; + +import java.util.Collection; + +import org.springframework.stereotype.Service; + +@Service +public class CourseService { + private final CourseRepository repo; + public CourseService(CourseRepository repo) { + this.repo = repo; + } + public Collection findAll() { + return repo.findAll(); + } + public CourseEntity save(CourseEntity entity) { + return repo.save(entity); + } + public CourseEntity findById(Long courseid) { + return repo.findById(courseid).get(); + } + +} 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 index fe5989c..39bf35d 100644 --- a/main/src/main/java/gsp/technologies/main/api/organization/OrganizationFaceController.java +++ b/main/src/main/java/gsp/technologies/main/api/organization/OrganizationFaceController.java @@ -1,23 +1,98 @@ package gsp.technologies.main.api.organization; +import java.util.Collection; +import java.util.stream.Collectors; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.PostMapping; + @Controller @RequestMapping(path = "/organizations") public class OrganizationFaceController { private static final Logger log = LoggerFactory.getLogger(OrganizationFaceController.class); + @Autowired + private OrganizationService service; + + public OrganizationFaceController(OrganizationService organizationService) { + this.service = organizationService; + } + + /** * Главная страница для работы с перечнем организаций * @return */ @GetMapping("") - public String organizations() { + public String organizations(Model model) { log.info("GET /organizations"); + // получаем список организаций + Collection organizations = service.findAll(); + // сортируем по названию + organizations = organizations.stream().sorted((o1, o2) -> o1.getName().compareTo(o2.getName())).collect(Collectors.toList()); + // передаем список организаций в шаблон + model.addAttribute("organizations", organizations); + model.addAttribute("organizationService", service); return "organizations/main"; } + /** + * Добавление новой организации + */ + @PostMapping("") + public String create(@RequestParam(name = "name", required = true) String name) { + log.info("POST /organizations"); + OrganizationEntity entity = new OrganizationEntity(); + entity.setName(name); + + service.save(entity); + + return "redirect:/organizations"; + } + /** + * Удаление организации + * @param id + */ + @GetMapping("/{id}/delete") + public String delete(@PathVariable(name = "id", required = true) Long id) { + log.info("GET /organizations/{id}/delete"); + service.deleteById(id); + return "redirect:/organizations"; + } + + /** + * Переход к форме редактирования организации + */ + @GetMapping("/{id}/edit") + public String edit(@PathVariable(name = "id", required = true) Long id, Model model) { + log.info("GET /organizations/{id}/edit"); + OrganizationEntity entity = service.findById(id); + model.addAttribute("organization", entity); + return "organizations/edit"; + } + + /** + * Редакрирование организации + */ + @PostMapping("/{id}/edit") + public String update(@PathVariable(name = "id", required = true) Long id, @RequestParam(name = "name", required = true) String name) { + log.info("POST /organizations/{id}/edit"); + OrganizationEntity entity = new OrganizationEntity(); + entity.setName(name); + entity.setId(id); + service.save(entity); + return "redirect:/organizations"; + } + + + + } 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 6963000..111a51c 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 @@ -19,4 +19,12 @@ public class OrganizationService { public OrganizationEntity save(OrganizationEntity organization) { return repo.save(organization); } + + public void deleteById(Long id) { + repo.deleteById(id); + } + + public OrganizationEntity findById(Long id) { + return repo.findById(id).get(); + } } diff --git a/main/src/main/java/gsp/technologies/main/api/position/PositionController.java b/main/src/main/java/gsp/technologies/main/api/position/PositionApiController.java similarity index 89% rename from main/src/main/java/gsp/technologies/main/api/position/PositionController.java rename to main/src/main/java/gsp/technologies/main/api/position/PositionApiController.java index fce7cf6..899df9e 100644 --- a/main/src/main/java/gsp/technologies/main/api/position/PositionController.java +++ b/main/src/main/java/gsp/technologies/main/api/position/PositionApiController.java @@ -1,5 +1,7 @@ package gsp.technologies.main.api.position; +import java.util.Collection; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -20,12 +22,12 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(path = "/api/v1/positions") -public class PositionController { - private static final Logger log = LoggerFactory.getLogger(PositionController.class); +public class PositionApiController { + private static final Logger log = LoggerFactory.getLogger(PositionApiController.class); @Autowired private PositionService positionService; - public PositionController(PositionService positionService) { + public PositionApiController(PositionService positionService) { this.positionService = positionService; } @@ -55,7 +57,8 @@ public class PositionController { // return new ResponseEntity>>(assembler.toModel(page), HttpStatus.OK); } Page page = positionService.findAll(of); - return new ResponseEntity<>(page, HttpStatus.OK); + Collection content = page.getContent(); + return new ResponseEntity<>(content, HttpStatus.OK); } /** 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 5b50520..c92863b 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 @@ -7,7 +7,6 @@ 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; import lombok.Data; @@ -35,4 +34,6 @@ public class PositionEntity { @OnDelete(action = OnDeleteAction.CASCADE) private OrganizationEntity organization; + + } diff --git a/main/src/main/java/gsp/technologies/main/api/position/PositionFaceController.java b/main/src/main/java/gsp/technologies/main/api/position/PositionFaceController.java new file mode 100644 index 0000000..740032a --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/position/PositionFaceController.java @@ -0,0 +1,79 @@ +package gsp.technologies.main.api.position; + +import java.util.Collection; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import gsp.technologies.main.api.organization.OrganizationEntity; +import gsp.technologies.main.api.organization.OrganizationService; + +@Controller +@RequestMapping(path = "/positions") +public class PositionFaceController { + private static final Logger log = LoggerFactory.getLogger(PositionFaceController.class); + + @Autowired + private OrganizationService organizationService; + + @Autowired + private PositionService positionService; + + public PositionFaceController(PositionService positionService, OrganizationService organizationService) { + this.positionService = positionService; + this.organizationService = organizationService; + } + + /** + * Главная страница работы с должностями + */ + @GetMapping("") + public String positions(Model model) { + log.info("GET /positions"); + + // получаем список организаций + Collection organizations = organizationService.findAll() + .stream().sorted((o1, o2) -> o1.getName().compareTo(o2.getName())).collect(Collectors.toList()); + // + model.addAttribute("organizations", organizations); + + //получаем список должностей + int number = 0; + int size = 100; + String sortBy = "name"; + + Sort sort = Sort.by(sortBy); + Pageable of = PageRequest.of(number, size, sort); + + Collection positions = positionService.findAll(of).getContent(); + model.addAttribute("positions", positions); + return "positions/main"; + } + + /** + * Добавление должности + */ + @PostMapping("") + public String create( + @RequestParam(name = "name", required = true) String name, + @RequestParam(name = "organizationId", required = true) Long organizationId) { + log.info("POST /positions"); + PositionEntity entity = new PositionEntity(); + entity.setName(name); + entity.setOrganization(organizationService.findById(organizationId)); + positionService.save(entity); + return "redirect:/positions"; + } + +} 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 d33286c..f087f78 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,5 +1,7 @@ package gsp.technologies.main.api.position; +import java.util.Collection; + import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; @@ -10,4 +12,6 @@ public interface PositionRepository extends JpaRepository Page findByOrganizationId(Pageable of, long organizationId); + Collection findByOrganizationId(Long id); + } 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 b4e1310..7c7e8ae 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 java.util.Collection; + import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -16,6 +18,10 @@ public class PositionService { return repo.findByOrganizationId(of, id); } + public Collection findAllByOrganizationId(Long id) { + return repo.findByOrganizationId(id); + } + public Page findAll(Pageable of) { return repo.findAll(of); } @@ -23,4 +29,14 @@ public class PositionService { public void save(PositionEntity position) { repo.save(position); } + + public Collection findAll() { + return repo.findAll(); + } + + public PositionEntity findById(Long id) { + return repo.findById(id).orElse(null); + } + + } diff --git a/main/src/main/java/gsp/technologies/main/quiz/QuizController.java b/main/src/main/java/gsp/technologies/main/api/quiz/QuizController.java similarity index 69% rename from main/src/main/java/gsp/technologies/main/quiz/QuizController.java rename to main/src/main/java/gsp/technologies/main/api/quiz/QuizController.java index 3fa3b56..292ac2d 100644 --- a/main/src/main/java/gsp/technologies/main/quiz/QuizController.java +++ b/main/src/main/java/gsp/technologies/main/api/quiz/QuizController.java @@ -1,4 +1,4 @@ -package gsp.technologies.main.quiz; +package gsp.technologies.main.api.quiz; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -8,7 +8,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import gsp.technologies.main.account.dto.AccountDTO; @Controller @RequestMapping(path = "/quiz") @@ -18,12 +17,7 @@ public class QuizController { @GetMapping("") public String quiz(Model model, @RequestParam(name = "accountid", required = true) String accountid) { log.info("GET /quiz"); - AccountDTO accountDTO = AccountDTO.builder() - .id(accountid) - .code(accountid) - .build(); - - model.addAttribute("account", accountDTO); + return "quiz"; } } diff --git a/main/src/main/java/gsp/technologies/main/api/target/TargetEntity.java b/main/src/main/java/gsp/technologies/main/api/target/TargetEntity.java new file mode 100644 index 0000000..7e62455 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/target/TargetEntity.java @@ -0,0 +1,46 @@ +package gsp.technologies.main.api.target; + +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +import gsp.technologies.main.api.course.CourseEntity; +import gsp.technologies.main.api.position.PositionEntity; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Entity +public class TargetEntity { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Long id; + + // На одну должность может быть несколько целей + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @JoinColumn(name = "position", referencedColumnName = "id") + @OnDelete(action = OnDeleteAction.CASCADE) + private PositionEntity position; + + // На один курс может быть несколько целей + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @JoinColumn(name = "course", referencedColumnName = "id") + @OnDelete(action = OnDeleteAction.CASCADE) + private CourseEntity course; + + private Integer questionsCount; + private Integer questionsLimit; + + +} diff --git a/main/src/main/java/gsp/technologies/main/api/target/TargetFaceController.java b/main/src/main/java/gsp/technologies/main/api/target/TargetFaceController.java new file mode 100644 index 0000000..d0e4440 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/target/TargetFaceController.java @@ -0,0 +1,91 @@ +package gsp.technologies.main.api.target; + +import java.util.Collection; +import java.util.Collections; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import gsp.technologies.main.api.course.CourseEntity; +import gsp.technologies.main.api.course.CourseService; +import gsp.technologies.main.api.organization.OrganizationEntity; +import gsp.technologies.main.api.organization.OrganizationService; +import gsp.technologies.main.api.position.PositionEntity; +import gsp.technologies.main.api.position.PositionService; + +@Controller +@RequestMapping(path = "/targets") +public class TargetFaceController { + private static final Logger log = LoggerFactory.getLogger(TargetFaceController.class); + + @Autowired + private TargetService targetService; + + @Autowired + private OrganizationService organizationService; + + @Autowired + private PositionService positionService; + + @Autowired + private CourseService courseService; + + public TargetFaceController(TargetService targetService, OrganizationService organizationService, PositionService positionService, CourseService courseService) { + this.targetService = targetService; + this.organizationService = organizationService; + this.positionService = positionService; + this.courseService = courseService; + } + + @GetMapping("") + public String targets(Model model) { + log.info("GET /targets"); + //передаем в шаблон перечень организаций + Collection organizations = organizationService.findAll(); + model.addAttribute("organizations", organizations); + + //передаем в шаблон перечень должностей + Collection positions = positionService.findAll(); + model.addAttribute("positions", positions); + + //передаем в шаблон перечень курсов + Collection courses = courseService.findAll(); + model.addAttribute("courses", courses); + + //передаем в шаблон перечень созданных задачь + Collection targets = targetService.findAll(); + model.addAttribute("targets", targets); + + return "targets/main"; + } + + @PostMapping("") + public String create( + @RequestParam(name = "courseid", required = true) Long courseid, + @RequestParam(name = "positionid", required = true) Long positionid, + @RequestParam(name = "questions-count", required = true) Integer questionsCount, + @RequestParam(name = "questions-limit", required = true) Integer questionsLimit + ){ + log.info("POST /targets"); + TargetEntity entity = TargetEntity.builder() + .course(courseService.findById(courseid)) + .position(positionService.findById(positionid)) + .questionsCount(questionsCount) + .questionsLimit(questionsLimit) + .build(); + + targetService.save(entity); + return "redirect:/targets"; + } + + + + +} diff --git a/main/src/main/java/gsp/technologies/main/api/target/TargetRepository.java b/main/src/main/java/gsp/technologies/main/api/target/TargetRepository.java new file mode 100644 index 0000000..d22e45f --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/target/TargetRepository.java @@ -0,0 +1,9 @@ +package gsp.technologies.main.api.target; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface TargetRepository extends JpaRepository { + +} diff --git a/main/src/main/java/gsp/technologies/main/api/target/TargetService.java b/main/src/main/java/gsp/technologies/main/api/target/TargetService.java new file mode 100644 index 0000000..e85d528 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/api/target/TargetService.java @@ -0,0 +1,30 @@ +package gsp.technologies.main.api.target; + +import java.util.Collection; + +import org.springframework.stereotype.Service; + +import gsp.technologies.main.api.course.CourseEntity; +import gsp.technologies.main.api.position.PositionEntity; + +@Service +public class TargetService { + private final TargetRepository repo; + + public TargetService(TargetRepository repo) { + this.repo = repo; + } + + public Collection findAll() { + return repo.findAll(); + } + + public TargetEntity save(TargetEntity entity) { + return repo.save(entity); + } + + public Collection findAllByPosition(PositionEntity position) { + repo.findAllByPosition(position); + } + +} diff --git a/main/src/main/java/gsp/technologies/main/course/CourseController.java b/main/src/main/java/gsp/technologies/main/course/CourseController.java deleted file mode 100644 index de8da9a..0000000 --- a/main/src/main/java/gsp/technologies/main/course/CourseController.java +++ /dev/null @@ -1,36 +0,0 @@ -package gsp.technologies.main.course; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; - -import gsp.technologies.main.account.dto.AccountDTO; -import gsp.technologies.main.code.Code; - -@Controller -@RequestMapping(path = "/courses") -public class CourseController { - private static final Logger log = LoggerFactory.getLogger(CourseController.class); - - @PostMapping("/view") - public String coursewithdata(Model model, @RequestParam(name = "courseid", required = true) String courseid, @RequestParam(name = "accountid", required = true) String accountid) { - log.info("GET /courses/view"); - log.info("Account пришедший в контроллер: {}", accountid); - - model.addAttribute("courseid", courseid + ""); - - //получить полный аккаунт из репозитория по id - AccountDTO acc = AccountDTO.builder() - .id(accountid) - .code(Code.encode(accountid)) - .build(); - model.addAttribute("account", acc); - return "course"; - } - - -} diff --git a/main/src/main/java/gsp/technologies/main/course/dto/CourseDTO.java b/main/src/main/java/gsp/technologies/main/course/dto/CourseDTO.java deleted file mode 100644 index 2df3f2d..0000000 --- a/main/src/main/java/gsp/technologies/main/course/dto/CourseDTO.java +++ /dev/null @@ -1,19 +0,0 @@ -package gsp.technologies.main.course.dto; - -import java.io.Serializable; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -@AllArgsConstructor -@Builder -@Data -public class CourseDTO implements Serializable { - - private String id; - private String name; - private Boolean passed; -} 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 e7928e1..06cb3e9 100644 --- a/main/src/main/java/gsp/technologies/main/mainframe/MainframeController.java +++ b/main/src/main/java/gsp/technologies/main/mainframe/MainframeController.java @@ -1,11 +1,25 @@ package gsp.technologies.main.mainframe; +import java.util.Collection; +import java.util.stream.Collectors; + 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.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.RequestContextHolder; +import gsp.technologies.main.api.organization.OrganizationEntity; +import gsp.technologies.main.api.organization.OrganizationService; +import gsp.technologies.main.api.position.PositionEntity; +import gsp.technologies.main.api.position.PositionService; + import org.springframework.web.bind.annotation.GetMapping; /** @@ -18,12 +32,33 @@ import org.springframework.web.bind.annotation.GetMapping; @Controller @RequestMapping(path = "/mainframe") public class MainframeController { + @Autowired + private OrganizationService organizationService; + + @Autowired + private PositionService positionService; + + public MainframeController(PositionService positionService) { + this.positionService = positionService; + } private static final Logger log = LoggerFactory.getLogger(MainframeController.class); @GetMapping("") - public String mainframe() { + public String mainframe(Model model) { log.info("GET /mainframe"); log.info("текущая сессия: {}", RequestContextHolder.currentRequestAttributes().getSessionId()); + //получаем список организаций + Collection organizations = organizationService.findAll(); + //сортировка организаций + organizations = organizations.stream().sorted((o1, o2) -> o1.getName().compareTo(o2.getName())).collect(Collectors.toList()); + //передаем список организаций в шаблон + model.addAttribute("organizations", organizations); + + //берем первую в списке организацию + OrganizationEntity organization = organizations.stream().findFirst().get(); + //получаем список должностей по организации + Collection positions = positionService.findAllByOrganizationId(organization.getId()); + model.addAttribute("positions", positions); //сравнить номер сессии с имеющимися в базе //при наличии вернуть порядковый номер существующей сессии (в 35-ричной системе счисления) diff --git a/main/src/main/resources/templates/account.html b/main/src/main/resources/templates/account.html index 460e7b5..5f37458 100644 --- a/main/src/main/resources/templates/account.html +++ b/main/src/main/resources/templates/account.html @@ -21,7 +21,7 @@ xmlns:th="http://www.thymeleaf.org"> -
+