Skip to content
Open
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
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class CaseLabProjectApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package com.example.caselabproject.repositories;

import com.example.caselabproject.models.entities.Application;
import com.example.caselabproject.models.enums.ApplicationStatus;
import com.example.caselabproject.models.enums.RecordState;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDateTime;

import java.util.List;

public interface ApplicationRepository extends JpaRepository<Application, Long> {

List<Application> findAllByRecordStateAndApplicationStatusAndDeadlineDateBefore(
RecordState recordState, ApplicationStatus applicationStatus, LocalDateTime now
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.caselabproject.scheduler;

import com.example.caselabproject.models.entities.Application;
import com.example.caselabproject.models.enums.ApplicationStatus;
import com.example.caselabproject.models.enums.RecordState;
import com.example.caselabproject.repositories.ApplicationRepository;
import com.example.caselabproject.services.implementations.ApplicationItemServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@RequiredArgsConstructor
public class ApplicationScheduler {
private final ApplicationRepository applicationRepository;
private final ApplicationItemServiceImpl applicationItemServiceImpl;

/**
*
*
* @see Application
*/
@Scheduled(cron = "5 * * * * *") // every 5-th second of each minute
public void setScheduler() {
List<Application> applications = applicationRepository
.findAllByRecordStateAndApplicationStatusAndDeadlineDateBefore(
RecordState.ACTIVE, ApplicationStatus.WAITING_FOR_ANSWER, LocalDateTime.now());

for (Application application : applications) {
applicationItemServiceImpl.calcApplicationItemsResult(application);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public ApplicationItemVoteResponseDto voteApplicationItem(Long applicationId,
return ApplicationItemVoteResponseDto.mapFromEntity(applicationItem);
}

void calcApplicationItemsResult(Application application) {
public void calcApplicationItemsResult(Application application) {
AtomicInteger pending = new AtomicInteger();
AtomicInteger accepted = new AtomicInteger();
AtomicInteger denied = new AtomicInteger();
Expand All @@ -188,9 +188,9 @@ void calcApplicationItemsResult(Application application) {
}
});
if(sum * 0.6 >= accepted.get() + denied.get()){
application.setApplicationStatus(ApplicationStatus.NOT_ENOUGH_VOTES);
application.setApplicationStatus(ApplicationStatus.DENIED);
}else if(accepted.get() == denied.get()){
application.setApplicationStatus(ApplicationStatus.DRAW);
application.setApplicationStatus(ApplicationStatus.DENIED);
} else if (accepted.get() > denied.get()) {
application.setApplicationStatus(ApplicationStatus.ACCEPTED);
}else if (accepted.get() < denied.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
@Service
@RequiredArgsConstructor
public class ApplicationServiceImpl implements ApplicationService {

private final ApplicationRepository applicationRepository;

private final UserRepository userRepository;

private final ApplicationItemRepository applicationItemRepository;


Expand Down Expand Up @@ -59,7 +62,6 @@ public ApplicationUpdateResponseDto updateApplication(Long id, String username,
updateApplication.setDeadlineDate(application.getDeadlineDate());
applicationRepository.save(updateApplication);
}

return ApplicationUpdateResponseDto.mapFromEntity(application);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ private boolean departmentIsActive(Department department) {
*/
private Department saveInternal(Department department) {
try {
// при использовании просто save(), мы не сможем обработать ограничение
// уникальности, поэтому используем saveAndFlush().
return departmentRepository.save(department);
} catch (DataIntegrityViolationException ex) {
throw new DepartmentNameExistsException(department.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.caselabproject.repositories;


import com.example.caselabproject.models.entities.Application;
import com.example.caselabproject.models.enums.ApplicationStatus;
import com.example.caselabproject.models.enums.RecordState;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
class ApplicationRepositoryTest {
@Autowired
private ApplicationRepository underTest;

@Test /* Application#findAllByRecordStateAndApplicationStatusAndDeadlineDateBefore */
void shouldReturnNotEmptyList_whenStateIsActiveAndStatusIsWaiting() {
// given
LocalDateTime now = LocalDateTime.now();
final Application first = getApplication(
RecordState.ACTIVE, ApplicationStatus.WAITING_FOR_ANSWER, now.minusWeeks(1));
final Application second = getApplication(
RecordState.ACTIVE, ApplicationStatus.WAITING_FOR_ANSWER, now.minusMinutes(1));
final Application third = getApplication(
RecordState.DELETED, ApplicationStatus.WAITING_FOR_ANSWER, now.minusWeeks(1));
final Application fourth = getApplication(
RecordState.DELETED, ApplicationStatus.WAITING_FOR_ANSWER, now.plusHours(4));
final Application fifth = getApplication(
RecordState.ACTIVE, ApplicationStatus.DENIED, now.minusWeeks(1));
final Application sixth = getApplication(
RecordState.ACTIVE, ApplicationStatus.WAITING_FOR_ANSWER, now.plusMinutes(1));
final Application seventh = getApplication(
RecordState.DELETED, ApplicationStatus.WAITING_FOR_ANSWER, now.minusWeeks(1));

underTest.saveAll(List.of(first, second, third, fourth, fifth, sixth, seventh));

// when
final List<Application> actualApplications = underTest
.findAllByRecordStateAndApplicationStatusAndDeadlineDateBefore(
RecordState.ACTIVE, ApplicationStatus.WAITING_FOR_ANSWER, now);

// then
assertThat(actualApplications)
.hasSize(2)
.contains(first, second);
}

private Application getApplication(
RecordState state, ApplicationStatus status, LocalDateTime deadline) {
return Application.builder()
.name("something")
.recordState(state)
.applicationStatus(status)
.deadlineDate(deadline)
.build();
}
}