added email validation
This commit is contained in:
parent
995c69a71b
commit
65ff9d85dd
@ -13,13 +13,13 @@ public class User {
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private UUID id;
|
||||
|
||||
@Column(nullable = false, unique = true, length = 255)
|
||||
@Column(nullable = false, length = 255)
|
||||
private String email;
|
||||
|
||||
@Column(nullable = false, length = 255)
|
||||
private String password;
|
||||
|
||||
@Column(nullable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
|
||||
@Column(nullable = false, updatable = false,insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
|
||||
private LocalDateTime regTime;
|
||||
|
||||
public UUID getId() {
|
||||
|
||||
@ -1,31 +1,17 @@
|
||||
package dev.ksan.etfoglasiserver.service;
|
||||
|
||||
import dev.ksan.etfoglasiserver.dto.UserDTO;
|
||||
import dev.ksan.etfoglasiserver.model.User;
|
||||
import dev.ksan.etfoglasiserver.repository.UserRepo;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@Autowired UserRepo userRepo;
|
||||
/*
|
||||
public List<UserDTO> getUsers() {
|
||||
return userRepo.findAll().stream().map(this::toDTO).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public UserDTO getUserById(UUID id) {
|
||||
return userRepo.findById(id).map(this::toDTO).orElse(null);
|
||||
}
|
||||
|
||||
public UserDTO toDTO(User user) {
|
||||
return new UserDTO(user.getId(), user.getEmail());
|
||||
}
|
||||
*/
|
||||
public List<User> getUsers() {
|
||||
System.out.println("getUsers");
|
||||
return userRepo.findAll();
|
||||
@ -35,18 +21,39 @@ public class UserService {
|
||||
return userRepo.findById(userId).orElseThrow(() -> new RuntimeException("User not found"));
|
||||
}
|
||||
|
||||
public void addUser(User prod) {
|
||||
userRepo.save(prod);
|
||||
public void addUser(User user) {
|
||||
if(userRepo.findByEmail(user.getEmail()) != null) {
|
||||
throw new RuntimeException("User already exists");
|
||||
}
|
||||
if(this.isValidEmail(user.getEmail())) {
|
||||
userRepo.save(user);
|
||||
}
|
||||
}
|
||||
|
||||
public void addNewUser(UserDTO userDTO) {
|
||||
Optional<User> user = userRepo.findByEmail(userDTO.getEmail());
|
||||
public void updateUser(User user) {
|
||||
|
||||
if(this.isValidEmail(user.getEmail())) {
|
||||
|
||||
userRepo.save(user);
|
||||
}
|
||||
public void updateUser(User prod) {
|
||||
userRepo.save(prod);
|
||||
}
|
||||
|
||||
public void deleteUser(UUID userId) {
|
||||
userRepo.deleteById(userId);
|
||||
}
|
||||
public boolean isValidEmail(String email) {
|
||||
if(email == null) {
|
||||
return false;
|
||||
}
|
||||
String regex = "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
|
||||
|
||||
|
||||
if(email.length() < 256 &&email.matches(regex)){
|
||||
System.out.println(email.length());
|
||||
System.out.println("Email address is too long");
|
||||
return true;
|
||||
}
|
||||
throw new RuntimeException("Invalid email");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user