Changeset 7edede9 for src


Ignore:
Timestamp:
01/06/21 18:18:24 (4 years ago)
Author:
i-ina <76742075+i-ina@…>
Branches:
master
Children:
504c3ef
Parents:
a222a43
Message:

added user register

Location:
src/main/java/it/finki/tinki
Files:
3 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • src/main/java/it/finki/tinki/controller/LoginController.java

    ra222a43 r7edede9  
    1111@CrossOrigin(origins = "http://localhost:3000")
    1212@RequestMapping("/api")
    13 public class TestController {
     13public class LoginController {
    1414
    1515    AccountService accountService;
    1616
    17     public TestController(AccountService accountService) {
     17    public LoginController(AccountService accountService) {
    1818        this.accountService = accountService;
    1919    }
  • src/main/java/it/finki/tinki/repository/UserRepository.java

    ra222a43 r7edede9  
    55import org.springframework.stereotype.Repository;
    66
     7import java.util.Optional;
     8
    79@Repository
    810public interface UserRepository extends JpaRepository<User, Long> {
    911    User findByEmailAndPassword(String email, String password);
     12    Optional<User> findByEmail(String email);
    1013}
  • src/main/java/it/finki/tinki/service/impl/AccountServiceImpl.java

    ra222a43 r7edede9  
    11package it.finki.tinki.service.impl;
    22
     3import it.finki.tinki.model.Address;
     4import it.finki.tinki.model.Skill;
    35import it.finki.tinki.model.Users.Account;
    46import it.finki.tinki.model.Users.Company;
     
    68import it.finki.tinki.model.Users.User;
    79import it.finki.tinki.model.enumerator.AccountType;
     10import it.finki.tinki.model.exception.InvalidArgumentsException;
     11import it.finki.tinki.model.exception.UserExistsException;
     12import it.finki.tinki.repository.AddressRepository;
    813import it.finki.tinki.repository.CompanyRepository;
    914import it.finki.tinki.repository.TeamRepository;
     
    1318
    1419import java.util.HashMap;
     20import java.util.List;
    1521import java.util.Map;
    1622
     
    1824public class AccountServiceImpl implements AccountService {
    1925
     26    AddressRepository addressRepository;
    2027    UserRepository userRepository;
    2128    TeamRepository teamRepository;
    2229    CompanyRepository companyRepository;
    2330
    24     public AccountServiceImpl(UserRepository userRepository, TeamRepository teamRepository, CompanyRepository companyRepository) {
     31    public AccountServiceImpl(AddressRepository addressRepository, UserRepository userRepository, TeamRepository teamRepository, CompanyRepository companyRepository) {
     32        this.addressRepository = addressRepository;
    2533        this.userRepository = userRepository;
    2634        this.teamRepository = teamRepository;
     
    3341        if(type.equals(AccountType.USER)){
    3442            User u1 = userRepository.findByEmailAndPassword(email, password);
    35             if(u1!=null){
    36                 return u1;
    37             }
     43            return u1;
    3844        }
    3945        else if(type.equals(AccountType.TEAM)){
    4046            Team t1 = teamRepository.findByEmailAndPassword(email, password);
    41             if(t1!=null){
    42                 return t1;
    43             }
     47            return t1;
    4448        }
    4549        else if(type.equals(AccountType.COMPANY)){
    4650            Company c1 = companyRepository.findByEmailAndPassword(email, password);
    47             if(c1!=null){
    48                 return c1;
    49             }
     51            return c1;
    5052        }
    5153
    5254        return null;
    5355    }
     56
     57    public Account registerUser(String email, String password, String name, String surname, List<Skill> retainedSkills, List<Skill> skillsToLearn){
     58        if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty() || surname==null || surname.isEmpty()){
     59            throw new InvalidArgumentsException();
     60        }
     61
     62        if(this.userRepository.findByEmail(email).isPresent()){
     63            throw new UserExistsException();
     64        }
     65
     66        User u = new User(email, password, name, AccountType.USER, surname, retainedSkills, skillsToLearn);
     67        return this.userRepository.save(u);
     68    }
    5469}
Note: See TracChangeset for help on using the changeset viewer.