Merge pull request #16 from HediMjid/Blandine

menu controller et repo
This commit is contained in:
Blandine Bajard 2022-01-19 16:04:08 +01:00 committed by GitHub
commit 275880850d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 2 deletions

View File

@ -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> 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<Menu> 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 !");
}
}
}

View File

@ -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() {

View File

@ -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 <Menu, Integer> {
@Query(value = "select * from menu where team_id = :team_id", nativeQuery = true)
List<Menu> FindMenusByTeam(@Param("team_id") int team_id);
}