Implemented api for users
This commit is contained in:
@@ -38,6 +38,16 @@ public class UserController {
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/get/name/{name}")
|
||||
public ResponseEntity<User> getUserByName(@PathVariable("name") String name) {
|
||||
User user = userService.getUserByNameService(name);
|
||||
if (user != null) {
|
||||
return new ResponseEntity<>(user, HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/create/{id}")
|
||||
public ResponseEntity<User> createUser(@RequestBody User user) {
|
||||
User createdUser = userService.createUser(user);
|
||||
|
||||
@@ -4,7 +4,11 @@ import ch.progetto152.entity.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
Optional<User> findUserByName(String name);
|
||||
|
||||
}
|
||||
|
||||
@@ -19,12 +19,15 @@ public class UserService {
|
||||
}
|
||||
|
||||
public List<User> getAllUsers() {
|
||||
System.out.println(userRepository.findAll());
|
||||
return userRepository.findAll();
|
||||
}
|
||||
|
||||
public User getUserByIdService(Long id) {
|
||||
return null;
|
||||
return userRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public User getUserByNameService(String name){
|
||||
return userRepository.findUserByName(name).orElse(null);
|
||||
}
|
||||
|
||||
public User createUser(User user) {
|
||||
@@ -32,10 +35,23 @@ public class UserService {
|
||||
}
|
||||
|
||||
public User updateUser(Long id, User user) {
|
||||
User user1 = getUserByIdService(id);
|
||||
if (user1 != null) {
|
||||
user1.setName(user.getName());
|
||||
user1.setUsername(user.getUsername());
|
||||
user1.setPassword(user.getPassword());
|
||||
return userRepository.save(user1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteUser(Long id) {
|
||||
boolean exists = userRepository.existsById(id);
|
||||
if(!exists){
|
||||
return false;
|
||||
}
|
||||
userRepository.deleteById(id);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user