Skip to content

Commit

Permalink
Merge pull request Tuwaiq-Team-4#93 from Tuwaiq-Team-4/abdullah
Browse files Browse the repository at this point in the history
add user trips function
  • Loading branch information
W2AlharbiMe authored Sep 11, 2023
2 parents 94f9241 + f0d3a88 commit 9d8f6c1
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import com.example.wayz.DTO.DriverDTO;
import com.example.wayz.DTO.StudentDTO;
import com.example.wayz.Model.User;
import com.example.wayz.Service.AuthService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -29,13 +31,14 @@ public ResponseEntity<String> registerStudent(@RequestBody @Valid StudentDTO stu
@PostMapping("/register/driver")
public ResponseEntity<String> registerDriver(
@RequestParam("data") String data,
@RequestParam("car") String car,
@RequestParam("id") MultipartFile id,
@RequestParam("license") MultipartFile license,
@RequestParam("registration") MultipartFile registration,
@RequestParam("pic") MultipartFile pic
) throws IOException
{
authService.registerDriver(data, id, license, registration, pic);
authService.registerDriver(data, car, id, license, registration, pic);
return ResponseEntity.status(200).body("user registered");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public ResponseEntity getAllStudentTrips(@AuthenticationPrincipal User user) {
}

@PostMapping("/add")
public ResponseEntity addStudentTrip(@RequestBody @Valid StudentTrips studentTrip) {
studentTripsService.createStudentTrip(studentTrip);
public ResponseEntity addStudentTrip(@AuthenticationPrincipal User user, @RequestBody @Valid StudentTrips studentTrip) {
studentTripsService.createStudentTrip(studentTrip, user);
return ResponseEntity.status(200).body("Student Trips added successfully");
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/example/wayz/DTO/CarDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class CarDTO {
@Pattern(regexp = "^[a-zA-Z]*$", message = "Please enter only letters")
String plateLetter;

// { "seats": 4, "model": 2020, "plateLetter": "abc", "plateNumbers": 1234, "type": "hatchback" }

@NotEmpty(message = "Plate numbers cannot be empty")
@Pattern(regexp = "\\d+", message = "Please enter only numbers")
String plateNumbers;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/example/wayz/Model/DriverTrips.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class DriverTrips {
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime endTime;

private String shift;


// Relation

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/example/wayz/Model/StudentTrips.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public class StudentTrips {
private LocalDateTime timestamp;


@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime endTime;





@ManyToOne
@JoinColumn(name = "student_id", referencedColumnName = "user_id")
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/example/wayz/Model/UserTrips.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class UserTrips {
@JsonIgnore
private Driver driver;

@Column(nullable = false)
private String status;



}
10 changes: 10 additions & 0 deletions src/main/java/com/example/wayz/Repository/DriverRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DriverRepository extends JpaRepository<Driver,Integer> {

Driver findDriverById(Integer id);

//
// @Query("SELECT d FROM driver d ORDER BY ASC d.id LIMIT 5")
// List<Driver> findFirstFiveDrivers();
//
//
// @Query("SELECT d FROM driver d WHERE d.id > ?1 ORDER BY d.id LIMIT 5")
// List<Driver> findNextFiveDrivers(Integer id);

// @Query("select count (c) from driver c where c.Reports='approved'")
// Integer countApproved();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public interface DriverTripsRepository extends JpaRepository<DriverTrips, Intege
List<DriverTrips> findAllByDriverId(Integer driverId);



@Query("SELECT dt.driver FROM driver_trips dt WHERE dt.shift = ?1 AND dt.startTime BETWEEN dt.startTime AND dt.endTime ORDER BY dt.id ASC LIMIT 5")
List<Driver> findFirstFiveDriversInShift(String shift);


@Query("SELECT dt.driver FROM driver_trips dt WHERE dt.shift = ?1 AND dt.startTime BETWEEN dt.startTime AND dt.endTime AND dt.id > ?1 ORDER BY dt.id ASC LIMIT 5")
List<Driver> findNextFiveDrivers(Integer id);


@Query("select c.driver.name as tripsCount from driver_trips c order by count(c.driver.id) DESC limit 10")
List<String> findTheMostDriver();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public interface StudentTripsRepository extends JpaRepository<StudentTrips, Inte
@Query("select count (c) from StudentTrips c where c.student.id=?1 ")
Integer tripsNumber(Integer id);



@Query("SELECT st FROM StudentTrips st WHERE st.type = ?1 and st.timestamp between st.timestamp and st.endTime and st.userTrips = null ORDER BY st.id ASC LIMIT 10")
List<StudentTrips> findAllStudentTripsBetweenTimestampAndEndTime(String type);

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.example.wayz.Repository;

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

import java.util.List;

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



@Query("SELECT ut.driver FROM user_trips ut WHERE ut.status = 'completed'")
List<Driver> findAllFreeDrivers();


}
21 changes: 20 additions & 1 deletion src/main/java/com/example/wayz/Service/AuthService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.example.wayz.Service;

import com.example.wayz.DTO.CarDTO;
import com.example.wayz.DTO.DriverDTO;
import com.example.wayz.DTO.StudentDTO;
import com.example.wayz.Model.Car;
import com.example.wayz.Model.Driver;
import com.example.wayz.Model.Student;
import com.example.wayz.Model.User;
import com.example.wayz.Repository.AuthRepository;
import com.example.wayz.Repository.CarRepository;
import com.example.wayz.Repository.DriverRepository;
import com.example.wayz.Repository.StudentRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -25,6 +28,7 @@ public class AuthService {
private final DriverRepository driverRepository;
private final AuthRepository authRepository;
private final FileService fileService;
private final CarRepository carRepository;

public void registerStudent(StudentDTO studentDto) {
User user = new User(null, studentDto.getUsername(), studentDto.getPassword(), "STUDENT", null, null, null);
Expand All @@ -36,9 +40,10 @@ public void registerStudent(StudentDTO studentDto) {
studentRepository.save(student);
}

public void registerDriver(String data, MultipartFile id, MultipartFile license, MultipartFile registration, MultipartFile pic) throws IOException {
public void registerDriver(String data, String carStr, MultipartFile id, MultipartFile license, MultipartFile registration, MultipartFile pic) throws IOException {

DriverDTO driverDTO = new ObjectMapper().readValue(data, DriverDTO.class);
CarDTO carDTO = new ObjectMapper().readValue(carStr, CarDTO.class);

User user = new User(null, driverDTO.getUsername(), driverDTO.getPassword(), "DRIVER", null, null, null);

Expand All @@ -50,10 +55,22 @@ public void registerDriver(String data, MultipartFile id, MultipartFile license,
driver.setName(driverDTO.getName());
driver.setUnCashedTrips(0);


String hash = new BCryptPasswordEncoder().encode(user.getPassword());
user.setPassword(hash);
driver = driverRepository.save(driver); // make sure to get the ID

Car car = new Car();
car.setSeats(carDTO.getSeats());
car.setType(carDTO.getType());

car.setPlate(carDTO.getCarPlate());
car.setModelYear(carDTO.getModel());

car.setDriver(driver);
carRepository.save(car);



// save driver files.
HashMap<String, MultipartFile> files = new HashMap<>(4) {{
Expand All @@ -65,6 +82,8 @@ public void registerDriver(String data, MultipartFile id, MultipartFile license,

fileService.uploadDriverDocuments(files, driver.getId());


// {"name":"abdullah", "password": "12345678910@nN", "username": "0512263921"}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public HashMap<String, DriverTrips> addDriverTrip(User user, DriverTripDTO drive

if(driverTripDTO.getShift().equalsIgnoreCase("morning")) {
timeRange = timeRanges[0][index];
driverTrips.setShift("morning");
}

if(driverTripDTO.getShift().equalsIgnoreCase("evening")) {
timeRange = timeRanges[1][index];
driverTrips.setShift("evening");
}

driverTrips.setStartTime(timeRange.getStart());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/example/wayz/Service/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class FileService {
private final DriverRepository driverRepository;
private final ReportRepository reportRepository;

private final String SERVER_FILES_FOLDER = "C:/Users/isaud/IdeaProjects/wayz/src/main/resources/users_files/";
// private final String SERVER_FILES_FOLDER = "C:/Users/isaud/IdeaProjects/wayz/src/main/resources/users_files/";

// private final String SERVER_FILES_FOLDER = "/home/alharbi/projects/toys/wayz/src/main/resources/users_files/";
private final String SERVER_FILES_FOLDER = "/home/alharbi/projects/toys/wayz/src/main/resources/users_files/";

//// record to put the file info and the file itself in one place I think it's more readable this way, plus we can return both from a function.
public record FileInfoRecord(MediaType mediaType, byte[] data) {
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/example/wayz/Service/StudentTripsService.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
package com.example.wayz.Service;

import com.example.wayz.Api.ApiException.ApiException;
import com.example.wayz.Model.Student;
import com.example.wayz.Model.StudentTrips;
import com.example.wayz.Model.User;
import com.example.wayz.Repository.AuthRepository;
import com.example.wayz.Repository.StudentRepository;
import com.example.wayz.Repository.StudentTripsRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class StudentTripsService {

private final StudentTripsRepository studentTripsRepository;
private final StudentRepository studentRepository;

public List<StudentTrips> getAllStudentTrips() {
return studentTripsRepository.findAll();
}

public void createStudentTrip(StudentTrips studentTrip) {
public void createStudentTrip(StudentTrips studentTrip, User user) {
studentTrip.setEndTime(studentTrip.getTimestamp().plusMinutes(30));
Student student = studentRepository.findStudentById(user.getId());

if(student.getStudentTrips() == null) {
student.setStudentTrips(Set.of());
studentRepository.save(student);
}

studentTrip.setStudent(student);

studentTripsRepository.save(studentTrip);
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/example/wayz/Service/UserDetailsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
throw new ApiException("Wrong username or password.");
}

if(user.getRole().equalsIgnoreCase("DRIVER")) {
driverValidation(user);
}


return user;
}


private void driverValidation(User user) {
if (driverRepository.findDriverById(user.getId()).getStatus().equalsIgnoreCase("pending")) {
throw new ApiException("You cannot login your account is still pending.");
} else if (driverRepository.findDriverById(user.getId()).getStatus().equalsIgnoreCase("closed")) {
Expand All @@ -39,7 +49,5 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
driverService.closeDriver(user.getId());
throw new ApiException("YOU ARE FIRED, GET OUT.");
}

return user;
}
}
Loading

0 comments on commit 9d8f6c1

Please sign in to comment.