81 lines
2.3 KiB
Java
Executable File
81 lines
2.3 KiB
Java
Executable File
package com.plannaplan.services;
|
|
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
import com.plannaplan.entities.Commision;
|
|
import com.plannaplan.entities.User;
|
|
import com.plannaplan.repositories.AssignmentRepository;
|
|
import com.plannaplan.repositories.CommisionRepository;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
/**
|
|
* Service of CommisionService which can save commision, get user's commisions,
|
|
* get newest user's commision, get ammount of commisions.
|
|
*/
|
|
|
|
@Service
|
|
public class CommisionService {
|
|
@Autowired
|
|
private CommisionRepository repo;
|
|
@Autowired
|
|
private AssignmentRepository aRepository;
|
|
|
|
public CommisionService() {
|
|
}
|
|
|
|
/**
|
|
* save to database commision. It also checks for missing assignments from
|
|
* previous commision (you can not get rid of accepted assignment)
|
|
*
|
|
* @param commision to save to db
|
|
* @return Commision instance with id from database
|
|
*/
|
|
public Commision save(Commision commision) {
|
|
Optional<Commision> lastCommision = this.getNewestCommision(commision.getCommisionOwner());
|
|
if (lastCommision.isPresent()) {
|
|
final Commision lastCom = lastCommision.get();
|
|
lastCom.getAssignments().forEach(assignment -> {
|
|
assignment.setPastAssignment(true);
|
|
this.aRepository.save(assignment);
|
|
});
|
|
|
|
}
|
|
this.repo.save(commision);
|
|
return commision;
|
|
|
|
}
|
|
|
|
/**
|
|
* gets user commisions
|
|
*
|
|
* @param user owner of commisions
|
|
* @return list of user commisions
|
|
*/
|
|
public List<Commision> getUsersCommisions(User user) {
|
|
return this.repo.getUsers(user.getId());
|
|
}
|
|
|
|
/**
|
|
* get newest commision ov given user
|
|
*
|
|
* @param user owener of commision we attemp to get
|
|
* @return optional if commition was found
|
|
*/
|
|
public Optional<Commision> getNewestCommision(User user) {
|
|
return this.repo.getNewestCommision(user.getId()).stream().findFirst();
|
|
}
|
|
|
|
/**
|
|
* get ammpounts of commisions
|
|
*
|
|
* @return long - ammounts of commisions (all even from history, not only
|
|
* cutrrent one)
|
|
*/
|
|
public long getCommisionsAmmount() {
|
|
return this.repo.count();
|
|
}
|
|
}
|