Skip to content

Commit

Permalink
Merge pull request #78 from JimTheCat/CU-8696y76jy_EPIC---API-v3_King…
Browse files Browse the repository at this point in the history
…a-Traczyk

feature: Create BasicUserInfoDto
  • Loading branch information
KinTrae authored Dec 14, 2024
2 parents a69c49a + 3da6ea0 commit aa8d7e2
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package meowhub.backend.users.controllers;

import lombok.RequiredArgsConstructor;
import meowhub.backend.users.dtos.BasicUserInfoDto;
import meowhub.backend.users.services.UserService;
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 org.webjars.NotFoundException;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;

@GetMapping("get-basic-user-info")
public ResponseEntity<BasicUserInfoDto> getBasicUserInfo(String login) {
try {
userService.getBasicUserInfo(login);
return ResponseEntity.ok(userService.getBasicUserInfo(login));
} catch (NotFoundException e) {
return ResponseEntity.notFound().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package meowhub.backend.users.dtos;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class BasicUserInfoDto {
private String id;
private String name;
private String surname;
private String login;
private byte[] profilePicture;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package meowhub.backend.users.repositories;

import meowhub.backend.users.dtos.BasicUserInfoDto;
import meowhub.backend.users.models.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;
Expand All @@ -13,4 +16,21 @@ public interface UserRepository extends JpaRepository<User, String> {
Boolean existsByLogin(String login);

Boolean existsByEmail(String email);

@Query(value = """
SELECT new meowhub.backend.users.dtos.BasicUserInfoDto (
u.id,
u.name,
u.surname,
u.login,
p.picture
)
FROM User u
LEFT JOIN Picture p ON p.user.id = u.id
LEFT JOIN ProfilePicture pp ON pp.picture.id = p.id
WHERE u.login = :login
ORDER BY pp.id
FETCH FIRST 1 ROWS ONLY
""")
Optional<BasicUserInfoDto> findBasicUserInfoByLogin(@Param("login") String login);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meowhub.backend.users.services;

import meowhub.backend.dtos.UserDto;
import meowhub.backend.users.dtos.BasicUserInfoDto;

import java.util.List;

Expand All @@ -10,4 +11,6 @@ public interface UserService {
UserDto getUserById(String userId);

void changeUserRole(String userId, String roleName);

BasicUserInfoDto getBasicUserInfo(String login);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import meowhub.backend.constants.Genders;
import meowhub.backend.constants.Roles;
import meowhub.backend.dtos.UserDto;
import meowhub.backend.users.dtos.BasicUserInfoDto;
import meowhub.backend.users.models.Role;
import meowhub.backend.users.models.User;
import meowhub.backend.users.repositories.RoleRepository;
import meowhub.backend.users.repositories.UserRepository;
import meowhub.backend.users.services.UserService;
import org.springframework.stereotype.Service;
import org.webjars.NotFoundException;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -41,6 +44,14 @@ public void changeUserRole(String userId, String roleCode) {
userRepository.save(user);
}

@Override
public BasicUserInfoDto getBasicUserInfo(String login) {
Optional<BasicUserInfoDto> basicUserInfoDto = userRepository.findBasicUserInfoByLogin(login);
if(basicUserInfoDto.isEmpty()) throw new NotFoundException("User not found");

return basicUserInfoDto.get();
}

private UserDto mapToUserDto(User user) {
if (user == null) throw new NullPointerException();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package meowhub.backend.users;

import lombok.RequiredArgsConstructor;
import meowhub.backend.InitDataTestConfig;
import meowhub.backend.users.dtos.BasicUserInfoDto;
import meowhub.backend.users.models.User;
import meowhub.backend.users.repositories.UserRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DataJpaTest
@ActiveProfiles("test")
@RequiredArgsConstructor
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Import(InitDataTestConfig.class)
class UserRepositoryIntegrationTest {
@Autowired
private UserRepository userRepository;

@Test
void testFindUserByLogin(){
Optional<User> result = userRepository.findByLogin("admin");
assertTrue(result.isPresent());
assertEquals("admin", result.get().getLogin());
}

@Test
void testFindBasicUserInfoByLogin() {
Optional<BasicUserInfoDto> result = userRepository.findBasicUserInfoByLogin("admin");
assertTrue(result.isPresent());

assertEquals("admin", result.get().getLogin());
assertNotNull(result.get().getId());
assertNotNull(result.get().getName());
assertNotNull(result.get().getSurname());
assertNotNull(result.get().getProfilePicture());
}
}

0 comments on commit aa8d7e2

Please sign in to comment.