From 90498a2cd1362692e3a79668f169772f4a70c715 Mon Sep 17 00:00:00 2001 From: Blandine Bajard <83599148+BlandineBajard@users.noreply.github.com> Date: Wed, 19 Jan 2022 16:01:44 +0100 Subject: [PATCH] menu controller et repo --- .../organizee/controller/MenuController.java | 90 +++++++++++++++++++ src/main/java/fr/organizee/model/Menu.java | 4 +- .../organizee/repository/MenuRepository.java | 15 +++- 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/main/java/fr/organizee/controller/MenuController.java diff --git a/src/main/java/fr/organizee/controller/MenuController.java b/src/main/java/fr/organizee/controller/MenuController.java new file mode 100644 index 0000000..f4e4ec8 --- /dev/null +++ b/src/main/java/fr/organizee/controller/MenuController.java @@ -0,0 +1,90 @@ +package fr.organizee.controller; + +import fr.organizee.model.Contact; +import fr.organizee.model.Menu; +import fr.organizee.repository.MenuRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import javax.persistence.EntityNotFoundException; +import java.util.List; +import java.util.Optional; + +@RestController +@CrossOrigin("*") +@RequestMapping("/menus") +public class MenuController { + + @Autowired + private MenuRepository menuRepository; + + @GetMapping(value = "/{id}") + @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") + public ResponseEntity findById(@PathVariable int id){ + Optional menu = null; + try + { + menu = menuRepository.findById(id); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } + + return ResponseEntity.status(HttpStatus.OK).body(menu); + } + + @GetMapping(value = "team/{team_id}") + @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") + public ResponseEntity findByTeamId(@PathVariable int team_id) { + List menus = null; + try { + menus = menuRepository.FindMenusByTeam(team_id); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } + return ResponseEntity.status(HttpStatus.OK).body(menus); + } + + @PostMapping(value="/add") + @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") + public ResponseEntity addMenu(@RequestBody Menu menu){ + Menu resultMenu = null; + try { + resultMenu = menuRepository.saveAndFlush(menu); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.CREATED).body(resultMenu); + } + + @PutMapping("/update/{id}") + @PreAuthorize("hasRole('ROLE_PARENT')") + public ResponseEntity updateMenu(@RequestBody Menu menu, @PathVariable Integer id) throws Exception { + Menu resultMenu = null; + try { + resultMenu = menuRepository.save(menu); + + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.OK).body(menuRepository); + } + + @DeleteMapping(value = "/delete/{id}") + @PreAuthorize("hasRole('ROLE_PARENT')") + public ResponseEntity deleteMenu(@PathVariable int id){ + try { + menuRepository.delete(menuRepository.getById(id)); + return ResponseEntity.status(HttpStatus.OK).body("Menu effacé !"); + + } catch (EntityNotFoundException e) { + + return ResponseEntity.status(HttpStatus.OK).body("Menu introuvable !"); + } + } +} + diff --git a/src/main/java/fr/organizee/model/Menu.java b/src/main/java/fr/organizee/model/Menu.java index 67b1531..b486e2b 100644 --- a/src/main/java/fr/organizee/model/Menu.java +++ b/src/main/java/fr/organizee/model/Menu.java @@ -12,6 +12,7 @@ public class Menu { private int id; private String libelle; private LocalDate dateMenu; + private int validationProposition; @ManyToOne(cascade = CascadeType.MERGE) @JoinColumn(name="TEAM_ID") @JsonIgnoreProperties("menu") @@ -22,9 +23,10 @@ public class Menu { public Menu() { } - public Menu(String libelle, LocalDate dateMenu) { + public Menu(String libelle, LocalDate dateMenu, int validationProposition) { this.libelle = libelle; this.dateMenu = dateMenu; + this.validationProposition=validationProposition; } public int getId() { diff --git a/src/main/java/fr/organizee/repository/MenuRepository.java b/src/main/java/fr/organizee/repository/MenuRepository.java index b9bb652..1f22d7d 100644 --- a/src/main/java/fr/organizee/repository/MenuRepository.java +++ b/src/main/java/fr/organizee/repository/MenuRepository.java @@ -1,4 +1,17 @@ package fr.organizee.repository; -public interface MenuRepository { +import fr.organizee.model.Contact; +import fr.organizee.model.Menu; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface MenuRepository extends JpaRepository { + + @Query(value = "select * from menu where team_id = :team_id", nativeQuery = true) + List FindMenusByTeam(@Param("team_id") int team_id); }