Skip to content

Commit

Permalink
Working on login from frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
stp94 committed Feb 26, 2021
1 parent 8df5936 commit 7c89d88
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 93 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@







<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
Expand All @@ -71,6 +75,17 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>





</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void configure(HttpSecurity httpSecurity)
throws Exception {
httpSecurity.csrf().disable();
httpSecurity.authorizeRequests()
.antMatchers("/trucks/all","/session/player/**").permitAll()
.antMatchers("/trucks/all","/session/player/**","/order/all").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
Expand All @@ -46,37 +46,29 @@ protected void configure(HttpSecurity httpSecurity)
.alwaysRemember(true)
.tokenValiditySeconds(30*5);



httpSecurity.headers()
.frameOptions()
.sameOrigin();

}


@Autowired
protected void configureGlobal(AuthenticationManagerBuilder builder, MyUserDetailService myUserDetailService) throws Exception {
// builder.jdbcAuthentication()
// .dataSource(dataSource)
// .usersByUsernameQuery("select username,password,enabled from users where username = ?")
// .authoritiesByUsernameQuery("select username,authority from authorities where username = ?")
//
// ;
builder.userDetailsService(myUserDetailService)
.passwordEncoder(passwordEncoder());


}

@Bean
public PasswordEncoder passwordEncoder () {
@Bean
public PasswordEncoder passwordEncoder () {
return new BCryptPasswordEncoder();
}
}




}
}



Original file line number Diff line number Diff line change
@@ -1,8 +1,92 @@
package pl.staszczykpiotr.truckforwarder_backend.controller;


import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import pl.staszczykpiotr.truckforwarder_backend.dto.Course;
import pl.staszczykpiotr.truckforwarder_backend.dto.Order;
import pl.staszczykpiotr.truckforwarder_backend.repository.CourseRepository;
import pl.staszczykpiotr.truckforwarder_backend.repository.OrderRepository;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class OrderController {

private OrderRepository orderRepository;
private CourseRepository courseRepository;

@Autowired
public OrderController(OrderRepository orderRepository, CourseRepository courseRepository){
this.orderRepository = orderRepository;
this.courseRepository = courseRepository;

}


@GetMapping("/order/all")

public List<Order> getOrders() {
return orderRepository.findAll();
}

@GetMapping("/order/vieworders")
public List<Map> getViewOrders(){
return orderRepository.findOrders();
}

@GetMapping("/session/course/viewcourses")
public List<Map> getViewCourses(){

return courseRepository.findCourses(SecurityContextHolder.getContext().getAuthentication().getName());
}

@PostMapping("/session/course/new")
public void postCreateCourse(@RequestBody com.fasterxml.jackson.databind.JsonNode userDataForCourse){



Calendar startTime = Calendar.getInstance();
Calendar endTime = Calendar.getInstance();
endTime.add(Calendar.SECOND,userDataForCourse.get("Distance").intValue());

Course course = new Course();
course.setId(Math.toIntExact(courseRepository.count()+1));
course.setStart_time(startTime);
course.setEnd_time(endTime);
course.setOwner(SecurityContextHolder.getContext().getAuthentication().getName());
course.setIdbought(userDataForCourse.get("selectedTruckId").intValue());
course.setIdorders(userDataForCourse.get("selectedOrderId").intValue());
course.setDuration(userDataForCourse.get("Distance").floatValue());
course.setFinished(false);
course.setProgress(0);
courseRepository.save(course);


//System.out.println(userDataForCourse.get("selectedOrderId")+"||"+userDataForCourse.get("selectedTruckId")+"||"+ userDataForCourse.get("Distance"));

}




@GetMapping("/session/course/update_progress")
public List<Map> getProgress(int id, int progress){


Course c = courseRepository.findById(id);
c.setProgress(progress);
courseRepository.save(c);


return courseRepository.getProgress(id);
}




// public List<Map> findCourses(){return courseRepository.findCourses(SecurityContextHolder.getContext().getAuthentication().getName());}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;
import pl.staszczykpiotr.truckforwarder_backend.MyUserDetailService;
import pl.staszczykpiotr.truckforwarder_backend.MyUserPrincipal;
import pl.staszczykpiotr.truckforwarder_backend.dto.BoughtTrucks;
import pl.staszczykpiotr.truckforwarder_backend.dto.Player;
import pl.staszczykpiotr.truckforwarder_backend.dto.Truck;
Expand All @@ -14,42 +11,108 @@
import pl.staszczykpiotr.truckforwarder_backend.repository.TruckRepository;

import java.util.List;
import java.util.Map;

@RestController


public class PlayerController {
private PlayerRepository playerRepository;
private BoughtTrucksRepository boughtTrucksRepository;


private TruckRepository truckRepository;



@Autowired
public PlayerController(PlayerRepository playerRepository, BoughtTrucksRepository boughtTrucksRepository) {
public PlayerController(PlayerRepository playerRepository, BoughtTrucksRepository boughtTrucksRepository, TruckRepository truckRepository ) {
this.playerRepository = playerRepository;
this.boughtTrucksRepository = boughtTrucksRepository;
this.truckRepository = truckRepository;
}

@GetMapping("/trucks/all")

public List<Truck> getTrucks() {
return truckRepository.findAll();
}


@GetMapping("/session/player/info")

public Player getPlayer() {
return playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName());

}

return playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName());
@GetMapping("/session/player/bought_trucks")

public List<Map> getBoughtTrucks() {
System.out.println(SecurityContextHolder.getContext().getAuthentication().getName());
System.out.println(boughtTrucksRepository.findBoughtTrucks(SecurityContextHolder.getContext().getAuthentication().getName()));
return boughtTrucksRepository.findBoughtTrucks(SecurityContextHolder.getContext().getAuthentication().getName());
}

@GetMapping("/session/player/bought_trucks")

public BoughtTrucks getBoughtTrucks(){



@PostMapping("/session/player/purchase_truck")
public void postPurchaseTruck(@RequestBody String SelectedTruck){

StringBuilder stringBuilder = new StringBuilder();
String selectedTruckByUser;
stringBuilder.append(SelectedTruck);
stringBuilder.delete(0,18);
stringBuilder.delete(stringBuilder.length()-2,stringBuilder.length()); // Format String from Post to correctly form
selectedTruckByUser = stringBuilder.toString();

BoughtTrucks boughtTrucks = new BoughtTrucks();

boughtTrucks.setIdbought_trucks(Math.toIntExact(boughtTrucksRepository.count() + 1));
boughtTrucks.setIdtrucks(truckRepository.findByName(selectedTruckByUser).getID());
boughtTrucks.setIdplayers(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()).getIdplayers());
boughtTrucks.setLife(100);
boughtTrucks.setAvailable(true);
boughtTrucks.setOwner(SecurityContextHolder.getContext().getAuthentication().getName());

boughtTrucksRepository.save(boughtTrucks); // Add Bought Truck


playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()).
setCash(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()).getCash() -
truckRepository.findByName(selectedTruckByUser).getPrice()); // Set Cash Status

