added email validation
This commit is contained in:
parent
995c69a71b
commit
65ff9d85dd
@ -13,13 +13,13 @@ public class User {
|
|||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private UUID id;
|
private UUID id;
|
||||||
|
|
||||||
@Column(nullable = false, unique = true, length = 255)
|
@Column(nullable = false, length = 255)
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@Column(nullable = false, length = 255)
|
@Column(nullable = false, length = 255)
|
||||||
private String password;
|
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;
|
private LocalDateTime regTime;
|
||||||
|
|
||||||
public UUID getId() {
|
public UUID getId() {
|
||||||
|
|||||||
@ -1,31 +1,17 @@
|
|||||||
package dev.ksan.etfoglasiserver.service;
|
package dev.ksan.etfoglasiserver.service;
|
||||||
|
|
||||||
import dev.ksan.etfoglasiserver.dto.UserDTO;
|
|
||||||
import dev.ksan.etfoglasiserver.model.User;
|
import dev.ksan.etfoglasiserver.model.User;
|
||||||
import dev.ksan.etfoglasiserver.repository.UserRepo;
|
import dev.ksan.etfoglasiserver.repository.UserRepo;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
@Autowired UserRepo userRepo;
|
@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() {
|
public List<User> getUsers() {
|
||||||
System.out.println("getUsers");
|
System.out.println("getUsers");
|
||||||
return userRepo.findAll();
|
return userRepo.findAll();
|
||||||
@ -35,18 +21,39 @@ public class UserService {
|
|||||||
return userRepo.findById(userId).orElseThrow(() -> new RuntimeException("User not found"));
|
return userRepo.findById(userId).orElseThrow(() -> new RuntimeException("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addUser(User prod) {
|
public void addUser(User user) {
|
||||||
userRepo.save(prod);
|
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) {
|
public void updateUser(User user) {
|
||||||
Optional<User> user = userRepo.findByEmail(userDTO.getEmail());
|
|
||||||
|
if(this.isValidEmail(user.getEmail())) {
|
||||||
|
|
||||||
|
userRepo.save(user);
|
||||||
}
|
}
|
||||||
public void updateUser(User prod) {
|
|
||||||
userRepo.save(prod);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteUser(UUID userId) {
|
public void deleteUser(UUID userId) {
|
||||||
userRepo.deleteById(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