Changeset 84d0fbb
- Timestamp:
- 12/19/21 19:39:00 (3 years ago)
- Branches:
- master
- Children:
- bdd6491
- Parents:
- 1ad8e64
- Files:
-
- 12 added
- 1 deleted
- 29 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/src/app/_models/dto/plannerDto.ts
r1ad8e64 r84d0fbb 1 import { Location } from "../location";2 1 3 2 export class PlannerDto{ -
trip-planner-front/src/app/_models/planner.ts
r1ad8e64 r84d0fbb 1 1 export class Planner{ 2 2 3 id: number; 3 4 name: string; -
trip-planner-front/src/app/_services/planner.service.ts
r1ad8e64 r84d0fbb 1 import { HttpClient } from "@angular/common/http";1 import { HttpClient, HttpHeaders } from "@angular/common/http"; 2 2 import { Injectable } from "@angular/core"; 3 3 import { Observable } from "rxjs"; … … 8 8 providedIn: 'root' 9 9 }) 10 export class PlannerService{ 11 10 export class PlannerService { 11 12 httpHeaders: HttpHeaders = new HttpHeaders({ 13 'Authorization': ''+sessionStorage.getItem("token"), 14 'Accept': 'application/json', 15 'Content-Type': 'application/json' 16 }); 17 18 12 19 constructor(private httpClient: HttpClient){ 13 20 } 14 21 22 15 23 getAllPlanners():Observable<Planner[]>{ 16 let url = "http://localhost:8080/api/planners"; 17 return this.httpClient.get<Planner[]>(url); 24 let url = "http://localhost:8080/api/planners/user"; 25 console.log("SERVID: " + sessionStorage.getItem("token")); 26 console.log(this.httpHeaders.get('Authorization')) ; 27 return this.httpClient.get<Planner[]>(url, {headers: this.httpHeaders}); 18 28 } 19 29 20 postInitialPlanner(planner: Planner): Observable< Object>{30 postInitialPlanner(planner: Planner): Observable<Planner>{ 21 31 let url = "http://localhost:8080/api/planner/new"; 22 32 return this.httpClient.post<Planner>(url, planner); … … 25 35 updatePlanner(id: number, plannerDto : PlannerDto):Observable<Planner>{ 26 36 let url = "http://localhost:8080/api/edit/planner/" + id; 27 return this.httpClient.put<Planner>(url, plannerDto );37 return this.httpClient.put<Planner>(url, plannerDto, {headers: this.httpHeaders}); 28 38 } 29 39 -
trip-planner-front/src/app/app-routing.module.ts
r1ad8e64 r84d0fbb 1 1 import { NgModule } from '@angular/core'; 2 2 import { RouterModule, Routes } from '@angular/router'; 3 import { AuthGuard } from './auth/auth.guard'; 3 4 import { HomepageComponent } from './homepage/homepage.component'; 5 import { LoginComponent } from './homepage/login/login.component'; 4 6 import { LocationDetailsComponent } from './location/location-details/location-details.component'; 5 7 import { LocationComponent } from './location/location.component'; … … 14 16 {path: 'locations', component: LocationComponent}, 15 17 {path: '', component: HomepageComponent}, 18 {path: '', component:LoginComponent}, 16 19 {path: 'location', component: LocationDetailsComponent} 17 20 ]; -
trip-planner-front/src/app/app.module.ts
r1ad8e64 r84d0fbb 4 4 import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core'; 5 5 import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 6 import { HttpClient, HttpClientModule } from '@angular/common/http';6 import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; 7 7 import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 8 8 import { CategoryService } from './_services/cateogry.service'; … … 47 47 import { RegisterComponent } from './homepage/register/register.component'; 48 48 import { LoginComponent } from './homepage/login/login.component'; 49 import { AuthGuard } from './auth/auth.guard'; 50 import { AuthInterceptor } from './auth/auth.interceptor'; 49 51 50 52 @NgModule({ … … 109 111 PlannerService, 110 112 { 111 provide: MatDialogRef, 112 useValue: {} 113 provide: MatDialogRef, 114 useValue: {}, 115 116 }, 117 { 118 provide: HTTP_INTERCEPTORS, 119 useClass: AuthInterceptor, 120 multi: true 113 121 }, 114 122 DialogService, 115 123 MessageService, 116 ConfirmationService 117 124 ConfirmationService, 125 AuthGuard 118 126 ], 119 127 -
trip-planner-front/src/app/create-initial-planner/create-initial-planner.component.ts
r1ad8e64 r84d0fbb 12 12 13 13 planner: Planner; 14 plannerDto: PlannerDto;15 14 16 15 constructor( private ref: DynamicDialogRef) { 17 16 this.planner = new Planner; 18 this.plannerDto = new PlannerDto();19 17 } 20 18 21 19 ngOnInit(): void { 22 20 this.planner = new Planner(); 23 this.plannerDto = new PlannerDto();24 21 } 25 22 -
trip-planner-front/src/app/homepage/homepage.component.html
r1ad8e64 r84d0fbb 21 21 </li> 22 22 <li class="nav-item"> 23 <button class="btn btn-dark" >Sign in</button>23 <button class="btn btn-dark" (click)="onClickLogIn()">Sign in</button> 24 24 </li> 25 25 </ul> … … 30 30 <img src={{imageURI}} /> 31 31 <h4></h4> 32 33 32 <!-- 33 <div class="content-section implementation carousel-demo" style="padding-top: 2em;"> 34 34 <div class="card"> 35 35 <p-carousel [value]="locations" [numVisible]="3" [numScroll]="3" [circular]="false" … … 91 91 </div> 92 92 </div> 93 --> 94 93 95 </main> 94 96 <br> -
trip-planner-front/src/app/homepage/homepage.component.ts
r1ad8e64 r84d0fbb 1 1 import { Component, OnInit } from '@angular/core'; 2 import { Router } from '@angular/router'; 2 3 import { DialogService, DynamicDialogRef } from 'primeng/dynamicdialog'; 4 import { LoginRequest } from '../_models/dto/loginRequest'; 5 import { UserDto } from '../_models/dto/userDto'; 3 6 import { Location } from '../_models/location'; 4 7 import { LocationService } from '../_services/location.service'; 8 import { UserService } from '../_services/user.service'; 9 import { LoginComponent } from './login/login.component'; 5 10 import { RegisterComponent } from './register/register.component'; 6 11 … … 20 25 ref: DynamicDialogRef; 21 26 22 constructor(private locationService: LocationService, private dialogService: DialogService) { 27 constructor(private locationService: LocationService, private dialogService: DialogService, private userService: UserService, 28 private router: Router) { 23 29 this.responsiveOptions = [ 24 30 { … … 44 50 45 51 ngOnInit(): void { 46 52 /* 47 53 this.locationService.getWeekendGetaways().subscribe( 48 54 data => { … … 55 61 } 56 62 ); 63 */ 57 64 } 58 65 59 onClickSignUp(){ 60 console.log("VLEGOV"); 66 onClickSignUp() { 61 67 this.ref = this.dialogService.open(RegisterComponent, { 62 68 header: 'Register form', … … 64 70 contentStyle: { "max-height": "500px", "overflow": "auto" }, 65 71 baseZIndex: 10000 66 }); 72 }); 73 74 this.ref.onClose.subscribe((user: UserDto) => { 75 this.userService.registerUser(user).subscribe( 76 data => { 77 console.log(data); 78 79 } 80 ); 81 }); 82 } 83 84 85 onClickLogIn() { 86 this.ref = this.dialogService.open(LoginComponent, { 87 header: 'Log in if you already have an account', 88 width: '70%', 89 contentStyle: { "max-height": "500px", "overflow": "auto" }, 90 baseZIndex: 10000 91 }); 92 this.ref.onClose.subscribe((loginRequest : LoginRequest) => { 93 this.userService.authenticateUser(loginRequest).subscribe( 94 (data : any) => { 95 console.log(data); 96 if(this.userService.isUserLoggedIn()){ 97 this.router.navigate(['planners']); 98 } 99 } 100 ); 101 }); 67 102 } 68 103 } -
trip-planner-front/src/app/homepage/login/login.component.html
r1ad8e64 r84d0fbb 1 <p>login works!</p> 1 <div class="p-col-12 p-md-4"> 2 <div class="p-inputgroup"> 3 <span class="p-inputgroup-addon"><i class="pi pi-user"></i></span> 4 <input type="text" pInputText placeholder="Email address (Username)" 5 [(ngModel)]="loginRequest.username" name="username"> 6 </div> 7 </div> 8 <div class="p-col-12 p-md-4"> 9 <div class="p-inputgroup"> 10 <span class="p-inputgroup-addon"><i class="pi pi-password"></i></span> 11 <input type="password" pInputText placeholder="Password" name="password" [(ngModel)]="loginRequest.password"> 12 </div> 13 </div> 14 15 <div class="p-col-12 p-md-4"> 16 <button pButton pRipple label="Log in" class="p-button-secondary" type="submit" 17 (click)="onFormLogIn(loginRequest)"></button> 18 </div> -
trip-planner-front/src/app/homepage/login/login.component.ts
r1ad8e64 r84d0fbb 1 1 import { Component, OnInit } from '@angular/core'; 2 import { Router } from '@angular/router'; 3 import { DynamicDialogRef } from 'primeng/dynamicdialog'; 4 import { LoginRequest } from 'src/app/_models/dto/loginRequest'; 5 import { UserService } from 'src/app/_services/user.service'; 2 6 3 7 @Component({ … … 8 12 export class LoginComponent implements OnInit { 9 13 10 constructor() { } 14 15 loginRequest : LoginRequest; 16 17 constructor(private ref: DynamicDialogRef, private router: Router, private userService : UserService) { 18 this.loginRequest = new LoginRequest(); 19 20 } 11 21 12 22 ngOnInit(): void { 13 23 } 14 24 25 onFormLogIn(loginRequest){ 26 this.ref.close(loginRequest); 27 28 } 15 29 } -
trip-planner-front/src/app/homepage/register/register.component.html
r1ad8e64 r84d0fbb 1 <div class="p-grid p-fluid"> 2 <div class="p-col-12 p-md-4"> 3 <div class="p-inputgroup"> 4 <span class="p-inputgroup-addon"><i class="pi pi-user"></i></span> 5 <input type="text" pInputText placeholder="Full name"> 1 2 <div class="p-grid p-fluid"> 3 <div class="p-col-12 p-md-4"> 4 <div class="p-inputgroup"> 5 <span class="p-inputgroup-addon"><i class="pi pi-user"></i></span> 6 <input type="text" pInputText placeholder="Full name" [(ngModel)]="user.fullName" name="fullName" 7 > 8 </div> 6 9 </div> 7 </div> 8 9 <div class="p-col-12 p-md-4"> 10 <div class="p-inputgroup"> 11 <span class="p-inputgroup-addon"><i class="pi pi-user"></i></span> 12 <input type="text" pInputText placeholder="Ema address (Username)"> 10 11 <div class="p-col-12 p-md-4"> 12 <div class="p-inputgroup"> 13 <span class="p-inputgroup-addon"><i class="pi pi-user"></i></span> 14 <input type="text" pInputText placeholder="Email address (Username)" [(ngModel)]="user.username" name="username" 15 > 16 </div> 13 17 </div> 14 </div> 15 <div class="p-col-12 p-md-4"> 16 <div class="p-inputgroup"> 17 <span class="p-inputgroup-addon"><i class="pi pi-password"></i></span> 18 <input type="text" pInputText placeholder="Password"> 18 <div class="p-col-12 p-md-4"> 19 <div class="p-inputgroup"> 20 <span class="p-inputgroup-addon"><i class="pi pi-password"></i></span> 21 <input type="password" pInputText placeholder="Password" [(ngModel)]="user.password" name="password" 22 > 23 </div> 19 24 </div> 20 </div> 21 <div class="p-col-12 p-md-4"> 22 <div class="p-inputgroup"> 23 <span class="p-inputgroup-addon"><i class="pi pi-password"></i></span> 24 <input type="text" pInputText placeholder="Repeat password"> 25 <div class="p-col-12 p-md-4"> 26 <div class="p-inputgroup"> 27 <span class="p-inputgroup-addon"><i class="pi pi-password"></i></span> 28 <input type="password" pInputText placeholder="Repeat password" [(ngModel)]="user.confirmPassword" name="confirmPassword" 29 > 30 </div> 25 31 </div> 26 </div> 27 28 <div class="p-col-12 p-md-4"> 29 <button pButton pRipple type="button" label="Submit" class="p-button-secondary"></button> 30 </div> 31 </div> 32 33 <div class="p-col-12 p-md-4"> 34 <button pButton pRipple label="Submit" class="p-button-secondary" type="submit" (click)="onFormSubmitSignUp(user)"></button> 35 </div> 36 </div> -
trip-planner-front/src/app/homepage/register/register.component.ts
r1ad8e64 r84d0fbb 1 1 import { Component, OnInit } from '@angular/core'; 2 import { DynamicDialogRef } from 'primeng/dynamicdialog'; 3 import { UserDto } from 'src/app/_models/dto/userDto'; 2 4 3 5 @Component({ … … 8 10 export class RegisterComponent implements OnInit { 9 11 10 constructor() { } 12 user : UserDto; 13 14 constructor(private ref: DynamicDialogRef) { 15 this.user = new UserDto(); 16 } 11 17 12 18 ngOnInit(): void { 13 19 } 14 20 21 onFormSubmitSignUp(user){ 22 this.ref.close(user); 23 //window.location.reload(); 24 } 15 25 } -
trip-planner-front/src/app/planner/planner.component.ts
r1ad8e64 r84d0fbb 7 7 import { DialogService, DynamicDialogRef } from 'primeng/dynamicdialog'; 8 8 import { MessageService, PrimeNGConfig } from 'primeng/api'; 9 import { UserDto } from '../_models/dto/userDto'; 10 import { UserService } from '../_services/user.service'; 9 11 10 12 … … 20 22 plannerDto: PlannerDto; 21 23 ref: DynamicDialogRef; 22 24 user: UserDto; 23 25 24 26 constructor(private plannerService: PlannerService, private router: Router, 25 private dialogService: DialogService, private primengConfig: PrimeNGConfig, private messageService: MessageService) { 27 private dialogService: DialogService, private primengConfig: PrimeNGConfig, private messageService: MessageService, 28 private userService : UserService) { 26 29 this.planners = []; 27 30 this.plannerDto = new PlannerDto(); 28 31 this.ref = new DynamicDialogRef; 32 this.user = new UserDto(); 29 33 } 30 34 … … 36 40 data => { 37 41 this.planners = data; 42 localStorage.getItem("token"); 38 43 } 39 ); 44 ); 40 45 } 41 46 -
trip-planner/src/main/java/finki/diplomska/tripplanner/TripPlannerApplication.java
r1ad8e64 r84d0fbb 6 6 import org.springframework.context.annotation.Bean; 7 7 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 8 import org.springframework.web.cors.CorsConfiguration; 9 import org.springframework.web.cors.CorsConfigurationSource; 10 import org.springframework.web.cors.UrlBasedCorsConfigurationSource; 11 12 import java.util.Arrays; 8 13 9 14 @SpringBootApplication … … 15 20 return new BCryptPasswordEncoder(); 16 21 } 22 23 17 24 public static void main(String[] args) { 18 25 SpringApplication.run(TripPlannerApplication.class, args); -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/Planner.java
r1ad8e64 r84d0fbb 1 1 package finki.diplomska.tripplanner.models; 2 2 3 import com.fasterxml.jackson.annotation.JsonIgnore; 3 4 import lombok.*; 4 import reactor.util.annotation.Nullable;5 5 6 6 import javax.persistence.*; … … 10 10 @Data 11 11 @Table(name = "planners") 12 @NoArgsConstructor13 12 @AllArgsConstructor 14 13 @Getter … … 34 33 private List<Location> locationList; 35 34 35 @ManyToOne(fetch = FetchType.EAGER) 36 private User user; 36 37 37 public Planner(String name, String description, List<Location> locationList){ 38 public Planner(){ 39 } 40 41 public Planner(String name, String description, List<Location> locationList, User user){ 38 42 this.name = name; 39 43 this.description = description; 40 44 this.locationList = locationList; 45 this.user = user; 41 46 } 42 47 -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/User.java
r1ad8e64 r84d0fbb 11 11 import javax.validation.constraints.Email; 12 12 import javax.validation.constraints.NotBlank; 13 import java.util.ArrayList; 13 14 import java.util.Collection; 14 15 import java.util.Date; 16 import java.util.List; 15 17 16 18 @Entity … … 38 40 private Date update_At; 39 41 40 //OneToMany with Project 42 //OneToMany with Planners 43 @OneToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, mappedBy = "user", orphanRemoval = true) 44 @JsonIgnore 45 private List<Planner> planners = new ArrayList<>(); 41 46 42 47 public User() { 43 48 } 44 49 50 public User(String username, String fullName, String password, String confirmPassword) { 51 this.username = username; 52 this.fullName = fullName; 53 this.password = password; 54 this.confirmPassword = confirmPassword; 55 } 45 56 46 57 @PrePersist -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/PlannerDto.java
r1ad8e64 r84d0fbb 9 9 private String name; 10 10 private String description; 11 private String user; 11 12 12 public PlannerDto(String name, String description ) {13 public PlannerDto(String name, String description, String user) { 13 14 this.name = name; 14 15 this.description = description; 16 this.user = user; 15 17 } 16 18 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/UserDto.java
r1ad8e64 r84d0fbb 20 20 private Date update_At; 21 21 22 public UserDto(String username, String fullName, String password, String confirmPassword , Date create_At, Date update_At) {22 public UserDto(String username, String fullName, String password, String confirmPassword) { 23 23 this.username = username; 24 24 this.fullName = fullName; 25 25 this.password = password; 26 26 this.confirmPassword = confirmPassword; 27 this.create_At = create_At; 28 this.update_At = update_At; 27 29 28 } 30 29 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/repository/jpa/JpaPlannerRepository.java
r1ad8e64 r84d0fbb 4 4 import finki.diplomska.tripplanner.models.Planner; 5 5 import org.springframework.data.jpa.repository.JpaRepository; 6 import org.springframework.data.jpa.repository.Query; 7 import org.springframework.data.repository.query.Param; 6 8 import org.springframework.stereotype.Repository; 7 9 … … 11 13 public interface JpaPlannerRepository extends JpaRepository<Planner, Long> { 12 14 13 15 @Query(value = "SELECT * FROM planners AS p left JOIN users AS u ON p.user_id = u.id WHERE u.username like :username", nativeQuery = true) 16 List<Planner> getPlannersByUser(@Param("username") String username ); 14 17 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/repository/jpa/JpaUserRepository.java
r1ad8e64 r84d0fbb 7 7 @Repository 8 8 public interface JpaUserRepository extends CrudRepository<User, Long> { 9 User findByUsername(String username); 10 User getById(Long id); 9 11 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/security/SecurityConfig.java
r1ad8e64 r84d0fbb 2 2 3 3 4 import finki.diplomska.tripplanner.service.impl.CustomUserDetailsServiceImpl; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.annotation.Bean; 5 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.security.authentication.AuthenticationManager; 9 import org.springframework.security.config.BeanIds; 10 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 11 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 7 12 import org.springframework.security.config.annotation.web.builders.HttpSecurity; … … 9 14 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 10 15 import org.springframework.security.config.http.SessionCreationPolicy; 16 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 17 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; 18 19 import static finki.diplomska.tripplanner.security.SecurityConstants.MARIADB_URL; 20 import static finki.diplomska.tripplanner.security.SecurityConstants.SIGN_UP_URLS; 11 21 12 22 @Configuration … … 21 31 @Autowired 22 32 private JwtAuthenticationEntryPoint unauthorizedHandler; 33 34 @Autowired 35 private CustomUserDetailsServiceImpl customUserDetailsService; 36 37 @Bean 38 public JwtAuthenticationFilter jwtAuthenticationFilter() {return new JwtAuthenticationFilter();} 39 40 41 @Autowired 42 private BCryptPasswordEncoder bCryptPasswordEncoder; 43 44 @Override 45 protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { 46 authenticationManagerBuilder.userDetailsService(customUserDetailsService).passwordEncoder(bCryptPasswordEncoder); 47 } 48 49 @Override 50 @Bean(BeanIds.AUTHENTICATION_MANAGER) 51 protected AuthenticationManager authenticationManager() throws Exception { 52 return super.authenticationManager(); 53 } 23 54 24 55 @Override … … 43 74 "/**/*.js" 44 75 ).permitAll() 45 .antMatchers("/api/users/**").permitAll() 76 .antMatchers(SIGN_UP_URLS).permitAll() 77 .antMatchers(MARIADB_URL).permitAll() 46 78 .anyRequest().authenticated(); 79 http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); 80 47 81 } 48 82 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/PlannerService.java
r1ad8e64 r84d0fbb 11 11 12 12 Planner createPlannerWithRequestParams(String description, String name, List<Location> locationList); 13 Optional<Planner> editPlanner(Long id, PlannerDto plannerDto );13 Optional<Planner> editPlanner(Long id, PlannerDto plannerDto, String username); 14 14 List<Planner> getAllPlaners(); 15 List<Planner> getPlannersByUser(String username); 15 16 Optional<Planner> findById(Long id); 16 17 Planner editPlannerWithRequestParams(Long id, String description, String name, List<Location> locationList); 17 Optional<Planner> newPlanner(PlannerDto plannerDto );18 Optional<Planner> newPlanner(PlannerDto plannerDto, String username); 18 19 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/UserService.java
r1ad8e64 r84d0fbb 2 2 3 3 import finki.diplomska.tripplanner.models.User; 4 import finki.diplomska.tripplanner.models.dto.UserDto; 4 5 6 import java.util.Optional; 5 7 6 8 -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/PlannerServiceImpl.java
r1ad8e64 r84d0fbb 3 3 import finki.diplomska.tripplanner.models.Location; 4 4 import finki.diplomska.tripplanner.models.Planner; 5 import finki.diplomska.tripplanner.models.User; 5 6 import finki.diplomska.tripplanner.models.dto.PlannerDto; 6 7 import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException; … … 8 9 import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository; 9 10 import finki.diplomska.tripplanner.repository.jpa.JpaPlannerRepository; 10 import finki.diplomska.tripplanner. service.LocationService;11 import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository; 11 12 import finki.diplomska.tripplanner.service.PlannerService; 12 13 import org.springframework.stereotype.Service; 13 import reactor.util.annotation.Nullable;14 14 15 import java .util.ArrayList;15 import javax.jws.soap.SOAPBinding; 16 16 import java.util.List; 17 17 import java.util.Optional; … … 22 22 private final JpaPlannerRepository plannerRepository; 23 23 private final JpaLocationRepository locationRepository; 24 private final JpaUserRepository userRepository; 24 25 25 public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository ) {26 public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository, JpaUserRepository userRepository) { 26 27 this.plannerRepository = plannerRepository; 27 28 this.locationRepository = locationRepository; 29 this.userRepository = userRepository; 28 30 } 29 31 … … 31 33 public List<Planner> getAllPlaners() { 32 34 return this.plannerRepository.findAll(); 35 } 36 37 @Override 38 public List<Planner> getPlannersByUser(String username) { 39 User user = this.userRepository.findByUsername(username); 40 return this.plannerRepository.getPlannersByUser(user.getUsername()); 33 41 } 34 42 … … 40 48 41 49 @Override 42 public Optional<Planner> newPlanner(PlannerDto plannerDto ) {50 public Optional<Planner> newPlanner(PlannerDto plannerDto, String username) { 43 51 /* 44 52 List<Location> locationList = new ArrayList<>(); … … 48 56 locationList.add(loc); 49 57 } 50 51 58 */ 52 return Optional.of(this.plannerRepository.save(new Planner(plannerDto.getName(), plannerDto.getDescription(), null))); 59 User user = this.userRepository.findByUsername(username); 60 plannerDto.setUser(user.getUsername()); 61 return Optional.of(this.plannerRepository.save(new Planner(plannerDto.getName(), plannerDto.getDescription(), null, user))); 53 62 } 54 63 … … 69 78 70 79 @Override 71 public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto ) {80 public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto, String username) { 72 81 Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new PlannerNotFoundException(id)); 82 User user = this.userRepository.findByUsername(username); 73 83 74 84 planner.setName(plannerDto.getName()); 75 85 planner.setDescription(plannerDto.getDescription()); 86 plannerDto.setUser(user.getUsername()); 87 planner.setUser(user); 76 88 /* 77 89 List<Location> locationList = new ArrayList<>(); -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/UserServiceImpl.java
r1ad8e64 r84d0fbb 9 9 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 10 10 import org.springframework.stereotype.Service; 11 12 import java.util.Optional; 11 13 12 14 … … 28 30 // We don't persist or show the confirmPassword 29 31 newUser.setConfirmPassword(""); 30 return this.userRepository.save(newUser);32 return this.userRepository.save(newUser); 31 33 }catch(Exception e){ 32 34 throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists"); -
trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.java
r1ad8e64 r84d0fbb 3 3 import finki.diplomska.tripplanner.models.Location; 4 4 import finki.diplomska.tripplanner.models.Planner; 5 import finki.diplomska.tripplanner.models.User; 5 6 import finki.diplomska.tripplanner.models.dto.PlannerDto; 6 7 import finki.diplomska.tripplanner.service.LocationService; … … 9 10 import org.springframework.http.MediaType; 10 11 import org.springframework.http.ResponseEntity; 12 import org.springframework.security.core.Authentication; 11 13 import org.springframework.web.bind.annotation.*; 12 14 import org.springframework.web.bind.annotation.RestController; 13 15 16 import javax.validation.Valid; 14 17 import java.util.ArrayList; 15 18 import java.util.List; … … 17 20 18 21 @RestController 19 @CrossOrigin(origins = "http://localhost:4200" )22 @CrossOrigin(origins = "http://localhost:4200", exposedHeaders = "token") 20 23 @RequestMapping(value = "/api") 21 24 public class PlannerRestController { … … 31 34 public List<Planner> getAllPlanners(){ 32 35 return this.plannerService.getAllPlaners(); 36 } 37 38 39 @GetMapping(value = "/planners/user") 40 public List<Planner> getPlannersByUser(Authentication authentication){ 41 User user = (User) authentication.getPrincipal(); 42 return this.plannerService.getPlannersByUser(user.getUsername()); 33 43 } 34 44 … … 57 67 @PostMapping(value = "/planner/new", consumes= MediaType.APPLICATION_JSON_VALUE) 58 68 @ResponseStatus(HttpStatus.CREATED) 59 public ResponseEntity<Planner> newPlanner(@RequestBody PlannerDto plannerDto) { 60 return this.plannerService.newPlanner(plannerDto) 69 public ResponseEntity<?> newPlanner(@Valid @RequestBody PlannerDto plannerDto, Authentication authentication) { 70 71 User user = (User) authentication.getPrincipal(); 72 return this.plannerService.newPlanner(plannerDto, user.getUsername()) 61 73 .map(planner -> ResponseEntity.ok().body(planner)) 62 74 .orElseGet(() -> ResponseEntity.badRequest().build()); … … 65 77 66 78 @PutMapping(value ="edit/planner/{id}", consumes= MediaType.APPLICATION_JSON_VALUE) 67 public ResponseEntity<Planner> editPlanner(@PathVariable Long id, @RequestBody PlannerDto plannerDto){ 68 return this.plannerService.editPlanner(id, plannerDto) 79 public ResponseEntity<Planner> editPlanner(@PathVariable Long id, @Valid @RequestBody PlannerDto plannerDto, Authentication authentication){ 80 User user = (User) authentication.getPrincipal(); 81 return this.plannerService.editPlanner(id, plannerDto, user.getUsername()) 69 82 .map(planner -> ResponseEntity.ok().body(planner)) 70 83 .orElseGet(()-> ResponseEntity.badRequest().build()); -
trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/UserController.java
r1ad8e64 r84d0fbb 3 3 import finki.diplomska.tripplanner.models.User; 4 4 import finki.diplomska.tripplanner.models.dto.UserDto; 5 import finki.diplomska.tripplanner.payload.JWTLoginSucessReponse; 6 import finki.diplomska.tripplanner.payload.LoginRequest; 7 import finki.diplomska.tripplanner.security.JwtTokenProvider; 8 import finki.diplomska.tripplanner.security.SecurityConstants; 5 9 import finki.diplomska.tripplanner.service.UserService; 6 10 import finki.diplomska.tripplanner.service.impl.MapValidationErrorService; … … 9 13 import org.springframework.http.HttpStatus; 10 14 import org.springframework.http.ResponseEntity; 15 import org.springframework.security.authentication.AuthenticationManager; 16 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 17 import org.springframework.security.core.context.SecurityContextHolder; 11 18 import org.springframework.validation.BindingResult; 12 19 import org.springframework.web.bind.annotation.*; 13 20 import org.springframework.security.core.Authentication; 14 21 import javax.validation.Valid; 15 22 … … 28 35 private UserValidator userValidator; 29 36 30 @PostMapping("/register") 37 @Autowired 38 private JwtTokenProvider tokenProvider; 39 40 @Autowired 41 private AuthenticationManager authenticationManager; 42 43 @PostMapping(value = "/login") 44 public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest, BindingResult result){ 45 ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result); 46 if(errorMap != null) return errorMap; 47 48 Authentication authentication = authenticationManager.authenticate( 49 new UsernamePasswordAuthenticationToken( 50 loginRequest.getUsername(), 51 loginRequest.getPassword() 52 ) 53 ); 54 55 SecurityContextHolder.getContext().setAuthentication(authentication); 56 String jwt = SecurityConstants.TOKEN_PREFIX + tokenProvider.generateToken(authentication); 57 58 return ResponseEntity.ok(new JWTLoginSucessReponse(true, jwt)); 59 } 60 61 @PostMapping(value = "/register") 31 62 public ResponseEntity<?> registerUser(@Valid @RequestBody User user, BindingResult result){ 32 63 // Validate passwords match … … 38 69 39 70 return new ResponseEntity<User>(newUser, HttpStatus.CREATED); 71 40 72 } 41 73 } -
trip-planner/src/main/resources/application.properties
r1ad8e64 r84d0fbb 6 6 jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator 7 7 jasypt.encryptor.algorithm=PBEWithMD5AndDES 8 spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true 8 9 9 10 spring.jpa.show-sql=true -
trip-planner/src/test/api.http
r1ad8e64 r84d0fbb 75 75 GET http://localhost:8080/api/images?locationId=1 76 76 Content-Type: application/x-www-form-urlencoded 77 78 ### 79 POST http://localhost:8080/api/users/register 80 Content-Type: application/json 81 82 {"username": "teshht@test.com", "fullName": "nikola", "password" : "nikolce", "confirmPassword": "nikolce"} 83 84 ### 85 POST http://localhost:8080/api/users/login 86 Content-Type: application/json 87 88 {"username": "test@test.com", "password" : "nikolce"} 89 90 91 ### 92 GET http://localhost:8080/api/planners/user 93 Content-Type: application/json
Note:
See TracChangeset
for help on using the changeset viewer.