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);
}