package com.capitalone.dashboard.service;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.capitalone.dashboard.model.Authentication;
import com.capitalone.dashboard.repository.AuthenticationRepository;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {

    private final AuthenticationRepository authenticationRepository;

    @Autowired
    public AuthenticationServiceImpl(
            AuthenticationRepository authenticationRepository) {
        this.authenticationRepository = authenticationRepository;
    }

    @Override
    public Iterable<Authentication> all() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Authentication get(ObjectId id) {

        Authentication authentication = authenticationRepository.findOne(id);
        return authentication;
    }

    @Override
    public String create(String username, String password) {
        Authentication authentication = new Authentication(username, password);
        try{
        authenticationRepository.save(authentication);
        return "User is created";
        } catch(DuplicateKeyException e){
        	return "User already exists";
        }

    }

    @Override
    public String update(String username, String password) {
        Authentication authentication = authenticationRepository.findByUsername(username);
        if (null != authentication) {
            authentication.setPassword(password);
            authenticationRepository.save(authentication);
            return "User is updated";
        } else {
            return "User Does not Exist";
        }

    }

    @Override
    public void delete(ObjectId id) {
    	Authentication authentication = authenticationRepository.findOne(id);
    	if(authentication != null){
    		authenticationRepository.delete(authentication);
    	}
    }

    @Override
    public void delete(String username) {
        Authentication authentication = authenticationRepository
                .findByUsername(username);
        if(authentication != null){
        	authenticationRepository.delete(authentication);
        }
    }

    @Override
    public boolean authenticate(String username, String password) {
        boolean flag = false;
		Authentication authentication = authenticationRepository.findByUsername(username);
		
			if (authentication != null && authentication.getPassword().equals(password)) {
				flag = true;
			}
		return flag;
	}
}

@@ -1173,18 +1173,23 @@
      try
+ 
 %7B%0A
+    
         
@@ -1235,32 +1235,36 @@
 ation);%0A        
+   
+ 
 return %22User is 
@@ -1288,16 +1288,17 @@
  %7D catch
+ 
 (Duplica
@@ -1314,16 +1314,17 @@
 ption e)
+ 
 %7B%0A      
@@ -1325,17 +1325,20 @@
         
-%09
+    
 return %22
@@ -1861,17 +1861,20 @@
 ) %7B%0A    
-%09
+    
 Authenti
@@ -1939,19 +1939,23 @@
 d);%0A    
-%09
+    
 if
+ 
 (authent
@@ -1974,16 +1974,23 @@
 ull)
+ 
 %7B%0A    
+  
-%09%09
+      
 auth
@@ -2038,17 +2038,20 @@
 n);%0A    
-%09
+    
 %7D%0A    %7D%0A
@@ -2225,16 +2225,17 @@
       if
+ 
 (authent
@@ -2250,16 +2250,17 @@
 != null)
+ 
 %7B%0A      
@@ -2261,17 +2261,20 @@
         
-%09
+    
 authenti
@@ -2443,18 +2443,24 @@
  false;%0A
-%09%09
+        
 Authenti
@@ -2538,14 +2538,17 @@
 e);%0A
-%09%09%0A%09%09%09
+%0A        
 if (
@@ -2626,12 +2626,20 @@
 ) %7B%0A
-%09%09%09%09
+            
 flag
@@ -2651,15 +2651,26 @@
 ue;%0A
-%09%09%09%7D%0A%09%09
+        %7D%0A        
 retu
@@ -2682,9 +2682,12 @@
 ag;%0A
-%09
+    
 %7D%0A%7D%0A

reformat to fix spacing issues