Max/Backend/RegisterEndPoint #105

Merged
tonitch merged 3 commits from Max/Backend/RegisterEndPoint into master 2024-03-14 23:30:14 +01:00
10 changed files with 179 additions and 52 deletions

View File

@ -0,0 +1,84 @@
package ovh.herisson.Clyde.EndPoints;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.InscriptionService;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class InscriptionController {
private final InscriptionService inscriptionServ;
private final AuthenticatorService authServ;
public InscriptionController(InscriptionService inscriptionServ, AuthenticatorService authServ){
this.inscriptionServ = inscriptionServ;
this.authServ = authServ;
}
@GetMapping("/inscriptionRequests")
public ResponseEntity<Iterable<Map<String,Object>>> getAllRequests(@RequestHeader("Authorization") String token){
if (!isSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);}
Iterable<InscriptionRequest> inscriptionRequests = inscriptionServ.getAll();
ArrayList<Map<String,Object>> toReturn = new ArrayList<>();
for (InscriptionRequest i:inscriptionRequests){
toReturn.add(requestWithoutPassword(i));
}
return new ResponseEntity<>(toReturn, HttpStatus.OK);
}
@GetMapping("/inscriptionRequest/{id}")
public ResponseEntity<Map<String,Object>> getById(@PathVariable long id){
InscriptionRequest inscriptionRequest = inscriptionServ.getById(id);
if (inscriptionRequest == null) {return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);}
return new ResponseEntity<>(requestWithoutPassword(inscriptionRequest), HttpStatus.OK);
}
private Map<String,Object> requestWithoutPassword(InscriptionRequest inscriptionRequest) {
Map<String, Object> toReturn = new HashMap<>();
toReturn.put("id", inscriptionRequest.getId());
toReturn.put("firstName", inscriptionRequest.getFirstName());
toReturn.put("lastName", inscriptionRequest.getLastName());
toReturn.put("address", inscriptionRequest.getAddress());
toReturn.put("birthDate", inscriptionRequest.getBirthDate());
toReturn.put("country", inscriptionRequest.getCountry());
toReturn.put("cursus", inscriptionRequest.getCursus());
toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture());
toReturn.put("state", inscriptionRequest.getState());
return toReturn;
}
Review

Est-ce que l’opération inverse ne serait pas plus "efficace" ?
genre récupéré une map de l'objet et puis faire un return.remove("password") ?

Est-ce que l’opération inverse ne serait pas plus "efficace" ? genre récupéré une map de l'objet et puis faire un return.remove("password") ?
private boolean isSecretaryOrAdmin(String authorization){
if (authorization ==null)
return false;
User poster = authServ.getUserFromToken(authorization);
if (poster == null) return false;
return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin;
}
}

View File

@ -1,12 +1,19 @@
package ovh.herisson.Clyde.EndPoints;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.Column;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Tables.Cursus;
import ovh.herisson.Clyde.Tables.CursusType;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.User;
import java.util.Date;
import java.util.HashMap;
@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
@ -40,6 +47,13 @@ public class LoginController {
responseHeaders.set("Set-Cookie",String.format("session_token=%s",sessionToken));
return ResponseEntity.ok().headers(responseHeaders).build();
}
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody InscriptionRequest inscriptionRequest){
authServ.register(inscriptionRequest);
return new ResponseEntity<>("Is OK", HttpStatus.OK);
}
}

View File

@ -0,0 +1,10 @@
package ovh.herisson.Clyde.Repositories;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
public interface InscriptionRepository extends CrudRepository<InscriptionRequest,Long> {
InscriptionRequest findById(long aLong);
}

View File

@ -1,6 +1,9 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.EndPoints.LoginController;
import ovh.herisson.Clyde.Repositories.InscriptionRepository;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User;
@ -11,10 +14,12 @@ public class AuthenticatorService {
private final TokenService tokenService;
private final UserService userService;
private final InscriptionService inscriptionService;
public AuthenticatorService(TokenService tokenService, UserService userService){
public AuthenticatorService(TokenService tokenService, UserService userService, InscriptionService inscriptionService){
this.tokenService = tokenService;
this.userService = userService;
this.inscriptionService = inscriptionService;
}
public User getUserFromToken(String token){
@ -30,4 +35,8 @@ public class AuthenticatorService {
tokenService.saveToken(new Token(user, token,expirationDate));
return token;
}
public void register(InscriptionRequest inscriptionRequest) {
inscriptionService.save(inscriptionRequest);
}
}

View File

@ -0,0 +1,34 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.InscriptionRepository;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import java.util.HashMap;
import java.util.Map;
@Service
public class InscriptionService {
InscriptionRepository incriptionRepo;
public void save(InscriptionRequest inscriptionRequest){
incriptionRepo.save(inscriptionRequest);
}
public InscriptionService(InscriptionRepository inscriptionRepo){
this.incriptionRepo = inscriptionRepo;
}
public InscriptionRequest getById(long id){
InscriptionRequest inscriptionRequest = incriptionRepo.findById(id);
if (inscriptionRequest == null){
return null;
}
return inscriptionRequest;
}
public Iterable<InscriptionRequest> getAll(){
return incriptionRepo.findAll();
}
}

View File

@ -13,6 +13,12 @@ public class Cursus {
private int year;
private String option;
public static Cursus infoBab1 = new Cursus(1,"info");
public static Cursus chemistryBab1 = new Cursus(1,"chemistry");
public static Cursus psychologyBab1 = new Cursus(1,"psychology");
public Cursus(int year, String option){
this.year = year;
this.option = option;

View File

@ -0,0 +1,8 @@
package ovh.herisson.Clyde.Tables;
public enum CursusType {
infoBab1,
chemistryBab1,
psychologyBab1;
}

View File

@ -9,7 +9,7 @@ public class InscriptionRequest {
private int id;
private String firstName;
private String lastName;
private String adress;
private String address;
private String email;
private String country;
private Date birthDate;
@ -19,15 +19,19 @@ public class InscriptionRequest {
private Cursus cursus;
private RequestState state;
private String profilePicture;
private String password;
public InscriptionRequest(){}
public InscriptionRequest(String lastName, String firstName, String adress, String email, String country, Date birthDate, RequestState state, String profilePicture){
public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate, RequestState state, String profilePicture, String password){
this.lastName = lastName;
this.firstName = firstName;
this.adress = adress;
this.address = address;
this.email = email;
this.country = country;
this.birthDate = birthDate;
this.state = state;
this.profilePicture = profilePicture;
this.password = password;
}
public int getId() {
@ -50,12 +54,12 @@ public class InscriptionRequest {
this.lastName = lastName;
}
public String getAdress() {
return adress;
public String getAddress() {
return address;
}
public void setAdress(String adress) {
this.adress = adress;
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {

View File

@ -1,42 +0,0 @@
package ovh.herisson.Clyde.Tables;
import jakarta.persistence.*;
@Entity
public class Secretary {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Users")
private User user;
private String faculty;
public Secretary(User user, String faculty){
this.user = user;
this.faculty = faculty;
}
public Secretary() {}
public int getId() {
return id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getFaculty() {
return faculty;
}
public void setFaculty(String faculty) {
this.faculty = faculty;
}
}

View File

@ -9,11 +9,11 @@ public class UserCursus {
private int id;
//Un étudiant peut avoir plusieurs cursus
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Users")
private User user;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Cursus")
private Cursus cursus;