class AffiliationServiceImpl extends BaseTransactionalService implements AffiliationService {

    @Override
    @Transactional
    public ServiceResult<Void> updateUserAffiliations(long userId, AffiliationListResource affiliations) {
        return find(userRepository.findById(userId), notFoundError(User.class, userId)).andOnSuccess(user -> {
            List<Affiliation> targetAffiliations = user.getAffiliations();
            targetAffiliations.clear();
            affiliationMapper.mapToDomain(affiliations.getAffiliationResourceList())
                    .forEach(affiliation -> {
                        affiliation.setUser(user);
                        targetAffiliations.add(affiliation);
                    });
            userRepository.save(user);
            return serviceSuccess();
        });
    }

}