diff --git a/src/main/java/com/alkemy/ong/config/SecurityConfig.java b/src/main/java/com/alkemy/ong/config/SecurityConfig.java index 5d14a5e8..40853054 100644 --- a/src/main/java/com/alkemy/ong/config/SecurityConfig.java +++ b/src/main/java/com/alkemy/ong/config/SecurityConfig.java @@ -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; @@ -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 { @@ -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()) diff --git a/src/main/java/com/alkemy/ong/core/repository/UserRepository.java b/src/main/java/com/alkemy/ong/core/repository/UserRepository.java index 3ef61e52..0cd11d27 100644 --- a/src/main/java/com/alkemy/ong/core/repository/UserRepository.java +++ b/src/main/java/com/alkemy/ong/core/repository/UserRepository.java @@ -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 { +public interface UserRepository extends JpaRepository { Optional findByEmail(String email); } diff --git a/src/main/java/com/alkemy/ong/core/usecase/UserService.java b/src/main/java/com/alkemy/ong/core/usecase/UserService.java index 657941c8..a26e5b04 100644 --- a/src/main/java/com/alkemy/ong/core/usecase/UserService.java +++ b/src/main/java/com/alkemy/ong/core/usecase/UserService.java @@ -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 getList(); + } diff --git a/src/main/java/com/alkemy/ong/core/usecase/impl/UserServiceImpl.java b/src/main/java/com/alkemy/ong/core/usecase/impl/UserServiceImpl.java index 40109ffb..46908828 100644 --- a/src/main/java/com/alkemy/ong/core/usecase/impl/UserServiceImpl.java +++ b/src/main/java/com/alkemy/ong/core/usecase/impl/UserServiceImpl.java @@ -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; @@ -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 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))); } + } diff --git a/src/main/java/com/alkemy/ong/ports/input/rs/controller/UserController.java b/src/main/java/com/alkemy/ong/ports/input/rs/controller/UserController.java new file mode 100644 index 00000000..b30989e8 --- /dev/null +++ b/src/main/java/com/alkemy/ong/ports/input/rs/controller/UserController.java @@ -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> getUsers() { + + List list = service.getList(); + List response = mapper.userListToUserResponseList(list); + + return ResponseEntity.ok().body(response); + } + +} diff --git a/src/main/java/com/alkemy/ong/ports/input/rs/mapper/UserControllerMapper.java b/src/main/java/com/alkemy/ong/ports/input/rs/mapper/UserControllerMapper.java new file mode 100644 index 00000000..80104309 --- /dev/null +++ b/src/main/java/com/alkemy/ong/ports/input/rs/mapper/UserControllerMapper.java @@ -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 userListToUserResponseList(List users); +} diff --git a/src/main/java/com/alkemy/ong/ports/input/rs/response/UserResponse.java b/src/main/java/com/alkemy/ong/ports/input/rs/response/UserResponse.java new file mode 100644 index 00000000..20368102 --- /dev/null +++ b/src/main/java/com/alkemy/ong/ports/input/rs/response/UserResponse.java @@ -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; + +}