From d6647ce17abbf56a61685811eb88a58f6d5fb52a Mon Sep 17 00:00:00 2001 From: abdu <abdukiran@gmail.com> Date: Wed, 23 Oct 2024 00:04:23 +0200 Subject: [PATCH] add javadocs for userservice --- .../todo/service/UserService.java | 61 ++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/cloudcomputing/todo/service/UserService.java b/src/main/java/com/cloudcomputing/todo/service/UserService.java index f1f1d16..b25dbcf 100644 --- a/src/main/java/com/cloudcomputing/todo/service/UserService.java +++ b/src/main/java/com/cloudcomputing/todo/service/UserService.java @@ -35,10 +35,15 @@ public class UserService { @Autowired 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) { User newUser; - if(!(userRepository.existsByUserId(userDTO.getUserId()) || userRepository.existsByUserName(userDTO.getUserName()))){ + if (!(userRepository.existsByUserId(userDTO.getUserId()) || userRepository.existsByUserName(userDTO.getUserName()))) { newUser = userMapper.dtoToEntity(userDTO); return userMapper.entityToDto(userRepository.save(newUser)); } @@ -46,12 +51,21 @@ public class UserService { return null; } + /** + * @param userDTO the data transfer object containing user details + * @return the userDTO with the given username + */ public UserDTO getUser(UserDTO userDTO) { 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) { - if(customAuthenticator.authenticate(userDTO)){ + if (customAuthenticator.authenticate(userDTO)) { String userId = userRepository.findByUserName(userDTO.getUserName()).getUserId(); HttpSession session = request.getSession(); session.setAttribute("userId", userId); @@ -61,18 +75,27 @@ public class UserService { 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) { HttpSession session = request.getSession(); - if(session != null){ + if (session != null) { session.invalidate(); } return true; } + /** + * @param userId the id of the currently logged-in user + * @return list of todoDTOs + */ public List<TodoDTO> getAllTodos(String userId) { - List <Todo> todoList = todoRepository.findAllByUserId(userId); + List<Todo> todoList = todoRepository.findAllByUserId(userId); List<TodoDTO> todoDTOList = new ArrayList<>(); todoList.forEach((todoItem) -> { @@ -81,22 +104,36 @@ public class UserService { 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); todoRepository.save(todo); 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) { System.out.println("service here"); - if(todoRepository.existsById(todoId)){ + if (todoRepository.existsById(todoId)) { todoRepository.deleteById(todoId); } 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) { Todo todo = todoRepository.findById(todoId).get(); todo.setCompleted(true); @@ -105,14 +142,24 @@ public class UserService { 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) { HttpSession session = request.getSession(false); System.out.println("Session ID: " + (session != null ? session.getId() : "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) { List<Todo> todoList = todoRepository.findAllByUserId(userId); List<TodoDTO> todoDTOList = new ArrayList<>(); -- GitLab