Checkpoint: added performExchange

This commit is contained in:
Marcin Woźniak 2021-01-12 14:10:48 +01:00
parent 1c12a778af
commit bc8f7407fc
Signed by: y0rune
GPG Key ID: F204C385F57EB348
4 changed files with 99 additions and 40 deletions

View File

@ -68,6 +68,9 @@ public class Assignment {
public Assignment() { public Assignment() {
} }
public void setCommision(Commision commision) {
this.commision = commision;
}
/** /**
* Id getter * Id getter
* *

View File

@ -47,6 +47,11 @@ public class User {
return this.studentRegisteredGrups; 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) { public void claimGroup(Groups group) {
if (this.studentRegisteredGrups == null) { if (this.studentRegisteredGrups == null) {
this.studentRegisteredGrups = new HashSet<>(); this.studentRegisteredGrups = new HashSet<>();

View File

@ -1,27 +1,36 @@
package com.plannaplan.models; package com.plannaplan.models;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange;
public class MatchData { public class MatchData {
private Assignment assignmentOne; private Exchange exchangeOne;
private Assignment assignmentTwo; private Exchange exchangeTwo;
public MatchData(Assignment assignmentOne, Assignment assignmentTwo) { public MatchData(Exchange exchangeOne, Exchange exchangeTwo) {
this.assignmentOne = assignmentOne; this.exchangeOne = exchangeOne;
this.assignmentTwo = assignmentTwo; this.exchangeTwo = exchangeTwo;
}
public Exchange getExchangeOne() {
return this.exchangeOne;
}
public Exchange getExchangeTwo() {
return this.exchangeTwo;
} }
public Assignment getAssignmentTwo() { public Assignment getAssignmentTwo() {
return assignmentTwo; return this.exchangeTwo.getOwnedAssignment();
} }
public Assignment getAssignmentOne() { public Assignment getAssignmentOne() {
return assignmentOne; return this.exchangeOne.getOwnedAssignment();
} }
@Override @Override
public int hashCode() { public int hashCode() {
return this.assignmentOne.hashCode() + this.assignmentTwo.hashCode(); return this.getAssignmentOne().hashCode() + this.getAssignmentTwo().hashCode();
} }
@Override @Override
@ -44,6 +53,6 @@ public class MatchData {
MatchData c = (MatchData) o; MatchData c = (MatchData) o;
// Compare the data members and return accordingly // 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()));
} }
} }

View File

@ -1,5 +1,6 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -7,8 +8,10 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.models.MatchData; import com.plannaplan.models.MatchData;
import com.plannaplan.repositories.ExchangeRepository; import com.plannaplan.repositories.ExchangeRepository;
@ -21,6 +24,12 @@ public class ExchangeService {
@Autowired @Autowired
private ExchangeRepository repo; private ExchangeRepository repo;
@Autowired
private AssignmentService assignmentService;
@Autowired
private UserService userService;
/** /**
* @param exchange Instance to save in database * @param exchange Instance to save in database
* @return Exchange Instance contains database id * @return Exchange Instance contains database id
@ -62,14 +71,47 @@ public class ExchangeService {
} }
public void performExchange() { 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(){ public Set<MatchData> getMatches(){
final List<MatchData> matches = this.repo.getMatches().stream().map(m -> { final List<MatchData> matches = this.repo.getMatches().stream().map(m -> {
final Exchange exchangeOne = (Exchange) m[0]; final Exchange exchangeOne = (Exchange) m[0];
final Exchange exchangeTwo = (Exchange) m[1]; final Exchange exchangeTwo = (Exchange) m[1];
return new MatchData(exchangeOne.getOwnedAssignment(), exchangeTwo.getOwnedAssignment()); return new MatchData(exchangeOne, exchangeTwo);
}).collect(Collectors.toList()); }).collect(Collectors.toList());
final Set<MatchData> filterMatches = new HashSet<>(matches); final Set<MatchData> filterMatches = new HashSet<>(matches);