Skip to content

Commit

Permalink
secret sauce
Browse files Browse the repository at this point in the history
  • Loading branch information
W2AlharbiMe committed Sep 10, 2023
1 parent bc5348e commit 4c174e9
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/main/java/com/example/wayz/Model/UserTrips.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class UserTrips {


@ManyToOne
// @JoinColumn(name = "student_id", referencedColumnName = "user_id")
@JsonIgnore
private Driver driver;

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/example/wayz/Repository/UserTripsRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.wayz.Repository;

import com.example.wayz.Model.UserTrips;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserTripsRepository extends JpaRepository<UserTrips, Integer> {



}
64 changes: 64 additions & 0 deletions src/main/java/com/example/wayz/Service/UserTripsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.example.wayz.Service;

import com.example.wayz.Model.Driver;
import com.example.wayz.Model.StudentTrips;
import com.example.wayz.Model.UserTrips;
import com.example.wayz.Repository.DriverRepository;
import com.example.wayz.Repository.StudentTripsRepository;
import com.example.wayz.Repository.UserTripsRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserTripsService {

private final UserTripsRepository userTripsRepository;
private final StudentTripsRepository studentTripsRepository;
private final DriverRepository driverRepository;



public List<UserTrips> findAll() {
return userTripsRepository.findAll();
}


public void createUserTrips() {

List<StudentTrips> studentTrips = studentTripsRepository.findAll();
List<Driver> drivers = driverRepository.findAll();


// O(n^2)

for (Driver driver : drivers) {
int counter = driver.getCar().getSeats();

UserTrips userTrips = new UserTrips();
userTrips.setDriver(driver);

for (StudentTrips studentTrips1 : studentTrips) {
if(counter == 0) {
break;
}

userTrips.getStudentTrips().add(studentTrips1);

counter--;
}

userTripsRepository.save(userTrips);
}


}






}
121 changes: 121 additions & 0 deletions src/main/java/com/example/wayz/Utils/CreateDbData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.example.wayz.Utils;

import com.example.wayz.Model.*;
import com.example.wayz.Repository.*;
import com.example.wayz.Service.UserTripsService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Set;

@Component
@RequiredArgsConstructor
public class CreateDbData {

private final StudentRepository studentRepository;

private final AuthRepository authRepository;
private final DriverRepository driverRepository;
private final CarRepository carRepository;
private final DriverTripsRepository driverTripsRepository;
private final StudentTripsRepository studentTripsRepository;
private final UserTripsService userTripsService;

// @Bean

public void addData() {

int run = 0; // edit this manually to fill the database

if(run == 1) {

int STUDENT_COUNT = 160;
int DRIVER_COUNT = 32;

String[] names = new String[] {"saleh", "saud", "bakr", "abdullah"};


for (int i = 2; i < STUDENT_COUNT; i+=2) {

User u1 = new User();
u1.setUsername("050123456" + i);
u1.setPassword((new BCryptPasswordEncoder()).encode("1"));
u1.setRole("STUDENT");

Student s1 = new Student(null, "st1", "noura", "google.com", 100, u1, Set.of(), Set.of(), Set.of());
studentRepository.save(s1);



TimeRange[] morningTimeRanges = new TimeRange[3];

morningTimeRanges[0] = new TimeRange("08:00 - 08:29", LocalDateTime.now().withHour(6).withMinute(0), LocalDateTime.now().withHour(10).withMinute(29));
morningTimeRanges[1] = new TimeRange("10:30 - 12:29", LocalDateTime.now().withHour(10).withMinute(30), LocalDateTime.now().withHour(12).withMinute(29));
morningTimeRanges[2] = new TimeRange("12:30 - 14:29", LocalDateTime.now().withHour(12).withMinute(30), LocalDateTime.now().withHour(14).withMinute(29));


int index = (int) Math.floor(Math.random() * morningTimeRanges.length);

// student trips
StudentTrips studentTrips = new StudentTrips(null, 1, "going", morningTimeRanges[index].getStart(), null, null);

studentTripsRepository.save(studentTrips);
}

for (int i = 1; i < DRIVER_COUNT; i+=2) {

User u2 = new User();
u2.setUsername("050123456" + i);
u2.setPassword((new BCryptPasswordEncoder()).encode("1"));
u2.setRole("DRIVER");

Driver driver = new Driver();
driver.setUser(u2);
driver.setStatus("approved");

int index = (int) Math.floor((Math.random() * names.length));
driver.setName(names[index]);

driverRepository.save(driver);


Car car = new Car(null, 2019, 10, "abcdefg", "sedan", driver);
carRepository.save(car);


TimeRange[] morningTimeRanges = new TimeRange[3];

morningTimeRanges[0] = new TimeRange("06:00 - 10:29", LocalDateTime.now().withHour(6).withMinute(0), LocalDateTime.now().withHour(10).withMinute(29));
morningTimeRanges[1] = new TimeRange("10:30 - 12:29", LocalDateTime.now().withHour(10).withMinute(30), LocalDateTime.now().withHour(12).withMinute(29));
morningTimeRanges[2] = new TimeRange("12:30 - 14:29", LocalDateTime.now().withHour(12).withMinute(30), LocalDateTime.now().withHour(14).withMinute(29));


for (TimeRange timeRange : morningTimeRanges) {
DriverTrips driverTrips = new DriverTrips(null, "going", timeRange.getStart(), timeRange.getEnd(), driver);
driverTripsRepository.save(driverTrips);
}



}



// user trips
userTripsService.createUserTrips();






}




}
}

0 comments on commit 4c174e9

Please sign in to comment.