package io.anand.springboot;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    private ArrayList<User> users;

    public UserService () {
        users = new ArrayList<User> ();
        users.add(new User ("John", "Elementary", 192838, 145354545));
        users.add(new User ("Ravi", "Middle", 192298, 145312144));
        users.add(new User ("David", "Elementary", 192848, 145354652));
        users.add(new User ("Mary", "Middle", 192858, 145351234));
        users.add(new User ("Vinh", "High School", 192538, 14534567));
        users.add(new User ("Surya", "High School", 193138, 14538613));
    }

    public List<User> getUsers () {
        return (List<User>) userRepository.findAll();
        // return users;
    }

    public User getUser (String userName) {
        return userRepository.findByName(userName);
        /*
        try {
            return users.stream().filter(u -> u.getName().equals(userName)).findFirst().get();
        } catch (Exception e) {
            System.out.println("Caught Exception looking up user: " + userName + ", e: " + e);
        }
        return null;
        */
    }

    public User updUser (String userName, User updUser) {
        return userRepository.save(updUser);
        /*
        int index = users.indexOf(users.stream().filter(u -> u.getName().equals(userName)).findFirst().get());
        users.set(index, updUser);
        return users.get(index);
         */
    }

    public User addUser (User user) {

        User addedUser = userRepository.save(user);
        users.add(user);
        return addedUser;
    }

    public boolean remUser (User user) {
        userRepository.delete(user);
        boolean deleted = users.remove(getUser(user.getName()));
        System.out.println(deleted ? "Able " : " Unable " + "to remove the user: " + user.toString());
        return deleted;
    }
}

@@ -1131,38 +1131,43 @@
 Name) %7B%0A        
-return
+User user =
  userRepository.
@@ -1188,34 +1188,75 @@
 rName);%0A        
-/*
+if (null == user)%0A            return user;%0A
 %0A        try %7B%0A 
@@ -1315,36 +1315,42 @@
 me().equals(user
+.get
 Name
+()
 )).findFirst().g
@@ -1513,27 +1513,16 @@
 n null;%0A
-        */%0A
     %7D%0A%0A 
@@ -1582,30 +1582,35 @@
 ) %7B%0A        
-return
+User user =
  userReposit
@@ -1640,10 +1640,51 @@
     
-/*
+if (null == user)%0A            return user;%0A
 %0A   
@@ -1750,30 +1750,29 @@
 .get
-Name().equals(userName
+Id() == (user.getId()
 )).f
@@ -1862,20 +1862,8 @@
 x);%0A
-         */%0A
     
@@ -1903,17 +1903,16 @@
 user) %7B%0A
-%0A
         
@@ -1947,32 +1947,94 @@
 ory.save(user);%0A
+        if (null == addedUser)%0A            return addedUser;%0A%0A
         users.ad
@@ -2120,45 +2120,8 @@
 ) %7B%0A
-        userRepository.delete(user);%0A
     
@@ -2280,24 +2280,61 @@
 oString());%0A
+        userRepository.delete(user);%0A
         retu

Delete from local array first before removing from database, otherwise delete fails to find the intem in the get call and array will not be updated