Skip to content
Snippets Groups Projects
Commit 2a9646e8 authored by theutrama's avatar theutrama
Browse files

updates/ fixes

parent 567bb9fc
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -11,7 +11,7 @@ public class User implements Serializable { ...@@ -11,7 +11,7 @@ public class User implements Serializable {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userId", unique = true) @Column(name = "userId", unique = true)
private int userId; private Long userId;
@Column(name = "first_name", nullable = false) @Column(name = "first_name", nullable = false)
private String first_name; private String first_name;
...@@ -37,11 +37,11 @@ public class User implements Serializable { ...@@ -37,11 +37,11 @@ public class User implements Serializable {
} }
public int getUserId() { public Long getUserId() {
return userId; return userId;
} }
public void setUserId(int id) { public void setUserId(Long id) {
this.userId = id; this.userId = id;
} }
...@@ -77,4 +77,14 @@ public class User implements Serializable { ...@@ -77,4 +77,14 @@ public class User implements Serializable {
this.username = username; this.username = username;
} }
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", first_name='" + first_name + '\'' +
", last_name='" + last_name + '\'' +
", email='" + email + '\'' +
", username='" + username + '\'' +
'}';
}
} }
package com.quackr.demo.user; package com.quackr.demo.user;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
...@@ -22,4 +20,23 @@ public class UserController { ...@@ -22,4 +20,23 @@ public class UserController {
public List<User> getUsers(){ public List<User> getUsers(){
return userService.getUsers(); return userService.getUsers();
} }
@PostMapping
public void registerNewUser(@RequestBody User user){
userService.addNewUser(user);
}
@DeleteMapping(path = "{userId}")
public void deleteUser(@PathVariable("userId") Long userId){
userService.deleteUser(userId);
}
@PutMapping(path = "{userId}")
public void updateUser(@PathVariable("userId") Long userId,
@RequestParam(required = false) String first_name,
@RequestParam(required = false) String last_name,
@RequestParam(required = false) String email,
@RequestParam(required = false) String username){
userService.updateUser(userId, first_name, last_name, email, username);
}
} }
package com.quackr.demo.user; package com.quackr.demo.user;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public interface UserRepository extends JpaRepository<User, Long> { public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.email = ?1")
Optional<User> findUserByEmail(String email);
} }
...@@ -6,8 +6,11 @@ import org.springframework.web.bind.annotation.CrossOrigin; ...@@ -6,8 +6,11 @@ import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.transaction.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Service @Service
public class UserService { public class UserService {
...@@ -24,4 +27,49 @@ public class UserService { ...@@ -24,4 +27,49 @@ public class UserService {
return userRepository.findAll(); return userRepository.findAll();
} }
public void addNewUser(User user) {
Optional<User> userOptional = userRepository.findUserByEmail(user.getEmail());
if(userOptional.isPresent()){
throw new IllegalStateException("email already taken");
}
userRepository.save(user);
System.out.println(user);
}
public void deleteUser(Long userId) {
boolean exists = userRepository.existsById(userId);
if(!exists){
throw new IllegalStateException("user with id "+ userId+ " does not exist");
}
userRepository.deleteById(userId);
}
@Transactional
public void updateUser(Long userId, String firstName, String lastName, String email, String username) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalStateException("user with "+ userId + " does not exist"));
if(firstName != null && firstName.length()>0 && !Objects.equals(user.getFirst_name(), firstName)){
user.setFirst_name(firstName);
}
if(lastName != null && lastName.length()>0 && !Objects.equals(user.getLast_name(), lastName)){
user.setLast_name(lastName);
}
if(email != null && email.length()>0 && !Objects.equals(user.getEmail(),email)){
Optional<User> optionalUser = userRepository.findUserByEmail(email);
if(optionalUser.isPresent()){
throw new IllegalStateException("email is already taken");
}
user.setEmail(email);
}
if(username != null && username.length()>0 && !Objects.equals(user.getUsername(),username)){
//usernames can be used more than once
System.out.println("username update");
user.setUsername(username);
}
}
} }
...@@ -7,3 +7,6 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect ...@@ -7,3 +7,6 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true spring.h2.console.enabled=true
spring.datasource.initialization-mode=always spring.datasource.initialization-mode=always
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.show-sql=true
server.error.include-message=always
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment