Skip to content

Commit

Permalink
refactor: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Dec 23, 2024
1 parent d11e5c2 commit 5a4664b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,4 @@

public class PGSQLMapDialect extends PostgreSQLDialect {

// @Override
// public boolean equivalentTypes(int typeCode1, int typeCode2) {
// boolean result = super.equivalentTypes(typeCode1, typeCode2) || (SqlTypes.isCharacterOrClobType(typeCode1) && SqlTypes.isCharacterOrClobType(typeCode2));
// return result;
// }
//
// @Override
// protected void registerColumnTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
// super.registerColumnTypes(typeContributions, serviceRegistry);
// final DdlTypeRegistry ddlTypeRegistry = typeContributions.getTypeConfiguration().getDdlTypeRegistry();
// ddlTypeRegistry.addDescriptor( new DdlTypeImpl( SqlTypes.VARCHAR, "clob", this ) );
// ddlTypeRegistry.addDescriptor( new DdlTypeImpl( SqlTypes.VARCHAR, "text", this ) );
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class DataMigrationService {
private final UserRepository userRepository;
private final UserDetailService userDetailService;
private final UserDetailService userDetailService;

public DataMigrationService(UserRepository userRepository, UserDetailService userDetailService) {
this.userRepository = userRepository;
Expand All @@ -40,15 +40,18 @@ public DataMigrationService(UserRepository userRepository, UserDetailService use

@Async
public CompletableFuture<Long> encryptUserKeys() {
List<User> migratedUsers = this.userRepository.findOpenMigrations(1L).stream().map(myUser -> {
myUser.setUuid(Optional.ofNullable(myUser.getUuid()).filter(Predicate.not(String::isBlank)).orElse(UUID.randomUUID().toString()));
myUser.setMoviedbkey(this.userDetailService.encrypt(myUser.getMoviedbkey(), myUser.getUuid()));
myUser.setMigration(myUser.getMigration() + 1);
return myUser;
}).collect(Collectors.toList());
List<User> migratedUsers = this.userRepository.findOpenMigrations(1L).stream().map(myUser -> updateUser(myUser))
.collect(Collectors.toList());
this.userRepository.saveAll(migratedUsers);
return CompletableFuture.completedFuture(Integer.valueOf(migratedUsers.size()).longValue());
}


private User updateUser(User myUser) {
myUser.setUuid(Optional.ofNullable(myUser.getUuid()).filter(Predicate.not(String::isBlank))
.orElse(UUID.randomUUID().toString()));
myUser.setMoviedbkey(this.userDetailService.encrypt(myUser.getMoviedbkey(), myUser.getUuid()));
myUser.setMigration(myUser.getMigration() + 1);
return myUser;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public Optional<Movie> findMovieById(Long id, String bearerStr) {
public boolean deleteMovieById(Long id, String bearerStr) {
boolean result = true;
try {
User user = this.userDetailService.getCurrentUser(bearerStr);
final User user = this.userDetailService.getCurrentUser(bearerStr);
Optional<Movie> movieOpt = this.movieRep.findById(id);
if (movieOpt.isPresent() && movieOpt.get().getUsers().contains(user)) {
if (movieOpt.stream().map(myMovie -> myMovie.getUsers()).anyMatch(myUsers -> myUsers.contains(user))) {
Movie movie = movieOpt.get();
movie.getUsers().remove(user);
if (movie.getUsers().isEmpty()) {
Expand All @@ -130,6 +130,7 @@ public boolean deleteMovieById(Long id, String bearerStr) {
}
}
} catch (RuntimeException re) {
LOG.error("Delete movie failed.",re);
result = false;
}
return result;
Expand Down Expand Up @@ -178,11 +179,11 @@ public boolean importMovie(int movieDbId, String bearerStr) throws InterruptedEx
WrapperGenereDto result = this.movieDbRestClient
.fetchAllGeneres(this.decrypt(user.getMoviedbkey(), user.getUuid()));
List<Genere> generes = new ArrayList<>(this.genereRep.findAll());
for (GenereDto g : result.getGenres()) {
for (GenereDto gDto : result.getGenres()) {
Genere genereEntity = generes.stream()
.filter(myGenere -> Optional.ofNullable(myGenere.getGenereId()).stream()
.anyMatch(myGenereId -> myGenereId.equals(g.getId())))
.findFirst().orElse(this.mapper.convert(g));
.anyMatch(myGenereId -> myGenereId.equals(gDto.getId())))
.findFirst().orElse(this.mapper.convert(gDto));
if (genereEntity.getId() == null) {
genereEntity = genereRep.save(genereEntity);
generes.add(genereEntity);
Expand Down Expand Up @@ -224,17 +225,17 @@ public boolean importMovie(int movieDbId, String bearerStr) throws InterruptedEx
WrapperCastDto wrCast = this.movieDbRestClient.fetchCast(this.decrypt(user.getMoviedbkey(), user.getUuid()),
movieDto.getId());
if (movieEntity.getCast().isEmpty()) {
for (CastDto c : wrCast.getCast()) {
for (CastDto cDto : wrCast.getCast()) {
LOG.info("Creating new cast for movie");
if (c.getCharacter() == null || c.getCharacter().isBlank() || c.getName() == null
|| c.getName().isBlank()) {
if (cDto.getCharacter() == null || cDto.getCharacter().isBlank() || cDto.getName() == null
|| cDto.getName().isBlank()) {
continue;
}
Cast castEntity = this.mapper.convert(c);
Cast castEntity = this.mapper.convert(cDto);
movieEntity.getCast().add(castEntity);
castEntity.setMovie(movieEntity);
ActorDto actor = this.movieDbRestClient.fetchActor(this.decrypt(user.getMoviedbkey(), user.getUuid()),
c.getId(), 300L);
cDto.getId(), 300L);
Actor actorEntity = this.actorRep.findByActorId(actor.getActorId(), user.getId())
.orElse(this.mapper.convert(actor));
castEntity = this.castRep.save(castEntity);
Expand All @@ -246,12 +247,12 @@ public boolean importMovie(int movieDbId, String bearerStr) throws InterruptedEx
castEntity.setActor(actorEntity);
}
} else {
for (CastDto c : wrCast.getCast()) {
for (CastDto cDto : wrCast.getCast()) {
LOG.info("update cast for movie");
ActorDto actor = this.movieDbRestClient.fetchActor(this.decrypt(user.getMoviedbkey(), user.getUuid()),
c.getId(), 300L);
Optional<Actor> actorOpt = this.actorRep.findByActorId(actor.getActorId(), user.getId());
Actor actorEntity = actorOpt.orElse(this.mapper.convert(actor));
cDto.getId(), 300L);
Actor actorEntity = this.actorRep.findByActorId(actor.getActorId(), user.getId())
.orElse(this.mapper.convert(actor));
actorEntity = this.actorRep.save(actorEntity);
if (!actorEntity.getUsers().contains(user)) {
actorEntity.getUsers().add(user);
Expand Down

0 comments on commit 5a4664b

Please sign in to comment.