playerRepository.save(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName())); // Update Cash Status



}

@PostMapping("/session/player/changeAvailability")
public void changeAvailability (@RequestBody com.fasterxml.jackson.databind.JsonNode selectedTruck){

System.out.println(selectedTruck.get("selectedTruckId"));
BoughtTrucks bt = boughtTrucksRepository.findById(Integer.parseInt(selectedTruck.get("selectedTruckId").toString()));
bt.setAvailable(false);
boughtTrucksRepository.save(bt);

}

@PostMapping("/session/player/changeAvailabilityToTrue")
public void changeAvailabilityToTrue (@RequestBody com.fasterxml.jackson.databind.JsonNode selectedTruck){

System.out.println(selectedTruck.get("selectedTruckId"));
System.out.println(selectedTruck.get("rewardCash"));
playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()).
setCash(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()).getCash() + selectedTruck.get("rewardCash").floatValue()); // Set Cash Status
playerRepository.save(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()));

BoughtTrucks bt = boughtTrucksRepository.findById(Integer.parseInt(selectedTruck.get("selectedTruckId").toString()));
bt.setAvailable(true);
boughtTrucksRepository.save(bt);

return boughtTrucksRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName());
}





Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public Principal retrievePrincipal(Principal principal) {


@PostMapping()
public Player loginUser(@RequestBody String username, String password){
public Boolean loginUser(@RequestBody String username, String password){

return playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName());
//return playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName());
return SecurityContextHolder.getContext().getAuthentication().isAuthenticated();

}

Expand Down
Loading

0 comments on commit 7c89d88

Please sign in to comment.