Commit 5591dd65 authored by Alexandre Ducarne's avatar Alexandre Ducarne

Hibernate bdd

parent 449c7023
package com.RESTAPI.RESTAPI.Controllers;
import com.RESTAPI.RESTAPI.Entities.Compte;
import com.RESTAPI.RESTAPI.Repositories.CompteRepository;
import com.RESTAPI.RESTAPI.Requests.CompteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@Controller
@RequestMapping(path="/compte")
public class CompteController {
@Autowired
private CompteRepository compteRepository;
@PutMapping(path="/")
public
Compte addCompte (@RequestBody CompteRequest compteRequest) {
Compte compte = Compte.builder()
.email(compteRequest.getEmail())
.mdp(compteRequest.getMdp())
.status(compteRequest.getStatus())
.dateCreation(new Date())
.build();
compteRepository.save(compte);
return compte;
}
@GetMapping(path = "/{id}")
public @ResponseBody
Compte getCompteById(@RequestBody Integer id) {
return compteRepository.findById(id).get();
}
@GetMapping(path="/")
public @ResponseBody Iterable<Compte> getAllComptes() {
return compteRepository.findAll();
}
@DeleteMapping(path="/{id}")
public ResponseEntity<?> deleteCompte(@PathVariable int id){
try {
compteRepository.deleteById(id);
}
catch (Exception e) {
return new ResponseEntity<>(id, HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(id, HttpStatus.OK);
}
}
\ No newline at end of file
......@@ -3,33 +3,44 @@ package com.RESTAPI.RESTAPI.Controllers;
import com.RESTAPI.RESTAPI.Entities.User;
import com.RESTAPI.RESTAPI.Repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller // This means that this class is a Controller
@RequestMapping(path="/user") // This means URL's start with /demo (after Application path)
@Controller
@RequestMapping(path="/user")
public class UserController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
@Autowired
private UserRepository userRepository;
@PutMapping(path="/add") // Map ONLY GET Requests
@PutMapping(path="/")
public @ResponseBody
String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User addNewUser (@RequestBody String name, String ) {
userRepository.save(user);
return user;
}
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
@GetMapping(path = "/{id}")
public @ResponseBody
User getUserById(@RequestBody Integer id) {
return userRepository.findById(id).get();
}
@GetMapping(path="/all")
@GetMapping(path="/")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
@DeleteMapping(path="/{id}")
public ResponseEntity<?> deleteUser(@PathVariable int id){
try {
userRepository.deleteById(id);
}
catch (Exception e) {
return new ResponseEntity<>(id, HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(id, HttpStatus.OK);
}
}
\ No newline at end of file
package com.RESTAPI.RESTAPI.Entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Avis {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
private String choix;
private String commentaire;
}
package com.RESTAPI.RESTAPI.Entities;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
@Entity
@Getter
@Setter
@Builder
public class Compte {
@Id
private String email;
private String mdp;
private Boolean status;
private Date dateCreation;
}
package com.RESTAPI.RESTAPI.Entities;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
@Entity
public class Consultation {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
private Date dateConsultation;
@ManyToOne
private Sujet sujet;
@ManyToMany
List<Avis> avis;
}
package com.RESTAPI.RESTAPI.Entities;
import javax.persistence.*;
@Entity
public class Proposition {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
private String nom;
@ManyToOne
private Sujet sujet;
}
package com.RESTAPI.RESTAPI.Entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "status_sujets")
public class StatusSujet {
@Id
private String status;
}
package com.RESTAPI.RESTAPI.Entities;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
public class Sujet {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
private String nom;
private LocalDateTime dateFinPhase;
@ManyToOne
private StatusSujet status;
}
package com.RESTAPI.RESTAPI.Entities;
import com.RESTAPI.RESTAPI.Enums.TypeUtilisateur;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
@Entity // This tells Hibernate to make a table out of this class
@Entity
@Getter
@Setter
public class User {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
private String name;
private String email;
private String nom;
private Boolean sexe;
private String type;
private Date dateNaissance;
private Integer telephone;
private TypeUtilisateur typeUtilisateur;
@OneToOne
private Compte compte;
@ManyToMany
private List<Consultation> consultations;
public User(String nom, Boolean sexe, String type, Date dateNaissance, Integer telephone, TypeUtilisateur typeUtilisateur, Compte compte) {
this.nom = nom;
this.sexe = sexe;
this.type = type;
this.dateNaissance = dateNaissance;
this.telephone = telephone;
this.typeUtilisateur = typeUtilisateur;
this.compte = compte;
}
}
\ No newline at end of file
package com.RESTAPI.RESTAPI.Enums;
public enum TypeUtilisateur
{
UTILISATEUR,
PARTICIPANT
}
package com.RESTAPI.RESTAPI.Repositories;
import com.RESTAPI.RESTAPI.Entities.Compte;
import org.springframework.data.repository.CrudRepository;
public interface CompteRepository extends CrudRepository<Compte, Integer> {
}
package com.RESTAPI.RESTAPI.Requests;
import lombok.Getter;
@Getter
public class CompteRequest {
private String email;
private String mdp;
private Boolean status;
}
spring.datasource.url=jdbc:mysql://localhost:3306/restAPI?serverTimezone=UTC
spring.datasource.username=root
spring.jpa.hibernate.ddl-auto=create
\ No newline at end of file
spring.jpa.hibernate.ddl-auto=update
\ 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