Changeset f25d07e for phonelux-backend/src/main
- 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
- Files:
-
- 2 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/PhoneController.java
rdbd4834 rf25d07e 3 3 import finki.it.phoneluxbackend.entities.Phone; 4 4 import finki.it.phoneluxbackend.entities.PhoneOffer; 5 import finki.it.phoneluxbackend.services.PhoneOfferService; 5 6 import finki.it.phoneluxbackend.services.PhoneService; 7 import lombok.AllArgsConstructor; 6 8 import org.springframework.beans.factory.annotation.Autowired; 7 9 import org.springframework.web.bind.annotation.*; 8 10 11 import java.util.Comparator; 9 12 import java.util.List; 13 import java.util.stream.Collectors; 10 14 11 15 @RestController 16 @AllArgsConstructor 12 17 @RequestMapping(path = "/") 13 18 public class PhoneController { 14 19 private final PhoneService phoneService; 20 private final PhoneOfferService phoneOfferService; 15 21 16 @Autowired 17 public PhoneController(PhoneService phoneService) { 18 this.phoneService = phoneService; 22 // handle request parameters for filtering phones 23 @GetMapping(path = "/phones") 24 public List<Phone> getPhones(){ 25 return phoneService.getPhones().stream() 26 .sorted(Comparator.comparing(Phone::getTotal_offers).reversed()) 27 .collect(Collectors.toList()); 28 } 29 30 @GetMapping(path = "/phones/{phoneId}") 31 public Phone getPhoneById(@PathVariable("phoneId") Long phoneId) 32 { 33 return phoneService.getPhoneById(phoneId); 34 } 35 36 @GetMapping(path = "/brands") 37 public List<String> getBrands(){ 38 return phoneService.getBrands(); 39 } 40 41 @GetMapping(path = "/shops") 42 public List<String> getShops(){ 43 return phoneOfferService.getShops(); 44 } 45 46 @GetMapping(path = "/lowestPrice") 47 public int getLowestPrice() 48 { 49 return phoneOfferService.getLowestPrice(); 50 } 51 52 @GetMapping(path = "/highestPrice") 53 public int getHighestPrice() 54 { 55 return phoneOfferService.getHighestPrice(); 19 56 } 20 57 21 58 22 // handle request parameters for filtering phones23 @GetMapping24 public List<Phone> getPhones(){25 return phoneService.getPhones();26 }27 28 59 } -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/PhoneOfferController.java
rdbd4834 rf25d07e 1 1 package finki.it.phoneluxbackend.controllers; 2 2 3 import finki.it.phoneluxbackend.entities.Phone; 3 4 import finki.it.phoneluxbackend.entities.PhoneOffer; 4 5 import finki.it.phoneluxbackend.services.PhoneOfferService; 6 import finki.it.phoneluxbackend.services.PhoneService; 7 import lombok.AllArgsConstructor; 5 8 import org.springframework.beans.factory.annotation.Autowired; 6 9 import org.springframework.web.bind.annotation.GetMapping; … … 12 15 13 16 @RestController 14 @RequestMapping(path = "/phone/{phoneId}") 17 @AllArgsConstructor 18 @RequestMapping(path = "/phones/offers/{phoneId}") 15 19 public class PhoneOfferController { 16 20 private final PhoneOfferService phoneOfferService; 17 18 @Autowired 19 public PhoneOfferController(PhoneOfferService phoneOfferService) { 20 this.phoneOfferService = phoneOfferService; 21 } 21 private final PhoneService phoneService; 22 22 23 23 @GetMapping … … 25 25 return phoneOfferService.getPhoneOffersForPhone(phoneId); 26 26 } 27 27 28 } -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/RegistrationController.java
rdbd4834 rf25d07e 4 4 import finki.it.phoneluxbackend.services.RegistrationService; 5 5 import lombok.AllArgsConstructor; 6 import org.springframework.http.ResponseEntity; 6 7 import org.springframework.web.bind.annotation.*; 7 8 … … 13 14 14 15 @PostMapping 15 public StringRegisterRequest(@RequestBody RegistrationRequest request)16 public ResponseEntity<Object> RegisterRequest(@RequestBody RegistrationRequest request) 16 17 { 17 18 return registrationService.register(request); -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/entities/ConfirmationToken.java
rdbd4834 rf25d07e 13 13 @Setter 14 14 @NoArgsConstructor 15 @Entity(name = "confirmation_tokens") 15 @Entity(name = "ConfirmationToken") 16 @Table(name = "confirmation_tokens") 16 17 public class ConfirmationToken { 17 18 -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/entities/Phone.java
rdbd4834 rf25d07e 28 28 private String image_url; 29 29 30 @Column(name = "total_offers") 31 private Integer total_offers; 32 33 @Column(name = "lowest_price") 34 private Integer lowestPrice; 35 30 36 @OneToMany(fetch = FetchType.LAZY, mappedBy = "phone") 31 37 @JsonIgnore -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/entities/PhoneOffer.java
rdbd4834 rf25d07e 5 5 6 6 import javax.persistence.*; 7 import java.util.ArrayList; 7 8 import java.util.Date; 9 import java.util.List; 8 10 9 11 @AllArgsConstructor … … 73 75 private String offer_shop_code; 74 76 77 @ManyToMany(mappedBy = "favouriteOffers") 78 private List<User> users = new ArrayList<User>(); 79 75 80 @ManyToOne(fetch = FetchType.LAZY) 76 81 @JoinColumn(name = "phone_id", referencedColumnName = "id") -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/entities/User.java
rdbd4834 rf25d07e 8 8 9 9 import javax.persistence.*; 10 import java.util.ArrayList; 10 11 import java.util.Collection; 11 12 import java.util.Collections; 13 import java.util.List; 12 14 13 15 @Getter … … 16 18 @NoArgsConstructor 17 19 @AllArgsConstructor 18 @Entity(name = "users") 20 @Entity(name = "User") 21 @Table(name = "users") 19 22 public class User implements UserDetails { 20 23 … … 38 41 private Boolean locked = false; 39 42 private Boolean enabled = false; 43 44 @ManyToMany 45 @JoinTable( 46 name = "users_favourite_offers", 47 joinColumns = @JoinColumn(name = "user_id"), 48 inverseJoinColumns = @JoinColumn(name = "offer_id") 49 ) 50 private List<PhoneOffer> favouriteOffers = new ArrayList<PhoneOffer>(); 40 51 41 52 public User(String firstName, String lastName, String email, String password, UserRole userRole) { -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/repositories/ConfirmationTokenRepository.java
rdbd4834 rf25d07e 17 17 @Transactional 18 18 @Modifying 19 @Query("UPDATE confirmation_tokensc " +19 @Query("UPDATE ConfirmationToken c " + 20 20 "SET c.confirmedAt = ?2 " + 21 21 "WHERE c.token = ?1") -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/repositories/UserRepository.java
rdbd4834 rf25d07e 18 18 @Transactional 19 19 @Modifying 20 @Query("UPDATE usersa " +20 @Query("UPDATE User a " + 21 21 "SET a.enabled = TRUE WHERE a.email = ?1") 22 22 int enableUser(String email); -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/security/configs/WebSecurityConfig.java
rdbd4834 rf25d07e 1 1 package finki.it.phoneluxbackend.security.configs; 2 2 3 import finki.it.phoneluxbackend.security.CustomAuthenticationFilter; 4 import finki.it.phoneluxbackend.security.CustomAuthorizationFilter; 3 5 import finki.it.phoneluxbackend.services.UserService; 4 6 import lombok.AllArgsConstructor; 7 import org.springframework.context.annotation.Bean; 5 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.security.authentication.AuthenticationManager; 6 10 import org.springframework.security.authentication.dao.DaoAuthenticationProvider; 7 import org.springframework.security.config.annotation.SecurityBuilder;8 11 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 9 import org.springframework.security.config.annotation.web.WebSecurityConfigurer;10 12 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 11 13 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 12 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;13 14 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 14 import org.springframework.security.config. annotation.web.configuration.WebSecurityCustomizer;15 import org.springframework.security.config.http.SessionCreationPolicy; 15 16 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 16 import org.springframework.security.web.SecurityFilterChain; 17 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; 18 19 import static org.springframework.http.HttpMethod.GET; 17 20 18 21 @Configuration … … 26 29 @Override 27 30 protected void configure(HttpSecurity http) throws Exception { 28 http 29 .csrf().disable() 30 .authorizeRequests() 31 .antMatchers("/registration/**") 32 .permitAll() 33 .anyRequest() 34 .authenticated().and() 35 .formLogin(); 31 // http 32 // .csrf().disable() 33 // .authorizeRequests() 34 // .antMatchers("/registration/**") 35 // .permitAll() 36 // .anyRequest() 37 // .authenticated().and() 38 // .formLogin(); 39 40 http.csrf().disable(); 41 http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 42 // http.authorizeRequests().antMatchers(GET,"/phones").hasAnyAuthority("USER"); 43 http.authorizeRequests().anyRequest().permitAll(); 44 http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean())); 45 http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class); 36 46 37 47 } … … 42 52 } 43 53 54 @Bean 55 @Override 56 public AuthenticationManager authenticationManagerBean() throws Exception { 57 return super.authenticationManagerBean(); 58 } 44 59 45 60 public DaoAuthenticationProvider daoAuthenticationProvider(){ -
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.