From 3956037ab5e7e293aa729ad0609d2b0efbc9862a Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 2 Mar 2024 23:33:48 +0100 Subject: [PATCH] example backend api with user table --- .../Clyde/EndPoints/UserController.java | 27 +++++++++ .../Clyde/Repositories/UserRepository.java | 16 ++++++ .../java/ovh/herisson/Clyde/Tables/User.java | 57 ++++++++++++++++++- .../src/main/resources/application.properties | 3 +- 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java new file mode 100644 index 0000000..1210831 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -0,0 +1,27 @@ +package ovh.herisson.Clyde.EndPoints; + + +import org.springframework.web.bind.annotation.*; +import ovh.herisson.Clyde.Repositories.UserRepository; +import ovh.herisson.Clyde.Tables.User; + + +@RestController +@CrossOrigin(origins = "http://localhost:5173") +public class UserController { + + private final UserRepository userRepo; + + public UserController(UserRepository userRepo){ + this.userRepo = userRepo; + } + + @GetMapping("/users") + public Iterable getUsers(){ + return userRepo.findAll(); + } + @PostMapping("/users") + public void postUser(@RequestBody User user ){ + userRepo.save(user); + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java new file mode 100644 index 0000000..38f9ae0 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java @@ -0,0 +1,16 @@ +package ovh.herisson.Clyde.Repositories; + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.User; + +import java.util.List; + +public interface UserRepository extends CrudRepository { + + User findById(long id); + + /** + @Query(value = "select a.* from Users a ",nativeQuery = true) + Iterable findAllUsers();**/ +} \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java index ca69efe..2165681 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java @@ -14,7 +14,6 @@ public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int RegNo; - private String LastName; private String FirstName; private String Email; @@ -32,4 +31,60 @@ public class User { public User() { } + + public String getLastName() { + return LastName; + } + + public void setLastName(String LastName) { + this.LastName = LastName; + } + + public String getFirstName() { + return FirstName; + } + + public void setFirstName(String firstName) { + FirstName = firstName; + } + + public String getEmail() { + return Email; + } + + public void setEmail(String email) { + Email = email; + } + + public String getAdress() { + return Adress; + } + + public void setAdress(String adress) { + Adress = adress; + } + + public String getCountry() { + return Country; + } + + public void setCountry(String country) { + Country = country; + } + + public Date getBirthDate() { + return BirthDate; + } + + public void setBirthDate(Date birthDate) { + BirthDate = birthDate; + } + + public ovh.herisson.Clyde.Tables.Role getRole() { + return Role; + } + + public void setRole(ovh.herisson.Clyde.Tables.Role role) { + Role = role; + } } diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 8b13789..df288a6 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1 +1,2 @@ - +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect