Skip to content

Commit

Permalink
add fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vertonowsky committed Jun 21, 2024
1 parent 52c60f8 commit bba54fb
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/example/backend/email/AsyncConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ public Executor emailTaskExecutor() {
return executor;
}

@Bean(name = "schemeTaskExecutor")
public Executor schemeTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(5);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("SchemeThread-");
executor.initialize();
return executor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public void delete(@UserEmail String email, @PathVariable("id") Long id) throws
schemeService.delete(id, email);
}

@PutMapping("visibility")
public List<SchemeToCarDto> changeVisibility(@RequestBody @Valid SchemeDisplayDto schemeDisplayDto) throws IllegalInputException {
return schemeToCarService.changeVisibility(schemeDisplayDto).stream().map(SchemeSerializer::schemeToCar).collect(Collectors.toList());
@PutMapping("{id}/visibility")
public List<SchemeToCarDto> changeVisibility(@PathVariable("id") Long id, @RequestBody @Valid SchemeDisplayDto schemeDisplayDto) throws IllegalInputException {
return schemeToCarService.changeVisibility(id, schemeDisplayDto).stream().map(SchemeSerializer::schemeToCar).collect(Collectors.toList());
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/example/backend/scheme/dto/SchemeDisplayDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class SchemeDisplayDto {

private CarStatus status;

private boolean all;

public Set<Long> getIds() {
return ids;
}
Expand All @@ -29,4 +31,12 @@ public CarStatus getStatus() {
public void setStatus(CarStatus status) {
this.status = status;
}

public Boolean getAll() {
return all;
}

public void setAll(Boolean all) {
this.all = all;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public interface SchemeToCarRepository extends JpaRepository<SchemeToCar, Long>

Page<SchemeToCar> findBySchemeId(Long id, Pageable pageable);

Set<SchemeToCar> findAllByIdIn(@Param("ids") Set<Long> ids);
List<SchemeToCar> findAllByIdIn(@Param("ids") Set<Long> ids);

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

Expand Down Expand Up @@ -113,7 +114,7 @@ public Scheme edit(Long id, String email, SchemeDto schemeDto) throws IllegalInp
scheme.setData(schemeDto.getData());
}

if (!scheme.getData().equals(EMPTY_JSON_OBJECT) && !sameData)
if (!sameData)
loadDataForSingleScheme(scheme, getTimerQuery(), true);

return schemeRepository.save(scheme);
Expand Down Expand Up @@ -178,6 +179,9 @@ public void checkNewOffersCount(List<SchemeDto> schemeDtoList) {
* @param query final GraphQL indicating String query
*/
public void loadDataForSingleScheme(Scheme scheme, String query, boolean oneTime) {
if (scheme.getData().equals(EMPTY_JSON_OBJECT))
return;

OtomotoDto otomotoDto;
try {
otomotoDto = OBJECT_MAPPER.readValue(scheme.getData(), OtomotoDto.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.example.backend.scheme.repository.SchemeToCarRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
Expand All @@ -19,22 +20,25 @@ public SchemeToCarService(SchemeToCarRepository schemeToCarRepository) {
this.schemeToCarRepository = schemeToCarRepository;
}

public Set<SchemeToCar> changeVisibility(SchemeDisplayDto schemeDisplayDto) throws IllegalInputException {
public List<SchemeToCar> changeVisibility(Long schemeId, SchemeDisplayDto schemeDisplayDto) throws IllegalInputException {
CarStatus status = schemeDisplayDto.getStatus();
Set<Long> schemeToCarIds = schemeDisplayDto.getIds();

if (status == null || CarStatus.FIRST_INITIALIZATION.equals(status))
throw new IllegalInputException(ErrorCode.STATUS_NOT_ALLOWED);

if (schemeToCarIds == null || schemeToCarIds.isEmpty())
if (!schemeDisplayDto.getAll() && schemeToCarIds == null || schemeToCarIds.isEmpty())
throw new IllegalInputException(ErrorCode.CAR_IDS_REQUIRED);

Set<SchemeToCar> schemeToCars = schemeToCarRepository.findAllByIdIn(schemeToCarIds);
List<SchemeToCar> schemeToCars = schemeDisplayDto.getAll() ?
schemeToCarRepository.findAllBySchemeId(schemeId) :
schemeToCarRepository.findAllByIdIn(schemeToCarIds);

for (SchemeToCar schemeToCar : schemeToCars)
schemeToCar.setStatus(status);

schemeToCarRepository.saveAll(schemeToCars);
return schemeToCars;
return schemeDisplayDto.getAll() ? schemeToCars.stream().limit(20).toList() : schemeToCars;
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/example/backend/timer/OtomotoTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.example.backend.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -37,6 +38,7 @@ public OtomotoTimer(UserService userService, SchemeService schemeService) {
* @throws EntityNotFoundException thrown if application setting is missing
*/
@Scheduled(fixedRate = 60 * 5 * 1000)
@Async("schemeTaskExecutor")
public void runTimer() throws EntityNotFoundException {
List<User> allUsers = userService.getAll();
if (Collections.isNullOrEmpty(allUsers))
Expand Down

0 comments on commit bba54fb

Please sign in to comment.