-1

I am trying out some new design patterns in java but I am getting confused as to why mine will not work.

I am aiming to pass a user account into a data transfer object which can then be used to sign up a new user by checking if they exist or not and if not using the getters and setters to make a user and save it to a mongoDB database with a mapper.

It all seems to be going well until I get a unresolved method call on the setpassword in my service implementation and in my mapper and I do not know why. I am getting my setFirstname by extending a base user in my useraccount which has a firstname field on it.

Any help would be great :)

package com.datingapp.model.user;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "userAccounts")
public class UserAccount extends User {
    @Id
    private String id;
    private String email;
    private String password;
    private String lastname;
    private String phoneNumber;

    public UserAccount() {}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}
package com.datingapp.dto.model.user;

import com.datingapp.model.user.User;
import com.datingapp.model.user.UserAccount;

public class UserAccountDto extends User {
    private String email;
    private String lastname;
    private String password;
    private String phoneNumber;

    public UserAccountDto() {
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

}
package com.datingapp.dto.mapper;

import com.datingapp.dto.model.user.UserAccountDto;
import com.datingapp.model.user.UserAccount;
import org.springframework.stereotype.Component;

@Component
public class UserAccountMapper {

    public static UserAccountDto toUserAccountDto(UserAccount userAccount) {
        return new UserAccountDto()
                .setEmail(userAccount.getEmail())
                .setFirstname(userAccount.getFirstname())
                .setLastname(userAccount.getLastname())
                .setPassword(userAccount.getPassword())
                .setPhoneNumber(userAccount.getPhoneNumber());
    }
}
package com.datingapp.service.user;

import com.datingapp.dto.mapper.UserAccountMapper;
import com.datingapp.dto.model.user.UserAccountDto;
import com.datingapp.model.user.UserAccount;
import com.datingapp.repository.user.UserAccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class UserAccountImpl implements UserAccountService {
    private UserAccountRepository userAccountRepository;
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    public UserAccountImpl(UserAccountRepository userAccountRepository, BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.userAccountRepository = userAccountRepository;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    @Override
    public UserAccountDto signup(UserAccountDto userDto) {
        UserAccount user = userAccountRepository.findByEmail(userDto.getEmail());
        if (user == null) {
            user = new UserAccount()
                    .setEmail(userDto.getEmail())
                    .setPassword(bCryptPasswordEncoder.encode(userDto.getPassword()))
                    .setFirstName(userDto.getFirstname())
                    .setLastName(userDto.getFirstname())
                    .setMobileNumber(userDto.getPhoneNumber());
            return UserAccountMapper.toUserAccountDto(userAccountRepository.save(user));
        }
    }
}
package com.datingapp.service.user;

import com.datingapp.dto.model.user.UserAccountDto;
import com.datingapp.model.user.UserAccount;

public interface UserAccountService {
    UserAccountDto signup(UserAccountDto userAccountDto);
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Oliver Darby
  • 454
  • 1
  • 6
  • 15

2 Answers2

3

Your setters are void methods, that is to say they return nothing.

You will have your expected result by making a setter like :

public UserAccountDto setEmail(String email) {
     this.email = email;

     return this ;
}

You have to add return this in your setters.

Pythagus
  • 127
  • 1
  • 6
  • So in my dto have a type of userAccountDto and return this for each setter and in my userAccount have a type of userAccount and return this for each one too? – Oliver Darby Jan 18 '21 at 19:20
  • 1
    Yes, you need to. I suggest you putting these setters in the `User` class to define them once and for all. All your setters should return `this` – Pythagus Jan 18 '21 at 19:25
0

I know Pythagus already answered your question, but let me give you a tip. Try to use Lombok. Lombok is a java lib which helps you to avoid boilerplate code. For example, with the @Data annotation, you are telling Lombok to create, under the hood, all your getters and setters. It will help you a lot.

Lais Bento
  • 13
  • 4