Skip to content

Commit

Permalink
[OT276-36] Endpoint para listar Usuarios (#28)
Browse files Browse the repository at this point in the history
* Added getList() method to UserService

* Implementation of getList() method in UserServiceImpl

* Creation of UserResponse in response package

* Creation of UserControllerMapper in mapper package

* Creation of UserController in controller package

* Added configuration for path '/v1/users' in SecurityConfig

* Added Transactional to getList() in UserServiceImpl

* Switch to JpaRepository in UserRepository

* Refactor of getUsers() method in UserController

* Added '@IterableMapping' to UserControllerMapper and Reformat Code to UserController
  • Loading branch information
Juacun authored Aug 31, 2022
1 parent d17b87f commit d0ff34f
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/alkemy/ong/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alkemy.ong.config.exception.handler.AuthenticationEntryPointHandler;
import com.alkemy.ong.config.exception.handler.CustomAccessDeniedHandler;
import com.alkemy.ong.config.security.JwtRequestFilter;
import com.alkemy.ong.ports.input.rs.api.ApiConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -16,8 +17,6 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletResponse;

@Configuration
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
Expand All @@ -37,6 +36,7 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers(HttpMethod.PATCH).hasRole("ADMIN")
.antMatchers(HttpMethod.DELETE).hasRole("ADMIN")
.antMatchers(HttpMethod.PUT).hasRole("ADMIN")
.antMatchers(HttpMethod.GET, ApiConstants.USERS_URI).hasRole("ADMIN")
.and().exceptionHandling()
.authenticationEntryPoint(new AuthenticationEntryPointHandler())
.accessDeniedHandler(new CustomAccessDeniedHandler())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.alkemy.ong.core.repository;

import com.alkemy.ong.core.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepository extends CrudRepository<User, Long> {
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
}
6 changes: 5 additions & 1 deletion src/main/java/com/alkemy/ong/core/usecase/UserService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.alkemy.ong.core.usecase;


import com.alkemy.ong.core.model.User;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.List;

public interface UserService extends UserDetailsService {

List<User> getList();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alkemy.ong.core.usecase.impl;

import com.alkemy.ong.core.model.User;
import com.alkemy.ong.core.repository.UserRepository;
import com.alkemy.ong.core.usecase.UserService;
import lombok.RequiredArgsConstructor;
Expand All @@ -8,16 +9,25 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

private final UserRepository userRepository;

@Override
@Transactional(readOnly = true)
public List<User> getList() {
return userRepository.findAll();
}

@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String email) {
return userRepository.findByEmail(email)
.orElseThrow(() -> new UsernameNotFoundException("username %s not found".formatted(email)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.alkemy.ong.ports.input.rs.controller;

import com.alkemy.ong.core.model.User;
import com.alkemy.ong.core.usecase.UserService;
import com.alkemy.ong.ports.input.rs.mapper.UserControllerMapper;
import com.alkemy.ong.ports.input.rs.response.UserResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.alkemy.ong.ports.input.rs.api.ApiConstants.USERS_URI;

@RestController
@RequestMapping(USERS_URI)
@RequiredArgsConstructor
public class UserController {

private final UserService service;
private final UserControllerMapper mapper;

@GetMapping
public ResponseEntity<List<UserResponse>> getUsers() {

List<User> list = service.getList();
List<UserResponse> response = mapper.userListToUserResponseList(list);

return ResponseEntity.ok().body(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.alkemy.ong.ports.input.rs.mapper;

import com.alkemy.ong.core.model.User;
import com.alkemy.ong.ports.input.rs.response.UserResponse;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Named;

import java.util.List;

@Mapper
public interface UserControllerMapper extends CommonMapper {

@Named("userToUserResponse")
UserResponse userToUserResponse(User user);

@Named("userListToUserResponseList")
@IterableMapping(qualifiedByName = "userToUserResponse")
List<UserResponse> userListToUserResponseList(List<User> users);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alkemy.ong.ports.input.rs.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserResponse {

private Long id;

@JsonProperty("first_Name")
private String firstName;

@JsonProperty("last_Name")
private String lastName;

private String email;

private String photo;

}

0 comments on commit d0ff34f

Please sign in to comment.