Created
October 17, 2017 03:08
-
-
Save tonthanhhung/737c008294fb5c5b353489750654043c to your computer and use it in GitHub Desktop.
Interview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.security.access.annotation.Secured; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import java.util.List; | |
@RestController | |
@RequestMapping("/register") | |
public class RegisterController { | |
@Autowired | |
private RegisterService registerService; | |
@Autowired | |
private UserDao userDao; | |
private void setRegisterService(RegisterService registerService) { | |
this.registerService = registerService; | |
} | |
@RequestMapping("/add/user") | |
public void register(String username, String password) { | |
if (username == null || username == "") { | |
System.err.println("Input is invalid!"); | |
return; | |
} | |
if (password == null || password == "") { | |
System.err.println("Input is invalid!"); | |
return; | |
} | |
List<User> list = registerService.getUsers(); | |
synchronized (list) { | |
for (int i = 0; i < list.size(); i++) { | |
if (list.get(i).username == username) { | |
System.err.println("Input is invalid!"); | |
return; | |
} | |
} | |
} | |
System.out.println("Registering user..."); | |
synchronized (registerService) { | |
registerService.register(username, password); | |
} | |
} | |
@Secured({"DELETE_USERS"}) | |
@RequestMapping("/delete") | |
public void delete(String username, String password) { | |
if (username == null || username == "") { | |
System.err.println("Username is invalid!"); | |
return; | |
} | |
userDao.delete(userDao.findById(username)); | |
} | |
@Secured({"allow-user-listing"}) | |
@RequestMapping("/") | |
public List rootPathMapping() { | |
return registerService.getUsers(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment