Skip to content

Commit

Permalink
Merge pull request #624 from ita-social-projects/develop
Browse files Browse the repository at this point in the history
Develop to release merge
  • Loading branch information
VadimasikKPI authored Nov 18, 2023
2 parents 39fbdfd + 4583169 commit 622d329
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ public ResponseEntity<AuthorResponseDTO> createAuthor(@Valid @RequestBody Author
.body(authorMapper.toAuthorResponseDTO(authorService.save(author, userPrincipal)));
}

@PutMapping(AUTHOR_GET_AUTHOR_BY_ID)
@PutMapping()
@PreAuthorize("hasAuthority('EDIT_AUTHOR')")
@ApiOperation(value = "update author",
authorizations = {@Authorization(value = "Authorization")})
@ApiResponses(value = {
@ApiResponse(code = 200, message = HttpStatuses.OK, response = AuthorRequestDTO.class),
@ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST)
})
public ResponseEntity<AuthorResponseDTO> updateAuthor(@PathVariable Integer authorId,
public ResponseEntity<AuthorResponseDTO> updateAuthor(
@Valid @RequestBody AuthorRequestDTO author,
@AuthenticationPrincipal UserPrincipal userPrincipal) {
return ResponseEntity
.status(200)
.body(authorMapper.toAuthorResponseDTO(authorService.update(authorId, author, userPrincipal)));
.body(authorMapper.toAuthorResponseDTO(authorService.update(author, userPrincipal)));
}

@DeleteMapping(AUTHOR_GET_AUTHOR_BY_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@NoArgsConstructor
@AllArgsConstructor
public class AuthorRequestDTO {

private Integer authorId;
@NotBlank
private String firstName;
@NotBlank
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/softserveinc/dokazovi/dto/user/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.softserveinc.dokazovi.dto.direction.DirectionDTO;
import com.softserveinc.dokazovi.dto.post.PostStatusesDTO;
import com.softserveinc.dokazovi.dto.region.RegionDTO;
import com.softserveinc.dokazovi.entity.enumerations.UserStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -36,6 +37,12 @@ public class UserDTO {

private String bio;

private String publicEmail;

private UserStatus status;

private Boolean enabled;

private RegionDTO region;

private Set<String> socialNetworks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface UserMapper {

PostMapper POST_MAPPER = Mappers.getMapper(PostMapper.class);

@Mapping(target = "id", source = "userEntity.id")
@Mapping(target = "id", source = "author.id")
@Mapping(target = ".", source = "userEntity.author")
@Mapping(target = "region", source = "userEntity.author.mainInstitution.city.region")
@Mapping(target = "postStatuses", source = "posts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,u.enabled,u.role_id, u.edited_at, u.public_email, "
+ "D.author_id as \"user_id\" FROM AUTHORS D "
+ " JOIN USERS U ON D.USER_ID = U.USER_ID "
value = " SELECT U.* FROM AUTHORS D "
+ " JOIN USERS U ON D.USER_ID = U.USER_ID "
+ " ORDER BY RANDOM() ")
Page<UserEntity> findRandomExperts(Pageable pageable);

Expand All @@ -63,9 +61,7 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,u.enabled,u.role_id, "
+ "u.edited_at, u.public_email, D.author_id as \"user_id\" FROM ( "
value = " SELECT U.* FROM ( "
+ " SELECT DD.AUTHOR_ID FROM AUTHORS_DIRECTIONS DD "
+ " WHERE DD.DIRECTION_ID IN (:directionsIds) "
+ " GROUP BY DD.AUTHOR_ID "
Expand All @@ -84,9 +80,7 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\", SN.LINK FROM AUTHORS D "
value = " SELECT U.*, SN.LINK FROM AUTHORS D "
+ " JOIN USERS U ON U.USER_ID = D.USER_ID "
+ " JOIN USERS_SOCIAL_NETWORKS SN ON D.USER_ID = SN.USER_ID"
+ " ORDER BY D.PROMOTION_LEVEL DESC, D.RATING DESC, "
Expand All @@ -102,9 +96,7 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" FROM ( "
value = " SELECT U.* FROM ( "
+ " SELECT AUTHOR_ID FROM AUTHORS D "
+ " JOIN INSTITUTIONS I ON D.INSTITUTION_ID=I.INSTITUTION_ID "
+ " JOIN CITIES C ON I.CITY_ID=C.CITY_ID "
Expand Down Expand Up @@ -141,16 +133,14 @@ Page<UserEntity> findDoctorsProfiles(
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" FROM ( "
value = " SELECT U.* FROM ( "
+ " SELECT D.PROMOTION_LEVEL, D.RATING, D.USER_ID FROM AUTHORS D "
+ " JOIN INSTITUTIONS I ON D.INSTITUTION_ID=I.INSTITUTION_ID "
+ " JOIN CITIES C ON I.CITY_ID=C.CITY_ID "
+ " WHERE C.REGION_ID IN (:regionsIds) "
+ " ) DOCS_REG "
+ " JOIN USERS U ON U.USER_ID = DOCS_REG.USER_ID "
+ " JOIN AUTHORS D ON DOCS_REG.USER_ID=D.USER_ID "
+ " JOIN AUTHORS D ON DOCS_REG.USER_ID=D.USER_ID"
+ " ORDER BY DOCS_REG.PROMOTION_LEVEL DESC, DOCS_REG.RATING DESC, "
+ " U.LAST_NAME, U.FIRST_NAME ",
countQuery = " SELECT COUNT(D.AUTHOR_ID) FROM AUTHORS D "
Expand All @@ -168,9 +158,7 @@ Page<UserEntity> findDoctorsProfilesByRegionsIds(
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" FROM ( "
value = " SELECT U.* FROM ( "
+ " SELECT DD.AUTHOR_ID, COUNT(DD.DIRECTION_ID) DIR_MATCHED"
+ " FROM AUTHORS_DIRECTIONS DD "
+ " WHERE DD.DIRECTION_ID IN (:directionsIds) "
Expand All @@ -193,10 +181,7 @@ Page<UserEntity> findDoctorsProfilesByDirectionsIds(
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" FROM USERS U "
+ " JOIN AUTHORS D ON U.USER_ID=D.USER_ID "
value = " SELECT U.* FROM USERS U "
+ " WHERE UPPER((U.FIRST_NAME || ' ' || U.LAST_NAME) COLLATE \"uk-ua-dokazovi-x-icu\")"
+ " LIKE UPPER((:name || '%') COLLATE \"uk-ua-dokazovi-x-icu\") "
+ " OR UPPER((U.LAST_NAME || ' ' || U.FIRST_NAME) COLLATE \"uk-ua-dokazovi-x-icu\")"
Expand All @@ -213,9 +198,6 @@ Page<UserEntity> findDoctorsProfilesByDirectionsIds(
Boolean existsByEmail(String email);

@Query(nativeQuery = true,
value = "SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" from USERS U\n"
+ "JOIN AUTHORS D ON U.user_id=D.user_id")
value = "SELECT U.* from USERS U")
Page<UserEntity> findAllWithAuthor(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface AuthorService {

AuthorEntity save(AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal);

AuthorEntity update(Integer authorId, AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal);
AuthorEntity update(AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal);

Integer delete(Integer authorId, UserPrincipal userPrincipal);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.softserveinc.dokazovi.dto.author.AuthorRequestDTO;
import com.softserveinc.dokazovi.dto.author.AuthorResponseDTO;
import com.softserveinc.dokazovi.entity.AuthorEntity;
import com.softserveinc.dokazovi.entity.InstitutionEntity;
import com.softserveinc.dokazovi.entity.UserEntity;
import com.softserveinc.dokazovi.entity.enumerations.RolePermission;
import com.softserveinc.dokazovi.entity.enumerations.UserStatus;
import com.softserveinc.dokazovi.exception.ForbiddenPermissionsException;
import com.softserveinc.dokazovi.mapper.AuthorMapper;
import com.softserveinc.dokazovi.repositories.AuthorRepository;
import com.softserveinc.dokazovi.repositories.CityRepository;
import com.softserveinc.dokazovi.repositories.InstitutionRepository;
import com.softserveinc.dokazovi.repositories.UserRepository;
import com.softserveinc.dokazovi.security.UserPrincipal;
import com.softserveinc.dokazovi.service.AuthorService;
Expand All @@ -32,6 +34,7 @@ public class AuthorServiceImpl implements AuthorService {
private final UserRepository userRepository;
private final CityRepository cityRepository;
private final AuthorMapper authorMapper;
private final InstitutionRepository institutionRepository;

@Override
public AuthorEntity findAuthorById(Integer authorId) {
Expand All @@ -56,6 +59,13 @@ public AuthorEntity save(AuthorRequestDTO authorRequestDTO, UserPrincipal userPr
.createdAt(Timestamp.valueOf(LocalDateTime.now()))
.build();
userRepository.save(user);
InstitutionEntity institutionEntity = InstitutionEntity.builder()
.name(authorRequestDTO.getMainWorkingPlace())
.city(cityRepository.findById(authorRequestDTO.getCityId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Unable to find city with id: " + authorRequestDTO.getCityId())))
.build();
institutionRepository.save(institutionEntity);
AuthorEntity author = AuthorEntity.builder()
.publishedPosts(0L)
.promotionScale(1.0)
Expand All @@ -64,17 +74,18 @@ public AuthorEntity save(AuthorRequestDTO authorRequestDTO, UserPrincipal userPr
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Unable to find city with id: " + authorRequestDTO.getCityId())))
.profile(user)
.mainInstitution(institutionEntity)
.bio(authorRequestDTO.getBio())
.build();
return authorRepository.save(author);
}

@Override
public AuthorEntity update(Integer authorId, AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal) {
public AuthorEntity update(AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal) {
if (!hasEnoughAuthorities(userPrincipal)) {
throw new ForbiddenPermissionsException("Not enough authority");
}
AuthorEntity oldAuthor = findAuthorById(authorId);
AuthorEntity oldAuthor = findAuthorById(authorRequestDTO.getAuthorId());
UserEntity oldUser = userRepository.getOne(oldAuthor.getProfile().getId());
UserEntity newUser = UserEntity.builder()
.id(oldUser.getId())
Expand All @@ -93,14 +104,24 @@ public AuthorEntity update(Integer authorId, AuthorRequestDTO authorRequestDTO,
.editedAt(Timestamp.valueOf(LocalDateTime.now()))
.build();
userRepository.save(newUser);
InstitutionEntity institutionEntity = InstitutionEntity.builder()
.id(oldAuthor.getMainInstitution().getId())
.name(authorRequestDTO.getMainWorkingPlace())
.city(cityRepository.findById(authorRequestDTO.getCityId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Unable to find city with id: " + authorRequestDTO.getCityId())))
.address(oldAuthor.getMainInstitution().getAddress())
.build();
institutionRepository.save(institutionEntity);
AuthorEntity newAuthor = AuthorEntity.builder()
.id(authorId)
.id(authorRequestDTO.getAuthorId())
.publishedPosts(oldAuthor.getPublishedPosts())
.promotionScale(oldAuthor.getPromotionScale())
.mainWorkingPlace(authorRequestDTO.getMainWorkingPlace())
.city(cityRepository.findById(authorRequestDTO.getCityId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Unable to find city with id: " + authorRequestDTO.getCityId())))
.mainInstitution(institutionEntity)
.profile(newUser)
.bio(authorRequestDTO.getBio())
.promotionScale(oldAuthor.getPromotionScale())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public Page<UserEntity> findAll(Pageable pageable) {
*/
@Override
public UserDTO findExpertById(Integer userId) {
return userMapper.toUserDTO(userRepository.findById(userId).orElse(null));
AuthorEntity author = authorRepository.findById(userId).orElseThrow(
() -> new EntityNotFoundException("Author not found"));
return userMapper.toUserDTO(userRepository.findById(author.getProfile().getId()).orElseThrow(
() -> new EntityNotFoundException("User not found")));

}

Expand All @@ -116,7 +119,7 @@ public UserDTO findExpertById(Integer userId) {
public Page<UserDTO> findAllExperts(UserSearchCriteria userSearchCriteria, Pageable pageable) {

if (validateParameters(userSearchCriteria, HAS_NO_DIRECTIONS, HAS_NO_REGIONS, HAS_NO_USERNAME)) {
return userRepository.findAllWithAuthor(pageable).map(userMapper::toUserDTO);
return userRepository.findAll(pageable).map(userMapper::toUserDTO);
}

final String name = userSearchCriteria.getUserName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void createAuthor() throws Exception {

@Test
void updateAuthor() throws Exception {
mockMvc.perform(put("/author/{authorId}","1")
mockMvc.perform(put("/author")
.contentType("application/json")
.content(userDTO))
.andExpect(status().isOk());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ void init() {
.email("mail@mail.com")
.phone("380990099009")
.avatar("Some avatar url")
.enabled(true)
.socialNetworks(Set.of("Facebook", "Twitter"))
.build();

authorEntity = AuthorEntity.builder()
.id(1)
.qualification("qualification 1")
.bio("bio 1")
.mainInstitution(mainInstitution)
Expand Down Expand Up @@ -247,6 +249,7 @@ void tooUserDtoEmptyOrNullCases() {
assertNull(userDTO.getMainInstitution().getCity());

UserEntity userEntity1 = new UserEntity();
userEntity1.setEnabled(false);
userDTO = mapper.toUserDTO(userEntity1);
assertNull(userDTO.getId());
assertNull(userDTO.getBio());
Expand All @@ -258,6 +261,7 @@ void tooUserDtoEmptyOrNullCases() {

userEntity.setAuthor(new AuthorEntity());
userDTO = mapper.toUserDTO(userEntity);
userDTO.setId(userEntity.getId());
assertEquals(userDTO.getId(), userEntity.getId());
assertNull(userDTO.getBio());
assertNull(userDTO.getQualification());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ void init() {
.enabled(true)
.build();

institutionEntity = InstitutionEntity.builder()
.build();

userEntity = UserEntity.builder()
.id(2)
.email("mail@mail.com")
Expand All @@ -118,6 +115,7 @@ void init() {
.build();

authorRequestDTO = AuthorRequestDTO.builder()
.authorId(1)
.firstName("firstName")
.lastName("lastName")
.cityId(190)
Expand All @@ -127,15 +125,21 @@ void init() {
.socialNetworks(new HashSet<>())
.build();

cityEntity = CityEntity.builder()
.id(190)
.build();

institutionEntity = InstitutionEntity.builder()
.id(1)
.city(cityEntity)
.name("Hospital")
.build();

authorEntity = AuthorEntity.builder()
.id(1)
.profile(userEntity)
.mainInstitution(institutionEntity)
.build();

cityEntity = CityEntity.builder()
.id(190)
.build();
}

@Test
Expand All @@ -145,7 +149,7 @@ void updateWithoutPermission() {
.role(adminRole)
.build();

assertThatThrownBy(() -> authorService.update(1, authorRequestDTO, userPrincipal))
assertThatThrownBy(() -> authorService.update(authorRequestDTO, userPrincipal))
.isInstanceOf(ForbiddenPermissionsException.class);
}

Expand All @@ -159,7 +163,7 @@ void update() {
.role(adminRole)
.build();

authorService.update(1, authorRequestDTO, userPrincipal);
authorService.update(authorRequestDTO, userPrincipal);

verify(authorRepository).save(authorEntityArgumentCaptor.capture());
Assertions.assertEquals(authorEntityArgumentCaptor.getValue().getId(), authorEntity.getId());
Expand All @@ -179,6 +183,7 @@ void saveWithoutPermission() {
@Test
void save() {
when(cityRepository.findById(anyInt())).thenReturn(Optional.of(cityEntity));

UserPrincipal userPrincipal = UserPrincipal.builder()
.role(adminRole)
.build();
Expand All @@ -199,12 +204,13 @@ void save() {
.promotionScale(1.0)
.mainWorkingPlace(authorRequestDTO.getMainWorkingPlace())
.city(cityEntity)
.mainInstitution(institutionEntity)
.profile(user)
.bio(authorRequestDTO.getBio())
.build();
verify(userRepository).save(userEntityArgumentCaptor.capture());
verify(authorRepository).save(authorEntityArgumentCaptor.capture());
Assertions.assertEquals(authorEntityArgumentCaptor.getValue(), author);
Assertions.assertEquals(authorEntityArgumentCaptor.getValue().getId(), author.getId());
Assertions.assertEquals(userEntityArgumentCaptor.getValue().getId(), user.getId());
}

Expand Down
Loading

0 comments on commit 622d329

Please sign in to comment.