Checkpoint: added performExchange
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package com.plannaplan.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@ -7,8 +8,10 @@ import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.plannaplan.entities.Assignment;
|
||||
import com.plannaplan.entities.Commision;
|
||||
import com.plannaplan.entities.Exchange;
|
||||
import com.plannaplan.entities.Groups;
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.models.MatchData;
|
||||
import com.plannaplan.repositories.ExchangeRepository;
|
||||
|
||||
@ -21,6 +24,12 @@ public class ExchangeService {
|
||||
@Autowired
|
||||
private ExchangeRepository repo;
|
||||
|
||||
@Autowired
|
||||
private AssignmentService assignmentService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* @param exchange Instance to save in database
|
||||
* @return Exchange Instance contains database id
|
||||
@ -62,14 +71,47 @@ public class ExchangeService {
|
||||
}
|
||||
|
||||
public void performExchange() {
|
||||
final Set<MatchData> matchData = this.getMatches();
|
||||
final List<Long> performedAssignmentExchanges = new ArrayList<>();
|
||||
matchData.forEach( m -> {
|
||||
final Assignment assignmentOne = m.getAssignmentOne();
|
||||
final Assignment assignmentTwo = m.getAssignmentTwo();
|
||||
|
||||
if (!(performedAssignmentExchanges.contains(assignmentOne.getId()) || performedAssignmentExchanges.contains(assignmentTwo.getId()))){
|
||||
final Commision commisionOne = assignmentOne.getCommision();
|
||||
final User userOne = commisionOne.getCommisionOwner();
|
||||
final Commision commisionTwo = assignmentTwo.getCommision();
|
||||
final User userTwo = commisionTwo.getCommisionOwner();
|
||||
|
||||
assignmentOne.setCommision(commisionTwo);
|
||||
assignmentTwo.setCommision(commisionOne);
|
||||
|
||||
userOne.removeGroup(assignmentOne.getGroup().getId());
|
||||
userTwo.removeGroup(assignmentTwo.getGroup().getId());
|
||||
|
||||
userOne.claimGroup(assignmentTwo.getGroup());
|
||||
userTwo.claimGroup(assignmentOne.getGroup());
|
||||
|
||||
this.assignmentService.save(assignmentOne);
|
||||
this.assignmentService.save(assignmentTwo);
|
||||
|
||||
this.userService.save(userOne);
|
||||
this.userService.save(userTwo);
|
||||
|
||||
performedAssignmentExchanges.add(assignmentOne.getId());
|
||||
performedAssignmentExchanges.add(assignmentTwo.getId());
|
||||
}
|
||||
|
||||
this.deleteExchange(m.getExchangeOne());
|
||||
this.deleteExchange(m.getExchangeTwo());
|
||||
});
|
||||
}
|
||||
|
||||
public Set<MatchData> getMatches(){
|
||||
final List<MatchData> matches = this.repo.getMatches().stream().map(m -> {
|
||||
final Exchange exchangeOne = (Exchange) m[0];
|
||||
final Exchange exchangeTwo = (Exchange) m[1];
|
||||
return new MatchData(exchangeOne.getOwnedAssignment(), exchangeTwo.getOwnedAssignment());
|
||||
return new MatchData(exchangeOne, exchangeTwo);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
final Set<MatchData> filterMatches = new HashSet<>(matches);
|
||||
|
Reference in New Issue
Block a user