Skip to content
Snippets Groups Projects
Commit d6647ce1 authored by abdu's avatar abdu
Browse files

add javadocs for userservice

parent 99278e85
No related branches found
No related tags found
No related merge requests found
...@@ -35,10 +35,15 @@ public class UserService { ...@@ -35,10 +35,15 @@ public class UserService {
@Autowired @Autowired
private TodoRepository todoRepository; private TodoRepository todoRepository;
/**
* @param userDTO the data transfer object containing user details
* @return the created userDTO or null if user already exists
*/
public UserDTO createUser(UserDTO userDTO) { public UserDTO createUser(UserDTO userDTO) {
User newUser; User newUser;
if(!(userRepository.existsByUserId(userDTO.getUserId()) || userRepository.existsByUserName(userDTO.getUserName()))){ if (!(userRepository.existsByUserId(userDTO.getUserId()) || userRepository.existsByUserName(userDTO.getUserName()))) {
newUser = userMapper.dtoToEntity(userDTO); newUser = userMapper.dtoToEntity(userDTO);
return userMapper.entityToDto(userRepository.save(newUser)); return userMapper.entityToDto(userRepository.save(newUser));
} }
...@@ -46,12 +51,21 @@ public class UserService { ...@@ -46,12 +51,21 @@ public class UserService {
return null; return null;
} }
/**
* @param userDTO the data transfer object containing user details
* @return the userDTO with the given username
*/
public UserDTO getUser(UserDTO userDTO) { public UserDTO getUser(UserDTO userDTO) {
return userMapper.entityToDto(userRepository.findByUserName(userDTO.getUserName())); return userMapper.entityToDto(userRepository.findByUserName(userDTO.getUserName()));
} }
/**
* @param request the request from the frontend which includes the jsessionid
* @param userDTO the data transfer object containing user details
* @return boolean value which indicates if the login was successful or not
*/
public boolean login(HttpServletRequest request, UserDTO userDTO) { public boolean login(HttpServletRequest request, UserDTO userDTO) {
if(customAuthenticator.authenticate(userDTO)){ if (customAuthenticator.authenticate(userDTO)) {
String userId = userRepository.findByUserName(userDTO.getUserName()).getUserId(); String userId = userRepository.findByUserName(userDTO.getUserName()).getUserId();
HttpSession session = request.getSession(); HttpSession session = request.getSession();
session.setAttribute("userId", userId); session.setAttribute("userId", userId);
...@@ -61,18 +75,27 @@ public class UserService { ...@@ -61,18 +75,27 @@ public class UserService {
return false; return false;
} }
/**
* @param request the request from the frontend which includes the jsessionid
* @param userId the id of the currently logged-in user
* @return true to indicate successful logout
*/
public boolean logout(HttpServletRequest request, String userId) { public boolean logout(HttpServletRequest request, String userId) {
HttpSession session = request.getSession(); HttpSession session = request.getSession();
if(session != null){ if (session != null) {
session.invalidate(); session.invalidate();
} }
return true; return true;
} }
/**
* @param userId the id of the currently logged-in user
* @return list of todoDTOs
*/
public List<TodoDTO> getAllTodos(String userId) { public List<TodoDTO> getAllTodos(String userId) {
List <Todo> todoList = todoRepository.findAllByUserId(userId); List<Todo> todoList = todoRepository.findAllByUserId(userId);
List<TodoDTO> todoDTOList = new ArrayList<>(); List<TodoDTO> todoDTOList = new ArrayList<>();
todoList.forEach((todoItem) -> { todoList.forEach((todoItem) -> {
...@@ -81,22 +104,36 @@ public class UserService { ...@@ -81,22 +104,36 @@ public class UserService {
return todoDTOList; return todoDTOList;
} }
public List<TodoDTO> addTodo(TodoDTO todoDTO){ /**
* @param todoDTO data transfer object that contains details about a todoElement
* @return a list of all todos for the given user
*/
public List<TodoDTO> addTodo(TodoDTO todoDTO) {
Todo todo = todoMapper.dtoToEntity(todoDTO); Todo todo = todoMapper.dtoToEntity(todoDTO);
todoRepository.save(todo); todoRepository.save(todo);
return getAllTodosForUser(todoDTO.getUserId()); return getAllTodosForUser(todoDTO.getUserId());
} }
/**
* @param userId the id of the currently logged-in user
* @param todoId id of the todo that will be deleted
* @return a list of all todos for the given user
*/
public List<TodoDTO> deleteTodo(String userId, String todoId) { public List<TodoDTO> deleteTodo(String userId, String todoId) {
System.out.println("service here"); System.out.println("service here");
if(todoRepository.existsById(todoId)){ if (todoRepository.existsById(todoId)) {
todoRepository.deleteById(todoId); todoRepository.deleteById(todoId);
} }
return getAllTodosForUser(userId); return getAllTodosForUser(userId);
} }
/**
* @param userId the id of the currently logged-in user
* @param todoId id of the todo that will be set to complete
* @return a list of all todos for the given user
*/
public List<TodoDTO> setTodoComplete(String userId, String todoId) { public List<TodoDTO> setTodoComplete(String userId, String todoId) {
Todo todo = todoRepository.findById(todoId).get(); Todo todo = todoRepository.findById(todoId).get();
todo.setCompleted(true); todo.setCompleted(true);
...@@ -105,14 +142,24 @@ public class UserService { ...@@ -105,14 +142,24 @@ public class UserService {
return getAllTodosForUser(userId); return getAllTodosForUser(userId);
} }
//helper methods /*****************
* HELPER METHODS *
*****************/
/**
* @param request the request from the frontend which includes the jsessionid
* @return boolean that indicates if the session is valid or not
*/
public boolean validateSession(HttpServletRequest request) { public boolean validateSession(HttpServletRequest request) {
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
System.out.println("Session ID: " + (session != null ? session.getId() : "null")); System.out.println("Session ID: " + (session != null ? session.getId() : "null"));
return session != null && session.getAttribute("userId") != null; return session != null && session.getAttribute("userId") != null;
} }
/**
* @param userId the id of the user for which all todos should be retrieved
* @return a list of all the todoDTOs for the given user
*/
public List<TodoDTO> getAllTodosForUser(String userId) { public List<TodoDTO> getAllTodosForUser(String userId) {
List<Todo> todoList = todoRepository.findAllByUserId(userId); List<Todo> todoList = todoRepository.findAllByUserId(userId);
List<TodoDTO> todoDTOList = new ArrayList<>(); List<TodoDTO> todoDTOList = new ArrayList<>();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment