Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package back.activitymanager.dto.activity;

import back.activitymanager.dto.user.UserWithoutRolesDto;
import back.activitymanager.dto.user.UserResponseDto;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;

Expand Down Expand Up @@ -29,7 +30,7 @@ public class ActivityDto {

private LocalDateTime localDateTime;

private UserWithoutRolesDto author;
private UserResponseDto author;

private List<UserWithoutRolesDto> participants;
private List<UserResponseDto> participants = new ArrayList<>();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(config = MapperConfig.class)
@Mapper(config = MapperConfig.class, uses = {UserMapper.class})
public interface ActivityMapper {

@Mapping(target = "currentNumberOfPeople", ignore = true)
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/back/activitymanager/mapper/DropboxHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package back.activitymanager.mapper;

import back.activitymanager.service.DropboxService;
import org.mapstruct.Named;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DropboxHelper {

private final DropboxService dropboxService;

@Autowired
public DropboxHelper(DropboxService dropboxService) {
this.dropboxService = dropboxService;
}

@Named("resolvePhotoLink")
public String resolvePhotoLink(String path) {
if (path == null) {
return "";
}

return dropboxService.getPhotoLink(path);
}
}
4 changes: 3 additions & 1 deletion src/main/java/back/activitymanager/mapper/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(config = MapperConfig.class)
@Mapper(config = MapperConfig.class, uses = DropboxHelper.class)
public interface UserMapper {

@Mapping(target = "photoPath", source = "photoPath", qualifiedByName = "resolvePhotoLink")
UserResponseDto toDto(User user);

User toModel(UserRegistrationRequestDto userRegistrationRequestDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
public interface ActivityRepository extends JpaRepository<Activity, Long> {
boolean existsByAuthor(User principal);

@EntityGraph
@Query("SELECT a FROM Activity a WHERE a.isDeleted = false AND a.localDateTime > current_timestamp")
@EntityGraph(attributePaths = {"participants", "author"})
@Query("SELECT a FROM Activity a WHERE a.isDeleted = false "
+ "AND a.localDateTime > current_timestamp")
Optional<Activity> findById(Long id);

@Query("SELECT a FROM Activity a WHERE a.isDeleted = false AND a.localDateTime > current_timestamp")
@EntityGraph(attributePaths = {"participants", "author"})
@Query("SELECT a FROM Activity a WHERE a.isDeleted = false "
+ "AND a.localDateTime > current_timestamp")
Page<Activity> findByAuthorEmail(Pageable pageable, String email);

@Query("SELECT a FROM Activity a WHERE a.isDeleted = false AND a.localDateTime > current_timestamp")
@EntityGraph(attributePaths = {"participants", "author"})
@Query("SELECT a FROM Activity a WHERE a.isDeleted = false "
+ "AND a.localDateTime > current_timestamp")
Page<Activity> findByParticipantsEmail(Pageable pageable, String name);
}
3 changes: 3 additions & 0 deletions src/main/java/back/activitymanager/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.springframework.security.core.Authentication;

public interface UserService {

String DEFAULT_PHOTO_PATH = "default";

UserResponseDto register(UserRegistrationRequestDto userRegistrationRequestDto)
throws RegistrationException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import back.activitymanager.exception.DropboxProcessException;
import back.activitymanager.service.DropboxService;
import back.activitymanager.service.UserService;
import com.dropbox.core.DbxException;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.GetTemporaryLinkResult;
Expand All @@ -27,14 +28,19 @@ public String uploadPhoto(MultipartFile file) {

try (InputStream in = file.getInputStream()) {
client.files().uploadBuilder(path).uploadAndFinish(in);
return path; // зберігаємо шлях (НЕ лінк!)
return path;
} catch (Exception e) {
throw new RuntimeException("Failed to upload photo", e);
}
}

@Override
public String getPhotoLink(String path) {

if (UserService.DEFAULT_PHOTO_PATH.equals(path)) {
return "";
}

try {
GetTemporaryLinkResult result = client.files().getTemporaryLink(path);
return result.getLink();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
@Transactional
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
private static final String DEFAULT_PHOTO_PATH = "default";
private final UserRepository userRepository;
private final UserMapper userMapper;
private final PasswordEncoder passwordEncoder;
Expand All @@ -49,7 +48,7 @@ public UserResponseDto register(UserRegistrationRequestDto request) {
@Override
public UserResponseDto getMyUserInfo(Authentication authentication) {
User user = (User) authentication.getPrincipal();
return mapUserToDtoWithPhoto(user);
return userMapper.toDto(user);
}

@Override
Expand All @@ -63,7 +62,7 @@ public UserResponseDto updateUser(Authentication authentication,
}

user = userRepository.save(user);
return mapUserToDtoWithPhoto(user);
return userMapper.toDto(user);
}

@Override
Expand Down Expand Up @@ -111,17 +110,6 @@ private UserResponseDto buildAndSaveUserWithPhoto(UserRegistrationRequestDto req
"Role is not found: ROLE_USER"));
user.setRoles(Set.of(role));

return mapUserToDtoWithPhoto(userRepository.save(user));
}

private UserResponseDto mapUserToDtoWithPhoto(User user) {
UserResponseDto dto = userMapper.toDto(user);
if (user.getPhotoPath() != null
&& !user.getPhotoPath().equals(DEFAULT_PHOTO_PATH)) {
dto.setPhotoPath(dropboxService.getPhotoLink(user.getPhotoPath()));
} else {
dto.setPhotoPath("");
}
return dto;
return userMapper.toDto(userRepository.save(user));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
databaseChangeLog:
- changeSet:
id: 10-update-users-photo-path
author: andriy
changes:
- update:
tableName: users
columns:
- column:
name: photo_path
value: "default"
where: "id in (1, 2)"
2 changes: 2 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ databaseChangeLog:
file: db/changelog/changes/08-insert-users-roles.yaml
- include:
file: db/changelog/changes/09-add-column-to-activities.yaml
- include:
file: db/changelog/changes/10-update-users-photo-path.yaml