UserServiceJpa.java 1.52 KB
package fr.plil.sio.persistence.jpa;

import fr.plil.sio.persistence.api.Group;
import fr.plil.sio.persistence.api.Right;
import fr.plil.sio.persistence.api.User;
import fr.plil.sio.persistence.api.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceJpa implements UserService {
    
    @Autowired
    UserRepository userRepository;
    
    @Autowired
    GroupRepository groupRepository;    
    
    @Override
    public User create(String name, String groupName) {
        if(name == null)
            throw new IllegalArgumentException("Name cannot be null");
        if(groupName == null)
            throw new IllegalArgumentException("Group name cannot be null");
        
        Group group = groupRepository.findByName(groupName);
        if(group == null)
            throw new IllegalStateException("Group not found");
        
        User user = new User();
        user.setName(name);
        user.setGroup(group);
        userRepository.save(user);
        
        return user;
    }

    @Override
    public boolean delete(String name) {
      
        
        return false;
    }

    @Override
    public User findByName(String name) {
        
        if(name == null)
            throw new IllegalArgumentException("Name cannot be null");
                
        return userRepository.findByName(name);
    }

    @Override
    public boolean isUserHasRight(String userName, Right right) {
      
        return false;
    }
}