package org.pwd.domain.processing;

import org.pwd.domain.audit.Audit;
import org.pwd.domain.audit.WebsiteAudit;
import org.pwd.domain.audit.WebsiteAuditReport;
import org.pwd.domain.audit.WebsiteAuditRepository;
import org.pwd.domain.websites.Website;
import org.pwd.interfaces.analysis.AnalysisRestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * Does audit for one website
 *
 * @author bartosz.walacik
 */
@Service
class WebsiteAuditor {
    private final AnalysisRestClient analysisRestClient;
    private final WebsiteAuditRepository websiteAuditRepository;

    @Autowired
    WebsiteAuditor(AnalysisRestClient analysisRestClient, WebsiteAuditRepository websiteAuditRepository) {
        this.analysisRestClient = analysisRestClient;
        this.websiteAuditRepository = websiteAuditRepository;
    }

    WebsiteAudit auditWebsite(Audit audit, Website website) {

        Optional<WebsiteAuditReport> auditReport = analysisRestClient.getAnalysis(website.getUrl());
        double prevScore = websiteAuditRepository.getPreviousScore(website.getId());

        WebsiteAudit websiteAudit = auditReport
                .map(websiteAuditReport -> audit.createWebsiteAudit(website, websiteAuditReport,prevScore))
                .orElseThrow(AnalysisNotCompleteException::new);


        return websiteAuditRepository.save(websiteAudit);
    }
}

@@ -1080,17 +1080,17 @@
         
-d
+D
 ouble pr
@@ -1156,16 +1156,95 @@
 etId());
+%0A        %0A        if (prevScore == null) %7B%0A            prevScore = 0;%0A        %7D
 %0A%0A      

Update WebsiteAuditor.java