Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package dev.pronunciationAppBack.controller;


import dev.pronunciationAppBack.model.UserEntity;
import dev.pronunciationAppBack.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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
public ResponseEntity<List<UserEntity>> getAllUsers() {
List<UserEntity> users = userService.getAllUsers();
HttpHeaders headers = getCommonHeaders("Get all users");

return !users.isEmpty()
? new ResponseEntity<>(users, headers, HttpStatus.OK)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}

@GetMapping("/{id}")
public ResponseEntity<UserEntity> getUserById(@PathVariable String id) {
Optional<UserEntity> userEntity = userService.getUserById(id);
HttpHeaders headers = getCommonHeaders("Get user by ID");

return userEntity.map(value -> new ResponseEntity<>(value, headers, HttpStatus.OK))
.orElseGet(() -> new ResponseEntity<>(headers, HttpStatus.NOT_FOUND));

}

@PostMapping("/createUser")
public ResponseEntity<UserEntity> createUser(@RequestBody UserEntity userEntity) {
UserEntity createUserEntity = userService.createUser(userEntity);
HttpHeaders headers = getCommonHeaders("Create a new user");

return new ResponseEntity<>(createUserEntity, headers, HttpStatus.CREATED);
}

@PutMapping("/{id}")
public ResponseEntity<UserEntity> updateUser(@PathVariable String id, @RequestBody UserEntity userEntity) {
UserEntity updateUser = userService.updateUser(userEntity);
HttpHeaders headers = getCommonHeaders("Update a user");

return new ResponseEntity<>(updateUser, headers, HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<String> deleteUser(@PathVariable("id") String idToDelete) {
HttpHeaders headers = getCommonHeaders("Delete a user");

if (userService.existsById(idToDelete)) {
userService.deleteUser(idToDelete);
return new ResponseEntity<>("User deleted", headers, HttpStatus.OK);
} else {
return new ResponseEntity<>("User not found", headers, HttpStatus.NOT_FOUND);
}
}

@DeleteMapping
public ResponseEntity<String> deleteAllUsers() {
userService.deleteAllUsers();
HttpHeaders headers = getCommonHeaders("Delete all users");
return new ResponseEntity<>("All users deleted", headers, HttpStatus.OK);
}

private HttpHeaders getCommonHeaders(String string) {
HttpHeaders headers = new HttpHeaders();
return headers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package dev.pronunciationAppBack.model;


import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class UserEntity {

@Id
private String id;
private String username;
private int age;
private String email;
private int totalScore;
private boolean isActive;

public UserEntity() {}

public UserEntity(String id, String username, int age, String email, int totalScore, boolean isActive) {
this.id = id;
this.username = username;
this.age = age;
this.email = email;
this.totalScore = totalScore;
this.isActive = isActive;
}

public String getId() {
return id;
}

public String getUsername() {
return username;
}

public int getAge() {
return age;
}

public String getEmail() {
return email;
}

public int getTotalScore() {
return totalScore;
}

public boolean getIsActive() {
return isActive;
}

public void setId(String id) {
this.id = id;
}

public void setUsername(String username) {
this.username = username;
}

public void setAge(int age) {
this.age = age;
}

public void setEmail(String email) {
this.email = email;
}

public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}

public void setActive(boolean isActive) {
this.isActive = isActive;
}

@Override
public String toString() {
return "UserEntity{" +
"id='" + id + '\'' +
", userName='" + username + '\'' +
", age='" + age + '\'' +
", email='" + email + '\'' +
", totalScore='" + totalScore + '\'' +
", isActive='" + isActive + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.pronunciationAppBack.repository;

import dev.pronunciationAppBack.model.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<UserEntity, String> {
UserEntity getUserById(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.pronunciationAppBack.service;

import dev.pronunciationAppBack.model.UserEntity;
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<UserEntity> getAllUsers(){
return userRepository.findAll();
}

public Optional<UserEntity> getUserById(String id) {
return Optional.ofNullable(userRepository.getUserById(id));
}

public UserEntity createUser(UserEntity user) {
return userRepository.save(user);
}

public UserEntity updateUser(UserEntity 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.url=jdbc:h2:/home/marcfernandez/myProjects/dataBase/pronunciationDB/pronunciationDB.db
spring.datasource.username=marc
spring.datasource.password=1234

# DDL OPTIONS: create-drop, create, update, none, validate
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.ddl-auto=update