简体   繁体   中英

how to create sign-in and sign-up form & create a test for it backend in Java and frontend js?

I would like to learn, how can I make a project that has this function. (sign-in and sign-up) These are the classes that I have so far.

this is the entity i have also the class User

@Entity(name = "Users")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id" ,nullable = false)
    private int id;

    @Column(name = "first_name" )
    private String firstname;

    @Column(name = "last_name" )
    private String lastname;

    @Column(name = "Email" )
    private String email;

    protected UserEntity() {

    }

    public UserEntity( String firstname, String lastname, String email) {

        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;


    }

this is the Service class where the methods should be

@Service
public class UserService {
    private final UserRepository usersRepository;

    public UserService(UserRepository usersRepository) {
        this.usersRepository = usersRepository;
    }


    public List<User> findAll (){
        List<UserEntity> users = usersRepository.findAll();
        return users.stream().map(this::entityToUser).collect(Collectors.toList());
    }


    public User findUserbyId(int id){
        var userentity = usersRepository.findById(id);
        return userentity.map(this::entityToUser).orElse(null);
    }

    public User update(int id , UserCreateRequest request){
        var userEntityOptional = usersRepository.findById(id);
        if(userEntityOptional.isEmpty()){
            return null;
        }
       var userEntity = userEntityOptional.get();
        userEntity.setFirstname(request.getFirstname());
        userEntity.setLastname(request.getLastname());
        userEntity.setEmail(request.getEmail());
        usersRepository.save(userEntity);

        return entityToUser(userEntity);
    }

    public boolean deleteById(int id){
        if (!usersRepository.existsById(id)){
            return false;
        }
        usersRepository.existsById(id);
        return true;
    }

    public User create(UserCreateRequest request){
         var userEntity = new UserEntity(request.getFirstname(),request.getLastname(),request.getEmail());
         userEntity = usersRepository.save(userEntity);
        return entityToUser(userEntity);

    }



    public User entityToUser(UserEntity userEntity){
      return new User (
                userEntity.getId(),
                userEntity.getFirstname(),
                userEntity.getLastname(),
                userEntity.getEmail());

    }


}

here is the User Controller

@RestController
public class UserRestController {

    private final UserService userService;

    public UserRestController(UserService userService) {
        this.userService = userService;
    }
    @GetMapping(path = "/api/v1/users")
    public ResponseEntity<List<User>> fetchUsers() {
        return ResponseEntity.ok(userService.findAll()) ;
}

I would like to know what do I need exactly to get this function right in an optimal way what are the necessary steps?

@Service
public class UserService {
     //make this Autowired
    @Autowired
    private  UserRepository usersRepository;

    


    public List<User> findAll (){
        List<UserEntity> users = usersRepository.findAll();
        return users.stream().map(this::entityToUser).collect(Collectors.toList());
    }


    public User findUserbyId(int id){
        var userentity = usersRepository.findById(id);
        return userentity.map(this::entityToUser).orElse(null);
    }

    public User update(int id , UserCreateRequest request){
        var userEntityOptional = usersRepository.findById(id);
        if(userEntityOptional.isEmpty()){
            return null;
        }
       var userEntity = userEntityOptional.get();
        userEntity.setFirstname(request.getFirstname());
        userEntity.setLastname(request.getLastname());
        userEntity.setEmail(request.getEmail());
        usersRepository.save(userEntity);

        return entityToUser(userEntity);
    }

    public boolean deleteById(int id){
        if (!usersRepository.existsById(id)){
            return false;
        }
        usersRepository.existsById(id);
        return true;
    }

    public User create(UserCreateRequest request){
         var userEntity = new UserEntity(request.getFirstname(),request.getLastname(),request.getEmail());
         userEntity = usersRepository.save(userEntity);
        return entityToUser(userEntity);

    }



    public User entityToUser(UserEntity userEntity){
      return new User (
                userEntity.getId(),
                userEntity.getFirstname(),
                userEntity.getLastname(),
                userEntity.getEmail());

    }


}

here is the User Controller

@RestController
public class UserRestController {
   //make this Autowired
    @Autowired
    private UserService userService;

    
    @GetMapping(path = "/api/v1/users")
    public ResponseEntity<List<User>> fetchUsers() {
        return ResponseEntity.ok(userService.findAll()) ;
}

Check this to know more about @Autowired https://www.baeldung.com/spring-autowire

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM