diff --git a/PRA/Images/image-1.png b/PRA/Images/image-1.png new file mode 100644 index 000000000..16d60b4d6 Binary files /dev/null and b/PRA/Images/image-1.png differ diff --git a/PRA/Images/image-2.png b/PRA/Images/image-2.png new file mode 100644 index 000000000..7241db014 Binary files /dev/null and b/PRA/Images/image-2.png differ diff --git a/PRA/Images/image-3.png b/PRA/Images/image-3.png new file mode 100644 index 000000000..50913882c Binary files /dev/null and b/PRA/Images/image-3.png differ diff --git a/PRA/Images/image-4.png b/PRA/Images/image-4.png new file mode 100644 index 000000000..db11fe291 Binary files /dev/null and b/PRA/Images/image-4.png differ diff --git a/PRA/Images/image-5.png b/PRA/Images/image-5.png new file mode 100644 index 000000000..149da8025 Binary files /dev/null and b/PRA/Images/image-5.png differ diff --git a/PRA/Images/image-6.png b/PRA/Images/image-6.png new file mode 100644 index 000000000..9fe981bf3 Binary files /dev/null and b/PRA/Images/image-6.png differ diff --git a/PRA/Images/image.png b/PRA/Images/image.png new file mode 100644 index 000000000..4a888436e Binary files /dev/null and b/PRA/Images/image.png differ diff --git a/PRA/Postman PRA02.md b/PRA/Postman PRA02.md new file mode 100644 index 000000000..5511f022e --- /dev/null +++ b/PRA/Postman PRA02.md @@ -0,0 +1,28 @@ + # Postman + Spring Boot PRA +--- +## Create User +![Create User](Images/image-1.png)| + +--- +## Get All Users +![Get All Users](Images/image-2.png) + +--- +## Get User By ID +![Get User By ID](Images/image-3.png) + +--- +## Update User +![Update User](Images/image-4.png) + +--- +## Delete User +![Delete User](Images/image-5.png) + +--- +## Delete All Users +![Delete All Users](Images/image-6.png) + +--- +## H2 Data Base +![H2](Images/image.png) diff --git a/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/controller/UserController.java b/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/controller/UserController.java new file mode 100644 index 000000000..644cdf13c --- /dev/null +++ b/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/controller/UserController.java @@ -0,0 +1,69 @@ +package dev.pronunciationAppBack.controller; + +import dev.pronunciationAppBack.model.User; +import dev.pronunciationAppBack.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/api/users") +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping("/hello") + public String hello() { + return "Hello Albert, this is a test"; + } + + @GetMapping + public List getAllUsers() { + return userService.getAllUsers(); + } + + @GetMapping("/{id}") + public User getUserById(@PathVariable String id) { + Optional user = userService.getUserById(id); + + if (user.isPresent()) { + return user.get(); + } else { + return null; + } + } + + @PostMapping("/createUser") + public User createUser(@RequestBody User user) { + return userService.createUser(user); + } + + @PutMapping("/{id}") + public User updateUser(@PathVariable String id, @RequestBody User user) { + if (!userService.existsById(id)) { + return null; + } + + user.setId(id); + return userService.updateUser(user); + } + + @DeleteMapping("/{id}") + public String deleteUser(@PathVariable String id) { + if (userService.existsById(id)) { + userService.deleteUser(id); + return "User deleted"; + } else { + return "User not found"; + } + } + + @DeleteMapping + public String deleteAllUsers() { + userService.deleteAllUsers(); + return "All users deleted"; + } +} diff --git a/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/model/User.java b/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/model/User.java new file mode 100644 index 000000000..d9e335786 --- /dev/null +++ b/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/model/User.java @@ -0,0 +1,79 @@ +package dev.pronunciationAppBack.model; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity +@Table(name = "APP_USERS") // Because USER is an H2 reserved word +public class User { + + @Id + private String id; + private String name; + private String email; + private String password; + private boolean isActive; + + public User() {} + + public User(String id, String name, String email, String password, boolean isActive) { + this.id = id; + this.name = name; + this.email = email; + this.password = password; + this.isActive = isActive; + + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName (String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public boolean isActive() { + return isActive; + } + + public void setActive(boolean active) { + this.isActive = active; + } + + @Override + public String toString() { + return "User{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", email='" + email + '\'' + + ", isActive=" + isActive + + '}'; + } + + +} diff --git a/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/repository/UserRepository.java b/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/repository/UserRepository.java new file mode 100644 index 000000000..ed6d655c4 --- /dev/null +++ b/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/repository/UserRepository.java @@ -0,0 +1,9 @@ +package dev.pronunciationAppBack.repository; + +import dev.pronunciationAppBack.model.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { + User getUserById(String id); + User getUserByEmail(String email); +} diff --git a/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/service/UserService.java b/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/service/UserService.java new file mode 100644 index 000000000..4fe674dd2 --- /dev/null +++ b/backend/pronunciationAppBack/src/main/java/dev/pronunciationAppBack/service/UserService.java @@ -0,0 +1,48 @@ +package dev.pronunciationAppBack.service; + +import dev.pronunciationAppBack.model.User; +import dev.pronunciationAppBack.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class UserService { + + @Autowired + private UserRepository userRepository; + + public List getAllUsers() { + return userRepository.findAll(); + } + + public Optional getUserById(String id) { + return userRepository.findById(id); + } + + public User createUser(User user) { + return userRepository.save(user); + } + + public User updateUser(User user) { + return userRepository.save(user); + } + + public void deleteUser(String id) { + userRepository.deleteById(id); + } + + public void deleteAllUsers() { + userRepository.deleteAll(); + } + + public boolean existsById(String id) { + return userRepository.existsById(id); + } + + public long getUserCount() { + return userRepository.count(); + } +} diff --git a/backend/pronunciationAppBack/src/main/resources/application.properties b/backend/pronunciationAppBack/src/main/resources/application.properties index 83cb16f3e..b5d25190e 100644 --- a/backend/pronunciationAppBack/src/main/resources/application.properties +++ b/backend/pronunciationAppBack/src/main/resources/application.properties @@ -12,9 +12,9 @@ spring.h2.console.enabled=true # H2 LOCAL DB SERVER -spring.datasource.url=jdbc:h2:/home/albert/MyProjects/DataBase/pronunciationDB/pronunciationDB.db -spring.datasource.username=albert -spring.datasource.password=1234 + spring.datasource.url=jdbc:h2:/home/luis/projects/database/usersDB + spring.datasource.username=luis + spring.datasource.password= # DDL OPTIONS: create-drop, create, update, none, validate -spring.jpa.hibernate.ddl-auto=none \ No newline at end of file +spring.jpa.hibernate.ddl-auto=update