Ignore:
Timestamp:
09/07/22 00:51:50 (2 years ago)
Author:
Marko <Marko@…>
Branches:
master
Children:
527b93f
Parents:
dbd4834
Message:

Edited registration and login services

Location:
phonelux-backend/src/main/java/finki/it/phoneluxbackend/services
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneOfferService.java

    rdbd4834 rf25d07e  
    77
    88import java.util.ArrayList;
     9import java.util.Comparator;
    910import java.util.List;
     11import java.util.stream.Collectors;
    1012
    1113@Service
     
    2426            throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
    2527
    26         return phoneRepository.findById(phoneId).get().getPhoneOffers();
     28        return phoneRepository.findById(phoneId).get().getPhoneOffers()
     29                .stream().sorted(Comparator.comparing(PhoneOffer::getPrice)).collect(Collectors.toList());
    2730    }
     31
     32    public List<String> getShops() {
     33        return phoneOfferRepository.findAll().stream()
     34                .map(PhoneOffer::getOffer_shop)
     35                .distinct()
     36                .collect(Collectors.toList());
     37    }
     38
     39
     40    public int getLowestPrice() {
     41        return phoneOfferRepository.findAll()
     42                .stream().sorted(Comparator.comparing(PhoneOffer::getPrice))
     43                .collect(Collectors.toList()).get(0).getPrice();
     44    }
     45
     46    public int getHighestPrice() {
     47        return phoneOfferRepository.findAll()
     48                .stream().sorted(Comparator.comparing(PhoneOffer::getPrice).reversed())
     49                .collect(Collectors.toList()).get(0).getPrice();
     50    }
     51
    2852}
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneService.java

    rdbd4834 rf25d07e  
    44import finki.it.phoneluxbackend.entities.PhoneOffer;
    55import finki.it.phoneluxbackend.repositories.PhoneRepository;
     6import org.springframework.data.domain.PageRequest;
    67import org.springframework.data.domain.Sort;
    78import org.springframework.stereotype.Service;
    89
     10import java.util.Comparator;
    911import java.util.List;
     12import java.util.stream.Collectors;
    1013
    1114@Service
     
    1720    }
    1821
     22
     23    // TODO: insert logic to filter
    1924    public List<Phone> getPhones(){
    2025        return phoneRepository.findAll();
    2126    }
    2227
     28    public List<String> getBrands(){
     29        return phoneRepository.findAll().stream()
     30                .map(Phone::getBrand).distinct()
     31                .collect(Collectors.toList());
     32    }
     33
     34    public Phone getPhoneById(Long phoneId) {
     35        boolean exists = phoneRepository.existsById(phoneId);
     36        if(!exists)
     37            throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
     38        return phoneRepository.findById(phoneId).get();
     39    }
    2340}
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/RegistrationService.java

    rdbd4834 rf25d07e  
    11package finki.it.phoneluxbackend.services;
    22
     3import com.fasterxml.jackson.core.JsonParser;
     4import com.fasterxml.jackson.core.io.JsonStringEncoder;
     5import com.fasterxml.jackson.databind.ObjectMapper;
     6import com.fasterxml.jackson.databind.util.JSONPObject;
    37import finki.it.phoneluxbackend.data.RegistrationRequest;
    48import finki.it.phoneluxbackend.data.UserRole;
     
    812import finki.it.phoneluxbackend.security.email.EmailValidator;
    913import lombok.AllArgsConstructor;
     14import org.apache.coyote.Response;
     15import org.apache.tomcat.util.json.JSONParser;
     16import org.springframework.http.HttpStatus;
     17import org.springframework.http.ResponseEntity;
    1018import org.springframework.stereotype.Service;
    1119import org.springframework.transaction.annotation.Transactional;
    1220
    1321import java.time.LocalDateTime;
     22import java.util.HashMap;
    1423
    1524@Service
     
    2231
    2332
    24     public String register(RegistrationRequest request) {
     33    public ResponseEntity<Object> register(RegistrationRequest request) {
    2534        boolean isValidEmail = emailValidator.test(request.getEmail());
    2635
    27         // validacija za mejl na frontend ?
     36        // mail is validated on frontend already
    2837        if (!isValidEmail)
    2938            throw new IllegalStateException("Email"+request.getEmail()+" not valid!");
    3039
    31         String token = userService.signUpUser(
     40        ResponseEntity response = userService.signUpUser(
    3241                new User(request.getFirstName(),
    3342                        request.getLastName(),
     
    3645                        UserRole.USER));
    3746
    38         String link = "http://localhost:8080/registration/confirm?token="+token;
     47        if (response.getStatusCode() == HttpStatus.BAD_REQUEST)
     48        {
     49            return response;
     50        }
     51
     52        String link = "http://localhost:8080/registration/confirm?token="+response.getBody()
     53                .toString().split(":")[1];
    3954        emailSender.send(request.getEmail(), buildEmail(request.getFirstName(),link));
    40         return token;
     55
     56        return response;
    4157    }
    4258
     
    113129    @Transactional
    114130    public String confirmToken(String token) {
    115         ConfirmationToken confirmationToken = confirmationTokenService.getToken(token)
    116                 .orElseThrow(() -> new IllegalStateException("Token not found!"));
     131        boolean confirmationTokenExists = confirmationTokenService.getToken(token).isPresent();
     132
     133        ConfirmationToken confirmationToken;
     134
     135        if(confirmationTokenExists)
     136            confirmationToken = confirmationTokenService.getToken(token).get();
     137        else
     138            return "Token not found!";
    117139
    118140        if(confirmationToken.getConfirmedAt() != null)
    119             throw new IllegalStateException("Email already confirmed!");
     141            return "Email already confirmed!";
    120142
    121143        LocalDateTime expiresAt = confirmationToken.getExpiresAt();
    122144
    123145        if (expiresAt.isBefore(LocalDateTime.now())){
    124             throw new IllegalStateException("Token expired");
     146            return "Token expired";
    125147        }
    126148
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/UserService.java

    rdbd4834 rf25d07e  
    11package finki.it.phoneluxbackend.services;
     2
    23
    34import finki.it.phoneluxbackend.entities.User;
     
    56import finki.it.phoneluxbackend.entities.ConfirmationToken;
    67import lombok.AllArgsConstructor;
     8
     9import org.springframework.http.ResponseEntity;
    710import org.springframework.security.core.userdetails.UserDetails;
    811import org.springframework.security.core.userdetails.UserDetailsService;
     
    1215
    1316import java.time.LocalDateTime;
     17
    1418import java.util.UUID;
    1519
     
    2832    }
    2933
    30     public String signUpUser(User user)
     34    public ResponseEntity<Object> signUpUser(User user)
    3135    {
    32        boolean userExists =  userRepository.findByEmail(user.getEmail()).isPresent();
     36       boolean userExists = userRepository.findByEmail(user.getEmail()).isPresent();
    3337
    34        if (userExists && user.getEnabled()){
    35            throw new IllegalStateException("Email "+user.getEmail()+" already taken!");
     38
     39       if (userExists){
     40           User userToRegister =  userRepository.findByEmail(user.getEmail()).get();
     41           if(userToRegister.getEnabled()) {
     42               return ResponseEntity.badRequest().body("Error: Email "+user.getEmail()+" already taken!");
     43           }
     44           else {
     45               return ResponseEntity.badRequest().body("Email "+user.getEmail()+" not activated!" );
     46           }
    3647       }
    3748
     
    4960        confirmationTokenService.saveConfirmationToken(confirmationToken);
    5061
    51         return token;
     62        return ResponseEntity.ok().body("token:"+token);
    5263    }
    5364
Note: See TracChangeset for help on using the changeset viewer.