Java电子邮件:如何在保存和返回实体之前发送电子邮件?

时间:2019-02-27 09:53:14

标签: java spring-boot

我正在创建一个帐户,然后发送带有密码的电子邮件。现在,它首先发送电子邮件,然后保存用户。如何更改订单,因为退货后我无法使用电子邮件功能。

我该如何解决?我应该为这两个功能创建另一个功能吗?

public WorkDetail setWorkerData(PatchWorkerRequest request, User user, WorkDetail workDetail) {

        if (request.getName() != null) {
            user.setName(request.getName());
        }

        if (request.getIdCode() != null) {
            user.setIdCode(request.getIdCode());
        }

        if (request.getEmail() != null) {
            user.setEmail(request.getEmail());
        }

        if (request.getPhone() != null) {
            user.setPhone(request.getPhone());
        }

        if (request.getStatus() != null) {
            user.setStatus(request.getStatus());
        }

        // Work details

        if (request.getJobRelation() != null) {
            workDetail.setJobRelation(request.getJobRelation());
        }

        if (request.getSalary() != null) {
            workDetail.setSalary(request.getSalary());
        }

        if (request.getDistricts() != null) {
            workDetail.setDistricts(request.getDistricts());
        }

        if (request.getCompetences() != null) {
            workDetail.setCompetences(request.getCompetences());
        }

        workDetail = workDetailRepository.save(workDetail);
        user.setWorkDetail(workDetail);
        workDetail.setUser(user);


        return workDetailRepository.save(workDetail);
    }

    public WorkDetail createWorker(PatchWorkerRequest request) {
        WorkDetail workDetail = new WorkDetail();
        User user = new User();
        String generatedPassword = userService.generatePassword(8);
        user.setPassword(passwordEncoder.encode(generatedPassword));
        emailService.sendMail("SYDA", new String[]{request.getEmail()},
                "SYDA: teile loodi kasutaja",
                "Teile loodi SYDA süsteemi kasutaja. Teie parool on: " + generatedPassword +
                        "\nSisse saate logida kasutades emaili: " + request.getEmail());
        return this.setWorkerData(request, user, workDetail);
    }

1 个答案:

答案 0 :(得分:1)

代替

emailService.sendMail("SYDA", new String[]{request.getEmail()},
            "SYDA: teile loodi kasutaja",
            "Teile loodi SYDA süsteemi kasutaja. Teie parool on: " + generatedPassword +
                    "\nSisse saate logida kasutades emaili: " + request.getEmail());
return this.setWorkerData(request, user, workDetail);

WorkDetail workDetailSaved = this.setWorkerData(request, user, workDetail);
emailService.sendMail("SYDA", new String[]{request.getEmail()},
            "SYDA: teile loodi kasutaja",
            "Teile loodi SYDA süsteemi kasutaja. Teie parool on: " + generatedPassword +
                    "\nSisse saate logida kasutades emaili: " + request.getEmail());
return workDetailSaved;

(由Stultuske提议)


顺便说一句:我将完全重写“ setWorkerData(request,user,workDetail)”方法,并将其重命名为“ buildWorkDetail(request,user)”,该方法仅创建一个WorkDetail实例。另外,我将从那里删除“保存”部分,并在创建WorkDetail之后执行。

示例:

public WorkDetail createWorker(PatchWorkerRequest request) {
    User user = buildUser(request); // builds a new User-instance from the request
    String generatedPassword = userService.generatePassword(8);
    user.setPassword(passwordEncoder.encode(generatedPassword));

    WorkDetail workDetail = buildWorkDetail(request, user); // builds a new WorkDetail-instance from the request
    workDetail.setUser(user);
    workDetailRepository.save(workDetail);
    user.setWorkDetail(workDetail);

    emailService.sendMail("SYDA", new String[]{request.getEmail()},
                "...",
                "...);
    return workDetail;
}