- Timestamp:
- 09/07/22 00:51:50 (2 years ago)
- Branches:
- master
- Children:
- 527b93f
- Parents:
- dbd4834
- 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 7 7 8 8 import java.util.ArrayList; 9 import java.util.Comparator; 9 10 import java.util.List; 11 import java.util.stream.Collectors; 10 12 11 13 @Service … … 24 26 throw new IllegalStateException("Phone with id "+phoneId+" does not exist"); 25 27 26 return phoneRepository.findById(phoneId).get().getPhoneOffers(); 28 return phoneRepository.findById(phoneId).get().getPhoneOffers() 29 .stream().sorted(Comparator.comparing(PhoneOffer::getPrice)).collect(Collectors.toList()); 27 30 } 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 28 52 } -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneService.java
rdbd4834 rf25d07e 4 4 import finki.it.phoneluxbackend.entities.PhoneOffer; 5 5 import finki.it.phoneluxbackend.repositories.PhoneRepository; 6 import org.springframework.data.domain.PageRequest; 6 7 import org.springframework.data.domain.Sort; 7 8 import org.springframework.stereotype.Service; 8 9 10 import java.util.Comparator; 9 11 import java.util.List; 12 import java.util.stream.Collectors; 10 13 11 14 @Service … … 17 20 } 18 21 22 23 // TODO: insert logic to filter 19 24 public List<Phone> getPhones(){ 20 25 return phoneRepository.findAll(); 21 26 } 22 27 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 } 23 40 } -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/RegistrationService.java
rdbd4834 rf25d07e 1 1 package finki.it.phoneluxbackend.services; 2 2 3 import com.fasterxml.jackson.core.JsonParser; 4 import com.fasterxml.jackson.core.io.JsonStringEncoder; 5 import com.fasterxml.jackson.databind.ObjectMapper; 6 import com.fasterxml.jackson.databind.util.JSONPObject; 3 7 import finki.it.phoneluxbackend.data.RegistrationRequest; 4 8 import finki.it.phoneluxbackend.data.UserRole; … … 8 12 import finki.it.phoneluxbackend.security.email.EmailValidator; 9 13 import lombok.AllArgsConstructor; 14 import org.apache.coyote.Response; 15 import org.apache.tomcat.util.json.JSONParser; 16 import org.springframework.http.HttpStatus; 17 import org.springframework.http.ResponseEntity; 10 18 import org.springframework.stereotype.Service; 11 19 import org.springframework.transaction.annotation.Transactional; 12 20 13 21 import java.time.LocalDateTime; 22 import java.util.HashMap; 14 23 15 24 @Service … … 22 31 23 32 24 public Stringregister(RegistrationRequest request) {33 public ResponseEntity<Object> register(RegistrationRequest request) { 25 34 boolean isValidEmail = emailValidator.test(request.getEmail()); 26 35 27 // validacija za mejl na frontend ?36 // mail is validated on frontend already 28 37 if (!isValidEmail) 29 38 throw new IllegalStateException("Email"+request.getEmail()+" not valid!"); 30 39 31 String token= userService.signUpUser(40 ResponseEntity response = userService.signUpUser( 32 41 new User(request.getFirstName(), 33 42 request.getLastName(), … … 36 45 UserRole.USER)); 37 46 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]; 39 54 emailSender.send(request.getEmail(), buildEmail(request.getFirstName(),link)); 40 return token; 55 56 return response; 41 57 } 42 58 … … 113 129 @Transactional 114 130 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!"; 117 139 118 140 if(confirmationToken.getConfirmedAt() != null) 119 throw new IllegalStateException("Email already confirmed!");141 return "Email already confirmed!"; 120 142 121 143 LocalDateTime expiresAt = confirmationToken.getExpiresAt(); 122 144 123 145 if (expiresAt.isBefore(LocalDateTime.now())){ 124 throw new IllegalStateException("Token expired");146 return "Token expired"; 125 147 } 126 148 -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/UserService.java
rdbd4834 rf25d07e 1 1 package finki.it.phoneluxbackend.services; 2 2 3 3 4 import finki.it.phoneluxbackend.entities.User; … … 5 6 import finki.it.phoneluxbackend.entities.ConfirmationToken; 6 7 import lombok.AllArgsConstructor; 8 9 import org.springframework.http.ResponseEntity; 7 10 import org.springframework.security.core.userdetails.UserDetails; 8 11 import org.springframework.security.core.userdetails.UserDetailsService; … … 12 15 13 16 import java.time.LocalDateTime; 17 14 18 import java.util.UUID; 15 19 … … 28 32 } 29 33 30 public StringsignUpUser(User user)34 public ResponseEntity<Object> signUpUser(User user) 31 35 { 32 boolean userExists = 36 boolean userExists = userRepository.findByEmail(user.getEmail()).isPresent(); 33 37 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 } 36 47 } 37 48 … … 49 60 confirmationTokenService.saveConfirmationToken(confirmationToken); 50 61 51 return token;62 return ResponseEntity.ok().body("token:"+token); 52 63 } 53 64
Note:
See TracChangeset
for help on using the changeset viewer.