Checkpoint: added performExchange
This commit is contained in:
parent
1c12a778af
commit
bc8f7407fc
@ -68,6 +68,9 @@ public class Assignment {
|
||||
public Assignment() {
|
||||
}
|
||||
|
||||
public void setCommision(Commision commision) {
|
||||
this.commision = commision;
|
||||
}
|
||||
/**
|
||||
* Id getter
|
||||
*
|
||||
|
@ -47,6 +47,11 @@ public class User {
|
||||
return this.studentRegisteredGrups;
|
||||
}
|
||||
|
||||
public void removeGroup(Long id) {
|
||||
final Groups groupToDelete = this.studentRegisteredGrups.stream().filter(e -> e.getId().equals(id)).findFirst().get();
|
||||
this.studentRegisteredGrups.remove(groupToDelete);
|
||||
}
|
||||
|
||||
public void claimGroup(Groups group) {
|
||||
if (this.studentRegisteredGrups == null) {
|
||||
this.studentRegisteredGrups = new HashSet<>();
|
||||
|
@ -1,27 +1,36 @@
|
||||
package com.plannaplan.models;
|
||||
|
||||
import com.plannaplan.entities.Assignment;
|
||||
import com.plannaplan.entities.Exchange;
|
||||
|
||||
public class MatchData {
|
||||
private Assignment assignmentOne;
|
||||
private Assignment assignmentTwo;
|
||||
private Exchange exchangeOne;
|
||||
private Exchange exchangeTwo;
|
||||
|
||||
public MatchData(Assignment assignmentOne, Assignment assignmentTwo) {
|
||||
this.assignmentOne = assignmentOne;
|
||||
this.assignmentTwo = assignmentTwo;
|
||||
public MatchData(Exchange exchangeOne, Exchange exchangeTwo) {
|
||||
this.exchangeOne = exchangeOne;
|
||||
this.exchangeTwo = exchangeTwo;
|
||||
}
|
||||
|
||||
public Exchange getExchangeOne() {
|
||||
return this.exchangeOne;
|
||||
}
|
||||
|
||||
public Exchange getExchangeTwo() {
|
||||
return this.exchangeTwo;
|
||||
}
|
||||
|
||||
public Assignment getAssignmentTwo() {
|
||||
return assignmentTwo;
|
||||
return this.exchangeTwo.getOwnedAssignment();
|
||||
}
|
||||
|
||||
public Assignment getAssignmentOne() {
|
||||
return assignmentOne;
|
||||
return this.exchangeOne.getOwnedAssignment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.assignmentOne.hashCode() + this.assignmentTwo.hashCode();
|
||||
return this.getAssignmentOne().hashCode() + this.getAssignmentTwo().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -44,6 +53,6 @@ public class MatchData {
|
||||
MatchData c = (MatchData) o;
|
||||
|
||||
// Compare the data members and return accordingly
|
||||
return (this.assignmentOne.equals(c.getAssignmentOne()) && this.assignmentTwo.equals(c.getAssignmentTwo())) || (this.assignmentOne.equals(c.getAssignmentTwo()) && this.assignmentTwo.equals(c.getAssignmentOne()));
|
||||
return (this.getAssignmentOne().equals(c.getAssignmentOne()) && this.getAssignmentTwo().equals(c.getAssignmentTwo())) || (this.getAssignmentOne().equals(c.getAssignmentTwo()) && this.getAssignmentTwo().equals(c.getAssignmentOne()));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user