├── .gitattributes ├── .DS_Store ├── README.md ├── src ├── main │ ├── java │ │ └── com │ │ │ └── codingshuttle │ │ │ └── youtube │ │ │ └── hospitalManagement │ │ │ ├── entity │ │ │ ├── type │ │ │ │ ├── RoleType.java │ │ │ │ ├── AuthProviderType.java │ │ │ │ ├── BloodGroupType.java │ │ │ │ └── PermissionType.java │ │ │ ├── Appointment.java │ │ │ ├── Department.java │ │ │ ├── Insurance.java │ │ │ ├── Doctor.java │ │ │ ├── Patient.java │ │ │ └── User.java │ │ │ ├── dto │ │ │ ├── OnboardDoctorRequestDto.java │ │ │ ├── LoginResponseDto.java │ │ │ ├── LoginRequestDto.java │ │ │ ├── CreateAppointmentRequestDto.java │ │ │ ├── SignupResponseDto.java │ │ │ ├── AppointmentResponseDto.java │ │ │ ├── DoctorResponseDto.java │ │ │ ├── PatientResponseDto.java │ │ │ ├── BloodGroupCountResponseEntity.java │ │ │ └── SignUpRequestDto.java │ │ │ ├── repository │ │ │ ├── DoctorRepository.java │ │ │ ├── InsuranceRepository.java │ │ │ ├── DepartmentRepository.java │ │ │ ├── AppointmentRepository.java │ │ │ ├── UserRepository.java │ │ │ └── PatientRepository.java │ │ │ ├── HospitalManagementApplication.java │ │ │ ├── error │ │ │ ├── ApiError.java │ │ │ └── GlobalExceptionHandler.java │ │ │ ├── security │ │ │ ├── CustomUserDetailsService.java │ │ │ ├── RolePermissionMapping.java │ │ │ ├── OAuth2SuccessHandler.java │ │ │ ├── JwtAuthFilter.java │ │ │ ├── AuthUtil.java │ │ │ ├── WebSecurityConfig.java │ │ │ └── AuthService.java │ │ │ ├── controller │ │ │ ├── HospitalController.java │ │ │ ├── DoctorController.java │ │ │ ├── AuthController.java │ │ │ ├── PatientController.java │ │ │ └── AdminController.java │ │ │ ├── service │ │ │ ├── PatientService.java │ │ │ ├── InsuranceService.java │ │ │ ├── DoctorService.java │ │ │ └── AppointmentService.java │ │ │ └── config │ │ │ └── AppConfig.java │ └── resources │ │ ├── application.properties │ │ ├── application.yml │ │ └── data.sql └── test │ └── java │ └── com │ └── codingshuttle │ └── youtube │ └── hospitalManagement │ ├── HospitalManagementApplicationTests.java │ ├── InsuranceTests.java │ └── PatientTests.java ├── .gitignore ├── .mvn └── wrapper │ └── maven-wrapper.properties ├── pom.xml ├── mvnw.cmd └── mvnw /.gitattributes: -------------------------------------------------------------------------------- 1 | /mvnw text eol=lf 2 | *.cmd text eol=crlf 3 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anuj-Kumar-Sharma/spring-boot-data-jpa-hospital-management-system/HEAD/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-data-jpa-hospital-management-system 2 | Spring Boot Project to teach Spring Boot Data JPA with real-world Project (Hospital Management System) 3 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/type/RoleType.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity.type; 2 | 3 | public enum RoleType { 4 | ADMIN, 5 | DOCTOR, 6 | PATIENT 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/type/AuthProviderType.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity.type; 2 | 3 | public enum AuthProviderType { 4 | GOOGLE, 5 | GITHUB, 6 | FACEBOOK, 7 | TWITTER, 8 | EMAIL 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/OnboardDoctorRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class OnboardDoctorRequestDto { 7 | private Long userId; 8 | private String specialization; 9 | private String name; 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/type/BloodGroupType.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity.type; 2 | 3 | public enum BloodGroupType { 4 | A_POSITIVE, 5 | A_NEGATIVE, 6 | B_POSITIVE, 7 | B_NEGATIVE, 8 | AB_POSITIVE, 9 | AB_NEGATIVE, 10 | O_POSITIVE, 11 | O_NEGATIVE 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/repository/DoctorRepository.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.repository; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.Doctor; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface DoctorRepository extends JpaRepository { 7 | } -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/repository/InsuranceRepository.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.repository; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.Insurance; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface InsuranceRepository extends JpaRepository { 7 | } -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/repository/DepartmentRepository.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.repository; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.Department; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface DepartmentRepository extends JpaRepository { 7 | } -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/repository/AppointmentRepository.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.repository; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.Appointment; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface AppointmentRepository extends JpaRepository { 7 | } -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/LoginResponseDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | public class LoginResponseDto { 11 | String jwt; 12 | Long userId; 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/codingshuttle/youtube/hospitalManagement/HospitalManagementApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class HospitalManagementApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/LoginRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | public class LoginRequestDto { 11 | private String username; 12 | private String password; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/CreateAppointmentRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import lombok.Data; 4 | 5 | import java.time.LocalDateTime; 6 | 7 | @Data 8 | public class CreateAppointmentRequestDto { 9 | private Long doctorId; 10 | private Long patientId; 11 | private LocalDateTime appointmentTime; 12 | private String reason; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/SignupResponseDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | @NoArgsConstructor 11 | public class SignupResponseDto { 12 | private Long id; 13 | private String username; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/AppointmentResponseDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import lombok.Data; 4 | 5 | import java.time.LocalDateTime; 6 | 7 | @Data 8 | public class AppointmentResponseDto { 9 | private Long id; 10 | private LocalDateTime appointmentTime; 11 | private String reason; 12 | private DoctorResponseDto doctor; 13 | // private PatientResponseDto patient; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/DoctorResponseDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | public class DoctorResponseDto { 11 | private Long id; 12 | private String name; 13 | private String specialization; 14 | private String email; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/HospitalManagementApplication.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class HospitalManagementApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(HospitalManagementApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/PatientResponseDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.type.BloodGroupType; 4 | import lombok.Data; 5 | 6 | import java.time.LocalDate; 7 | 8 | @Data 9 | public class PatientResponseDto { 10 | private Long id; 11 | private String name; 12 | private String gender; 13 | private LocalDate birthDate; 14 | private BloodGroupType bloodGroup; 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | .mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/BloodGroupCountResponseEntity.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.type.BloodGroupType; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import lombok.ToString; 8 | 9 | @Data 10 | @AllArgsConstructor 11 | @NoArgsConstructor 12 | @ToString 13 | public class BloodGroupCountResponseEntity { 14 | 15 | private BloodGroupType bloodGroupType; 16 | private Long count; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/dto/SignUpRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.dto; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.type.RoleType; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.util.HashSet; 9 | import java.util.Set; 10 | 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class SignUpRequestDto { 15 | private String username; 16 | private String password; 17 | private String name; 18 | 19 | private Set roles = new HashSet<>(); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.repository; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.User; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.type.AuthProviderType; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | import java.util.Optional; 8 | 9 | 10 | public interface UserRepository extends JpaRepository { 11 | 12 | Optional findByUsername(String username); 13 | 14 | Optional findByProviderIdAndProviderType(String providerId, AuthProviderType providerType); 15 | } -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/type/PermissionType.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity.type; 2 | 3 | import lombok.Getter; 4 | import lombok.RequiredArgsConstructor; 5 | 6 | @Getter 7 | @RequiredArgsConstructor 8 | public enum PermissionType { 9 | PATIENT_READ("patient:read"), 10 | PATIENT_WRITE("patient:write"), 11 | APPOINTMENT_READ("appointment:read"), 12 | APPOINTMENT_WRITE("appointment:write"), 13 | APPOINTMENT_DELETE("appointment:delete"), 14 | USER_MANAGE("user:manage"), // For admin tasks 15 | REPORT_VIEW("report:view"); 16 | 17 | private final String permission; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/error/ApiError.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.error; 2 | 3 | import lombok.Data; 4 | import org.springframework.http.HttpStatus; 5 | 6 | import java.time.LocalDateTime; 7 | 8 | @Data 9 | public class ApiError { 10 | 11 | private LocalDateTime timeStamp; 12 | private String error; 13 | private HttpStatus statusCode; 14 | 15 | public ApiError() { 16 | this.timeStamp = LocalDateTime.now(); 17 | } 18 | 19 | public ApiError(String error, HttpStatus statusCode) { 20 | this(); 21 | this.error = error; 22 | this.statusCode = statusCode; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=hospitalManagement 2 | 3 | #DB configuration 4 | spring.datasource.url=jdbc:postgresql://localhost:5432/hospitalDB 5 | spring.datasource.username= 6 | spring.datasource.password= 7 | 8 | server.servlet.context-path=/api/v1 9 | 10 | spring.jpa.hibernate.ddl-auto=create 11 | spring.jpa.show-sql=true 12 | #spring.jpa.properties.hibernate.format_sql=false 13 | 14 | #spring.jpa.defer-datasource-initialization=true 15 | #spring.sql.init.mode=always 16 | #spring.sql.init.data-locations=classpath:data.sql 17 | 18 | #security config 19 | #spring.security.user.name=anuj 20 | #spring.security.user.password=pass 21 | jwt.secretKey=asdfhads9f67as98dfyaisudhfa98s67dfy89aishudfuays89dfyasi8df7asdf87987g98a7sg986a89sdf7y -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/security/CustomUserDetailsService.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.security; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.repository.UserRepository; 4 | import lombok.RequiredArgsConstructor; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | import org.springframework.security.core.userdetails.UserDetailsService; 7 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 8 | import org.springframework.stereotype.Service; 9 | 10 | @Service 11 | @RequiredArgsConstructor 12 | public class CustomUserDetailsService implements UserDetailsService { 13 | 14 | private final UserRepository userRepository; 15 | 16 | @Override 17 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 18 | return userRepository.findByUsername(username).orElseThrow(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/Appointment.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.*; 5 | 6 | import java.time.LocalDateTime; 7 | 8 | @Entity 9 | @Getter 10 | @Setter 11 | @Builder 12 | @NoArgsConstructor 13 | @AllArgsConstructor 14 | @ToString 15 | public class Appointment { 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.IDENTITY) 18 | private Long id; 19 | 20 | @Column(nullable = false) 21 | private LocalDateTime appointmentTime; 22 | 23 | @Column(length = 500) 24 | private String reason; 25 | 26 | @ManyToOne 27 | @ToString.Exclude 28 | @JoinColumn(name = "patient_id", nullable = false) // patient is required and not nullable 29 | private Patient patient; 30 | 31 | @ManyToOne(fetch = FetchType.LAZY) 32 | @ToString.Exclude 33 | @JoinColumn(nullable = false) 34 | private Doctor doctor; 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/controller/HospitalController.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.controller; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.DoctorResponseDto; 4 | import com.codingshuttle.youtube.hospitalManagement.service.DoctorService; 5 | import lombok.RequiredArgsConstructor; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequestMapping("/public") 15 | @RequiredArgsConstructor 16 | public class HospitalController { 17 | 18 | private final DoctorService doctorService; 19 | 20 | @GetMapping("/doctors") 21 | public ResponseEntity> getAllDoctors() { 22 | return ResponseEntity.ok(doctorService.getAllDoctors()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | import java.util.HashSet; 10 | import java.util.Set; 11 | 12 | @Entity 13 | @Getter 14 | @Setter 15 | @NoArgsConstructor 16 | @AllArgsConstructor 17 | public class Department { 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | private Long id; 21 | 22 | @Column(nullable = false, unique = true, length = 100) 23 | private String name; 24 | 25 | @OneToOne 26 | private Doctor headDoctor; 27 | 28 | @ManyToMany 29 | @JoinTable( 30 | name = "my_dpt_doctors", 31 | joinColumns = @JoinColumn(name = "dpt_id"), 32 | inverseJoinColumns = @JoinColumn(name = "doctor_id") 33 | ) 34 | private Set doctors = new HashSet<>(); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | security.oauth2.client: 3 | provider: 4 | twitter: 5 | authorization-uri: https://twitter.com/i/oauth2/authorize 6 | token-uri: https://api.twitter.com/2/oauth2/token 7 | user-info-uri: https://api.twitter.com/2/users/me 8 | user-name-attribute: id 9 | 10 | registration: 11 | google: 12 | client-id: 204915941072-fckl8bu6ouj5lbq3q35ebb8k8hltns50.apps.googleusercontent.com 13 | client-secret: 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 14 | github: 15 | client-id: Ov23liDNZOj6fRNAyIr7 16 | client-secret: 7348245f6010707b73a9242b1fff741cf7bfa953 17 | twitter: 18 | client-id: asdlfjasldf 19 | client-secret: asldfjalsdfj 20 | redirect-uri: "{baseUrl}/login/oauth2/code/twitter" 21 | authorization-grant-type: authorization_code 22 | scope: 23 | - tweet.read 24 | - users.read 25 | - offline.access 26 | client-name: Twitter 27 | provider: twitter -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | wrapperVersion=3.3.2 18 | distributionType=only-script 19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.10/apache-maven-3.9.10-bin.zip 20 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/Insurance.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.*; 5 | import org.hibernate.annotations.CreationTimestamp; 6 | 7 | import java.time.LocalDate; 8 | import java.time.LocalDateTime; 9 | 10 | @Entity 11 | @Getter 12 | @Setter 13 | @NoArgsConstructor 14 | @AllArgsConstructor 15 | @Builder 16 | public class Insurance { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | private Long id; 21 | 22 | @Column(nullable = false, unique = true, length = 50) 23 | private String policyNumber; 24 | 25 | @Column(nullable = false, length = 100) 26 | private String provider; 27 | 28 | @Column(nullable = false) 29 | private LocalDate validUntil; 30 | 31 | @CreationTimestamp 32 | @Column(nullable = false, updatable = false) 33 | private LocalDateTime createdAt; 34 | 35 | @OneToOne(mappedBy = "insurance") // inverse side 36 | private Patient patient; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/Doctor.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.*; 5 | 6 | import java.util.ArrayList; 7 | import java.util.HashSet; 8 | import java.util.List; 9 | import java.util.Set; 10 | 11 | @Entity 12 | @Getter 13 | @Setter 14 | @Builder 15 | @NoArgsConstructor 16 | @AllArgsConstructor 17 | public class Doctor { 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | private Long id; 21 | 22 | @OneToOne 23 | @MapsId 24 | private User user; 25 | 26 | @Column(nullable = false, length = 100) 27 | private String name; 28 | 29 | @Column(length = 100) 30 | private String specialization; 31 | 32 | @Column(unique = true, length = 100) 33 | private String email; 34 | 35 | @ManyToMany(mappedBy = "doctors") 36 | private Set departments = new HashSet<>(); 37 | 38 | @OneToMany(mappedBy = "doctor") 39 | private List appointments = new ArrayList<>(); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO patient (name, gender, birth_date, email, blood_group) 2 | VALUES 3 | ('Aarav Sharma', 'MALE', '1990-05-10', 'aarav.sharma@example.com', 'O_POSITIVE'), 4 | ('Diya Patel', 'FEMALE', '1995-08-20', 'diya.patel@example.com', 'A_POSITIVE'), 5 | ('Dishant Verma', 'MALE', '1988-03-15', 'dishant.verma@example.com', 'A_POSITIVE'), 6 | ('Neha Iyer', 'FEMALE', '1992-12-01', 'neha.iyer@example.com', 'AB_POSITIVE'), 7 | ('Kabir Singh', 'MALE', '1993-07-11', 'kabir.singh@example.com', 'O_POSITIVE'); 8 | 9 | INSERT INTO doctor (name, specialization, email) 10 | VALUES 11 | ('Dr. Rakesh Mehta', 'Cardiology', 'rakesh.mehta@example.com'), 12 | ('Dr. Sneha Kapoor', 'Dermatology', 'sneha.kapoor@example.com'), 13 | ('Dr. Arjun Nair', 'Orthopedics', 'arjun.nair@example.com'); 14 | 15 | INSERT INTO appointment (appointment_time, reason, doctor_id, patient_id) 16 | VALUES 17 | ('2025-07-01 10:30:00', 'General Checkup', 1, 2), 18 | ('2025-07-02 11:00:00', 'Skin Rash', 2, 2), 19 | ('2025-07-03 09:45:00', 'Knee Pain', 3, 3), 20 | ('2025-07-04 14:00:00', 'Follow-up Visit', 1, 1), 21 | ('2025-07-05 16:15:00', 'Consultation', 1, 4), 22 | ('2025-07-06 08:30:00', 'Allergy Treatment', 2, 5); -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/controller/DoctorController.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.controller; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.AppointmentResponseDto; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.User; 5 | import com.codingshuttle.youtube.hospitalManagement.service.AppointmentService; 6 | import lombok.RequiredArgsConstructor; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.security.core.context.SecurityContextHolder; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | import java.util.List; 14 | 15 | @RestController 16 | @RequestMapping("/doctors") 17 | @RequiredArgsConstructor 18 | public class DoctorController { 19 | 20 | private final AppointmentService appointmentService; 21 | 22 | @GetMapping("/appointments") 23 | public ResponseEntity> getAllAppointmentsOfDoctor() { 24 | User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 25 | return ResponseEntity.ok(appointmentService.getAllAppointmentsOfDoctor(user.getId())); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/security/RolePermissionMapping.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.security; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.type.PermissionType; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.type.RoleType; 5 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 6 | 7 | import java.util.Map; 8 | import java.util.Set; 9 | import java.util.stream.Collectors; 10 | 11 | import static com.codingshuttle.youtube.hospitalManagement.entity.type.PermissionType.*; 12 | import static com.codingshuttle.youtube.hospitalManagement.entity.type.RoleType.*; 13 | 14 | public class RolePermissionMapping { 15 | 16 | private static final Map> map = Map.of( 17 | PATIENT, Set.of(PATIENT_READ, APPOINTMENT_READ, APPOINTMENT_WRITE), 18 | DOCTOR, Set.of(APPOINTMENT_DELETE, APPOINTMENT_WRITE, APPOINTMENT_READ, PATIENT_READ), 19 | ADMIN, Set.of(PATIENT_READ, PATIENT_WRITE, APPOINTMENT_READ, APPOINTMENT_WRITE, APPOINTMENT_DELETE, USER_MANAGE, REPORT_VIEW) 20 | ); 21 | 22 | public static Set getAuthoritiesForRole(RoleType role) { 23 | return map.get(role).stream() 24 | .map(permission -> new SimpleGrantedAuthority(permission.getPermission())) 25 | .collect(Collectors.toSet()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/controller/AuthController.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.controller; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.LoginRequestDto; 4 | import com.codingshuttle.youtube.hospitalManagement.dto.LoginResponseDto; 5 | import com.codingshuttle.youtube.hospitalManagement.dto.SignUpRequestDto; 6 | import com.codingshuttle.youtube.hospitalManagement.dto.SignupResponseDto; 7 | import com.codingshuttle.youtube.hospitalManagement.security.AuthService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.PostMapping; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | @RestController 16 | @RequestMapping("/auth") 17 | @RequiredArgsConstructor 18 | public class AuthController { 19 | 20 | private final AuthService authService; 21 | 22 | @PostMapping("/login") 23 | public ResponseEntity login(@RequestBody LoginRequestDto loginRequestDto) { 24 | return ResponseEntity.ok(authService.login(loginRequestDto)); 25 | } 26 | 27 | @PostMapping("/signup") 28 | public ResponseEntity signup(@RequestBody SignUpRequestDto signupRequestDto) { 29 | return ResponseEntity.ok(authService.signup(signupRequestDto)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/controller/PatientController.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.controller; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.AppointmentResponseDto; 4 | import com.codingshuttle.youtube.hospitalManagement.dto.CreateAppointmentRequestDto; 5 | import com.codingshuttle.youtube.hospitalManagement.dto.PatientResponseDto; 6 | import com.codingshuttle.youtube.hospitalManagement.service.AppointmentService; 7 | import com.codingshuttle.youtube.hospitalManagement.service.PatientService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.http.HttpStatus; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | @RestController 14 | @RequestMapping("/patients") 15 | @RequiredArgsConstructor 16 | public class PatientController { 17 | 18 | private final PatientService patientService; 19 | private final AppointmentService appointmentService; 20 | 21 | @PostMapping("/appointments") 22 | public ResponseEntity createNewAppointment(@RequestBody CreateAppointmentRequestDto createAppointmentRequestDto) { 23 | return ResponseEntity.status(HttpStatus.CREATED).body(appointmentService.createNewAppointment(createAppointmentRequestDto)); 24 | } 25 | 26 | @GetMapping("/profile") 27 | private ResponseEntity getPatientProfile() { 28 | Long patientId = 4L; 29 | return ResponseEntity.ok(patientService.getPatientById(patientId)); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/service/PatientService.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.service; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.PatientResponseDto; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.Patient; 5 | import com.codingshuttle.youtube.hospitalManagement.repository.PatientRepository; 6 | import jakarta.persistence.EntityNotFoundException; 7 | import jakarta.transaction.Transactional; 8 | import lombok.RequiredArgsConstructor; 9 | import org.modelmapper.ModelMapper; 10 | import org.springframework.data.domain.PageRequest; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.util.List; 14 | import java.util.stream.Collectors; 15 | 16 | @Service 17 | @RequiredArgsConstructor 18 | public class PatientService { 19 | 20 | private final PatientRepository patientRepository; 21 | private final ModelMapper modelMapper; 22 | 23 | @Transactional 24 | public PatientResponseDto getPatientById(Long patientId) { 25 | Patient patient = patientRepository.findById(patientId).orElseThrow(() -> new EntityNotFoundException("Patient Not " + 26 | "Found with id: " + patientId)); 27 | return modelMapper.map(patient, PatientResponseDto.class); 28 | } 29 | 30 | public List getAllPatients(Integer pageNumber, Integer pageSize) { 31 | return patientRepository.findAllPatients(PageRequest.of(pageNumber, pageSize)) 32 | .stream() 33 | .map(patient -> modelMapper.map(patient, PatientResponseDto.class)) 34 | .collect(Collectors.toList()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/controller/AdminController.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.controller; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.DoctorResponseDto; 4 | import com.codingshuttle.youtube.hospitalManagement.dto.OnboardDoctorRequestDto; 5 | import com.codingshuttle.youtube.hospitalManagement.dto.PatientResponseDto; 6 | import com.codingshuttle.youtube.hospitalManagement.service.DoctorService; 7 | import com.codingshuttle.youtube.hospitalManagement.service.PatientService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.http.HttpStatus; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | import java.util.List; 14 | 15 | @RestController 16 | @RequestMapping("/admin") 17 | @RequiredArgsConstructor 18 | public class AdminController { 19 | 20 | private final PatientService patientService; 21 | private final DoctorService doctorService; 22 | 23 | @GetMapping("/patients") 24 | public ResponseEntity> getAllPatients( 25 | @RequestParam(value = "page", defaultValue = "0") Integer pageNumber, 26 | @RequestParam(value = "size", defaultValue = "10") Integer pageSize 27 | ) { 28 | return ResponseEntity.ok(patientService.getAllPatients(pageNumber, pageSize)); 29 | } 30 | 31 | @PostMapping("/onBoardNewDoctor") 32 | public ResponseEntity onBoardNewDoctor(@RequestBody OnboardDoctorRequestDto onboardDoctorRequestDto) { 33 | return ResponseEntity.status(HttpStatus.CREATED).body(doctorService.onBoardNewDoctor(onboardDoctorRequestDto)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/service/InsuranceService.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.service; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.Insurance; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.Patient; 5 | import com.codingshuttle.youtube.hospitalManagement.repository.InsuranceRepository; 6 | import com.codingshuttle.youtube.hospitalManagement.repository.PatientRepository; 7 | import jakarta.persistence.EntityNotFoundException; 8 | import jakarta.transaction.Transactional; 9 | import lombok.RequiredArgsConstructor; 10 | import org.springframework.stereotype.Service; 11 | 12 | @Service 13 | @RequiredArgsConstructor 14 | public class InsuranceService { 15 | 16 | private final InsuranceRepository insuranceRepository; 17 | private final PatientRepository patientRepository; 18 | 19 | @Transactional 20 | public Patient assignInsuranceToPatient(Insurance insurance, Long patientId) { 21 | Patient patient = patientRepository.findById(patientId) 22 | .orElseThrow(() -> new EntityNotFoundException("Patient not found with id: " + patientId)); 23 | 24 | patient.setInsurance(insurance); 25 | insurance.setPatient(patient); // bidirectional consistency maintainence 26 | 27 | return patient; 28 | } 29 | 30 | @Transactional 31 | public Patient disaccociateInsuranceFromPatient(Long patientId) { 32 | Patient patient = patientRepository.findById(patientId) 33 | .orElseThrow(() -> new EntityNotFoundException("Patient not found with id: " + patientId)); 34 | 35 | patient.setInsurance(null); 36 | return patient; 37 | } 38 | 39 | // HW 40 | //Create three appointment for a patient and then delete Patient 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.config; 2 | 3 | import org.modelmapper.ModelMapper; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.authentication.AuthenticationManager; 7 | import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; 8 | import org.springframework.security.core.userdetails.User; 9 | import org.springframework.security.core.userdetails.UserDetails; 10 | import org.springframework.security.core.userdetails.UserDetailsService; 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12 | import org.springframework.security.crypto.password.PasswordEncoder; 13 | import org.springframework.security.provisioning.InMemoryUserDetailsManager; 14 | 15 | @Configuration 16 | public class AppConfig { 17 | 18 | @Bean 19 | public ModelMapper modelMapper() { 20 | return new ModelMapper(); 21 | } 22 | 23 | @Bean 24 | public PasswordEncoder passwordEncoder() { 25 | return new BCryptPasswordEncoder(); 26 | } 27 | 28 | @Bean 29 | public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { 30 | return configuration.getAuthenticationManager(); 31 | } 32 | 33 | // @Bean 34 | UserDetailsService userDetailsService() { 35 | UserDetails user1 = User.withUsername("admin") 36 | .password(passwordEncoder().encode("pass")) 37 | .roles("ADMIN") 38 | .build(); 39 | 40 | UserDetails user2 = User.withUsername("patient") 41 | .password(passwordEncoder().encode("pass")) 42 | .roles("PATIENT") 43 | .build(); 44 | 45 | return new InMemoryUserDetailsManager(user1, user2); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/Patient.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.type.BloodGroupType; 4 | import jakarta.persistence.*; 5 | import lombok.*; 6 | import org.hibernate.annotations.CreationTimestamp; 7 | 8 | import java.time.LocalDate; 9 | import java.time.LocalDateTime; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | @Entity 14 | @ToString 15 | @Getter 16 | @Setter 17 | @Table( 18 | name = "patient", 19 | uniqueConstraints = { 20 | // @UniqueConstraint(name = "unique_patient_email", columnNames = {"email"}), 21 | @UniqueConstraint(name = "unique_patient_name_birthdate", columnNames = {"name", "birthDate"}) 22 | }, 23 | indexes = { 24 | @Index(name = "idx_patient_birth_date", columnList = "birthDate") 25 | } 26 | ) 27 | @AllArgsConstructor 28 | @NoArgsConstructor 29 | @Builder 30 | public class Patient { 31 | 32 | @Id 33 | @GeneratedValue(strategy = GenerationType.IDENTITY) 34 | private Long id; 35 | 36 | @Column(nullable = false, length = 40) 37 | private String name; 38 | 39 | // @ToString.Exclude 40 | private LocalDate birthDate; 41 | 42 | @Column(unique = true, nullable = false) 43 | private String email; 44 | 45 | private String gender; 46 | 47 | @OneToOne 48 | @MapsId 49 | private User user; 50 | 51 | @CreationTimestamp 52 | @Column(updatable = false) 53 | private LocalDateTime createdAt; 54 | 55 | @Enumerated(EnumType.STRING) 56 | private BloodGroupType bloodGroup; 57 | 58 | @OneToOne(cascade = {CascadeType.ALL}, orphanRemoval = true) 59 | @JoinColumn(name = "patient_insurance_id") // owning side 60 | private Insurance insurance; 61 | 62 | @OneToMany(mappedBy = "patient", cascade = {CascadeType.REMOVE}, orphanRemoval = true, fetch = FetchType.EAGER) 63 | private List appointments = new ArrayList<>(); 64 | } -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/security/OAuth2SuccessHandler.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.security; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.LoginResponseDto; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import jakarta.servlet.ServletException; 6 | import jakarta.servlet.http.HttpServletRequest; 7 | import jakarta.servlet.http.HttpServletResponse; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.http.MediaType; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.security.core.Authentication; 12 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; 13 | import org.springframework.security.oauth2.core.user.OAuth2User; 14 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler; 15 | import org.springframework.stereotype.Component; 16 | 17 | import java.io.IOException; 18 | 19 | @Component 20 | @RequiredArgsConstructor 21 | public class OAuth2SuccessHandler implements AuthenticationSuccessHandler { 22 | 23 | private final AuthService authService; 24 | private final ObjectMapper objectMapper; 25 | 26 | @Override 27 | public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 28 | 29 | OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) authentication; 30 | OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal(); 31 | 32 | String registrationId = token.getAuthorizedClientRegistrationId(); 33 | 34 | ResponseEntity loginResponse = authService.handleOAuth2LoginRequest(oAuth2User, 35 | registrationId); 36 | 37 | response.setStatus(loginResponse.getStatusCode().value()); 38 | response.setContentType(MediaType.APPLICATION_JSON_VALUE); 39 | response.getWriter().write(objectMapper.writeValueAsString(loginResponse.getBody())); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/codingshuttle/youtube/hospitalManagement/InsuranceTests.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.Appointment; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.Insurance; 5 | import com.codingshuttle.youtube.hospitalManagement.entity.Patient; 6 | import com.codingshuttle.youtube.hospitalManagement.service.AppointmentService; 7 | import com.codingshuttle.youtube.hospitalManagement.service.InsuranceService; 8 | import org.junit.jupiter.api.Test; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | 12 | import java.time.LocalDate; 13 | import java.time.LocalDateTime; 14 | 15 | @SpringBootTest 16 | public class InsuranceTests { 17 | 18 | @Autowired 19 | private InsuranceService insuranceService; 20 | 21 | @Autowired 22 | private AppointmentService appointmentService; 23 | 24 | @Test 25 | public void testInsurance() { 26 | Insurance insurance = Insurance.builder() 27 | .policyNumber("HDFC_1234") 28 | .provider("HDFC") 29 | .validUntil(LocalDate.of(2030, 12, 12)) 30 | .build(); 31 | 32 | Patient patient = insuranceService.assignInsuranceToPatient(insurance, 1L); 33 | 34 | System.out.println(patient); 35 | 36 | var newPatient = insuranceService.disaccociateInsuranceFromPatient(patient.getId()); 37 | 38 | System.out.println(newPatient); 39 | } 40 | 41 | 42 | @Test 43 | public void testCreateAppointment() { 44 | Appointment appointment = Appointment.builder() 45 | .appointmentTime(LocalDateTime.of(2025, 11, 1, 14, 0, 0)) 46 | .reason("Cancer") 47 | .build(); 48 | 49 | // var newAppointment = appointmentService.createNewAppointment(appointment, 1L, 2L); 50 | 51 | // System.out.println(newAppointment); 52 | 53 | // var updatedAppointment = appointmentService.reAssignAppointmentToAnotherDoctor(newAppointment.getId(), 3L); 54 | 55 | // System.out.println(updatedAppointment); 56 | } 57 | } 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/service/DoctorService.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.service; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.DoctorResponseDto; 4 | import com.codingshuttle.youtube.hospitalManagement.dto.OnboardDoctorRequestDto; 5 | import com.codingshuttle.youtube.hospitalManagement.entity.Doctor; 6 | import com.codingshuttle.youtube.hospitalManagement.entity.User; 7 | import com.codingshuttle.youtube.hospitalManagement.entity.type.RoleType; 8 | import com.codingshuttle.youtube.hospitalManagement.repository.DoctorRepository; 9 | import com.codingshuttle.youtube.hospitalManagement.repository.UserRepository; 10 | import jakarta.transaction.Transactional; 11 | import lombok.RequiredArgsConstructor; 12 | import lombok.extern.slf4j.Slf4j; 13 | import org.modelmapper.ModelMapper; 14 | import org.springframework.stereotype.Service; 15 | 16 | import java.util.List; 17 | import java.util.stream.Collectors; 18 | 19 | @Service 20 | @RequiredArgsConstructor 21 | @Slf4j 22 | public class DoctorService { 23 | 24 | private final DoctorRepository doctorRepository; 25 | private final ModelMapper modelMapper; 26 | private final UserRepository userRepository; 27 | 28 | public List getAllDoctors() { 29 | return doctorRepository.findAll() 30 | .stream() 31 | .map(doctor -> modelMapper.map(doctor, DoctorResponseDto.class)) 32 | .collect(Collectors.toList()); 33 | } 34 | 35 | 36 | @Transactional 37 | public DoctorResponseDto onBoardNewDoctor(OnboardDoctorRequestDto onBoardDoctorRequestDto) { 38 | User user = userRepository.findById(onBoardDoctorRequestDto.getUserId()).orElseThrow(); 39 | 40 | if(doctorRepository.existsById(onBoardDoctorRequestDto.getUserId())) { 41 | throw new IllegalArgumentException("Already a doctor"); 42 | } 43 | 44 | Doctor doctor = Doctor.builder() 45 | .name(onBoardDoctorRequestDto.getName()) 46 | .specialization(onBoardDoctorRequestDto.getSpecialization()) 47 | .user(user) 48 | .build(); 49 | 50 | user.getRoles().add(RoleType.DOCTOR); 51 | 52 | return modelMapper.map(doctorRepository.save(doctor), DoctorResponseDto.class); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/repository/PatientRepository.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.repository; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.BloodGroupCountResponseEntity; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.Patient; 5 | import com.codingshuttle.youtube.hospitalManagement.entity.type.BloodGroupType; 6 | import jakarta.transaction.Transactional; 7 | import org.springframework.data.domain.Page; 8 | import org.springframework.data.domain.Pageable; 9 | import org.springframework.data.jpa.repository.JpaRepository; 10 | import org.springframework.data.jpa.repository.Modifying; 11 | import org.springframework.data.jpa.repository.Query; 12 | import org.springframework.data.repository.query.Param; 13 | 14 | import java.time.LocalDate; 15 | import java.util.List; 16 | 17 | public interface PatientRepository extends JpaRepository { 18 | Patient findByName(String name); 19 | 20 | List findByBirthDateOrEmail(LocalDate birthDate, String email); 21 | 22 | List findByBirthDateBetween(LocalDate startDate, LocalDate endDate); 23 | 24 | List findByNameContainingOrderByIdDesc(String query); 25 | 26 | @Query("SELECT p FROM Patient p where p.bloodGroup = ?1") 27 | List findByBloodGroup(@Param("bloodGroup") BloodGroupType bloodGroup); 28 | 29 | @Query("select p from Patient p where p.birthDate > :birthDate") 30 | List findByBornAfterDate(@Param("birthDate") LocalDate birthDate); 31 | 32 | @Query("select new com.codingshuttle.youtube.hospitalManagement.dto.BloodGroupCountResponseEntity(p.bloodGroup," + 33 | " Count(p)) from Patient p group by p.bloodGroup") 34 | // List countEachBloodGroupType(); 35 | List countEachBloodGroupType(); 36 | 37 | @Query(value = "select * from patient", nativeQuery = true) 38 | Page findAllPatients(Pageable pageable); 39 | 40 | @Transactional 41 | @Modifying 42 | @Query("UPDATE Patient p SET p.name = :name where p.id = :id") 43 | int updateNameWithId(@Param("name") String name, @Param("id") Long id); 44 | 45 | 46 | // @Query("SELECT p FROM Patient p LEFT JOIN FETCH p.appointments a LEFT JOIN FETCH a.doctor") 47 | @Query("SELECT p FROM Patient p LEFT JOIN FETCH p.appointments") 48 | List findAllPatientWithAppointment(); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.entity; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.type.AuthProviderType; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.type.RoleType; 5 | import com.codingshuttle.youtube.hospitalManagement.security.RolePermissionMapping; 6 | import jakarta.persistence.*; 7 | import lombok.*; 8 | import org.springframework.security.core.GrantedAuthority; 9 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 10 | import org.springframework.security.core.userdetails.UserDetails; 11 | 12 | import java.util.Collection; 13 | import java.util.HashSet; 14 | import java.util.List; 15 | import java.util.Set; 16 | import java.util.stream.Collectors; 17 | 18 | @Entity 19 | @Getter 20 | @Setter 21 | @AllArgsConstructor 22 | @NoArgsConstructor 23 | @Builder 24 | @Table(name = "app_user", indexes = { 25 | @Index(name = "idx_provider_id_provider_type", columnList = "providerId, providerType") 26 | }) 27 | public class User implements UserDetails { 28 | 29 | @Id 30 | @GeneratedValue(strategy = GenerationType.IDENTITY) 31 | private Long id; 32 | 33 | @JoinColumn(unique = true, nullable = false) 34 | private String username; 35 | private String password; 36 | 37 | private String providerId; 38 | 39 | @Enumerated(EnumType.STRING) 40 | private AuthProviderType providerType; 41 | 42 | @ElementCollection(fetch = FetchType.EAGER) 43 | @Enumerated(EnumType.STRING) 44 | Set roles = new HashSet<>(); 45 | 46 | @Override 47 | public Collection getAuthorities() { 48 | // return roles.stream() 49 | // .map(role -> new SimpleGrantedAuthority("ROLE_"+role.name())) 50 | // .collect(Collectors.toSet()); 51 | Set authorities = new HashSet<>(); 52 | roles.forEach( 53 | role -> { 54 | Set permissions = RolePermissionMapping.getAuthoritiesForRole(role); 55 | authorities.addAll(permissions); 56 | authorities.add(new SimpleGrantedAuthority("ROLE_"+role.name())); 57 | } 58 | ); 59 | return authorities; 60 | } 61 | } 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/error/GlobalExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.error; 2 | 3 | import io.jsonwebtoken.JwtException; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.security.access.AccessDeniedException; 7 | import org.springframework.security.core.AuthenticationException; 8 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 9 | import org.springframework.web.bind.annotation.ExceptionHandler; 10 | import org.springframework.web.bind.annotation.RestControllerAdvice; 11 | 12 | @RestControllerAdvice 13 | public class GlobalExceptionHandler { 14 | 15 | @ExceptionHandler(UsernameNotFoundException.class) 16 | public ResponseEntity handleUsernameNotFoundException(UsernameNotFoundException ex) { 17 | ApiError apiError = new ApiError("Username not found with username: "+ex.getMessage(), HttpStatus.NOT_FOUND); 18 | return new ResponseEntity<>(apiError, apiError.getStatusCode()); 19 | } 20 | 21 | @ExceptionHandler(AuthenticationException.class) 22 | public ResponseEntity handleAuthenticationException(AuthenticationException ex) { 23 | ApiError apiError = new ApiError("Authentication failed: " + ex.getMessage(), HttpStatus.UNAUTHORIZED); 24 | return new ResponseEntity<>(apiError, HttpStatus.UNAUTHORIZED); 25 | } 26 | 27 | @ExceptionHandler(JwtException.class) 28 | public ResponseEntity handleJwtException(JwtException ex) { 29 | ApiError apiError = new ApiError("Invalid JWT token: " + ex.getMessage(), HttpStatus.UNAUTHORIZED); 30 | return new ResponseEntity<>(apiError, HttpStatus.UNAUTHORIZED); 31 | } 32 | 33 | @ExceptionHandler(AccessDeniedException.class) 34 | public ResponseEntity handleAccessDeniedException(AccessDeniedException ex) { 35 | ApiError apiError = new ApiError("Access denied: Insufficient permissions", HttpStatus.FORBIDDEN); 36 | return new ResponseEntity<>(apiError, HttpStatus.FORBIDDEN); 37 | } 38 | 39 | @ExceptionHandler(Exception.class) 40 | public ResponseEntity handleGenericException(Exception ex) { 41 | ApiError apiError = new ApiError("An unexpected error occurred: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); 42 | return new ResponseEntity<>(apiError, HttpStatus.INTERNAL_SERVER_ERROR); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/security/JwtAuthFilter.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.security; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.User; 4 | import com.codingshuttle.youtube.hospitalManagement.repository.UserRepository; 5 | import jakarta.servlet.FilterChain; 6 | import jakarta.servlet.ServletException; 7 | import jakarta.servlet.http.HttpServletRequest; 8 | import jakarta.servlet.http.HttpServletResponse; 9 | import lombok.RequiredArgsConstructor; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 12 | import org.springframework.security.core.context.SecurityContextHolder; 13 | import org.springframework.stereotype.Component; 14 | import org.springframework.web.filter.OncePerRequestFilter; 15 | import org.springframework.web.servlet.HandlerExceptionResolver; 16 | 17 | import java.io.IOException; 18 | 19 | @Component 20 | @Slf4j 21 | @RequiredArgsConstructor 22 | public class JwtAuthFilter extends OncePerRequestFilter { 23 | 24 | private final UserRepository userRepository; 25 | private final AuthUtil authUtil; 26 | 27 | private final HandlerExceptionResolver handlerExceptionResolver; 28 | 29 | @Override 30 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 31 | try { 32 | log.info("incoming request: {}", request.getRequestURI()); 33 | 34 | final String requestTokenHeader = request.getHeader("Authorization"); 35 | if (requestTokenHeader == null || !requestTokenHeader.startsWith("Bearer")) { 36 | filterChain.doFilter(request, response); 37 | return; 38 | } 39 | 40 | String token = requestTokenHeader.split("Bearer ")[1]; 41 | String username = authUtil.getUsernameFromToken(token); 42 | 43 | if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) { 44 | User user = userRepository.findByUsername(username).orElseThrow(); 45 | UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken 46 | = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities()); 47 | SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken); 48 | } 49 | filterChain.doFilter(request, response); 50 | } catch (Exception ex) { 51 | handlerExceptionResolver.resolveException(request, response, null, ex); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/com/codingshuttle/youtube/hospitalManagement/PatientTests.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.BloodGroupCountResponseEntity; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.Patient; 5 | import com.codingshuttle.youtube.hospitalManagement.entity.type.BloodGroupType; 6 | import com.codingshuttle.youtube.hospitalManagement.repository.PatientRepository; 7 | import com.codingshuttle.youtube.hospitalManagement.service.PatientService; 8 | import org.junit.jupiter.api.Test; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.data.domain.Page; 12 | import org.springframework.data.domain.PageRequest; 13 | import org.springframework.data.domain.Pageable; 14 | import org.springframework.data.domain.Sort; 15 | 16 | import java.time.LocalDate; 17 | import java.util.List; 18 | 19 | @SpringBootTest 20 | public class PatientTests { 21 | 22 | @Autowired 23 | private PatientRepository patientRepository; 24 | 25 | @Autowired 26 | private PatientService patientService; 27 | 28 | @Test 29 | public void testPatientRepository() { 30 | 31 | List patientList = patientRepository.findAllPatientWithAppointment(); 32 | System.out.println(patientList); 33 | 34 | } 35 | 36 | @Test 37 | public void testTransactionMethods() { 38 | // Patient patient = patientService.getPatientById(1L); 39 | 40 | // Patient patient = patientRepository.findById(1L).orElseThrow(() -> new EntityNotFoundException("Patient not " + 41 | // "found with id: 1")); 42 | 43 | // Patient patient = patientRepository.findByName("Diya Patel"); 44 | 45 | // List patientList = patientRepository.findByBirthDateOrEmail(LocalDate.of(1988, 3, 15), "diya" + 46 | // ".patel@example.com"); 47 | 48 | // List patientList = patientRepository.findByBornAfterDate(LocalDate.of(1993, 3, 14)); 49 | 50 | Page patientList = patientRepository.findAllPatients(PageRequest.of(1, 2, Sort.by("name"))); 51 | 52 | for(Patient patient: patientList) { 53 | System.out.println(patient); 54 | } 55 | // 56 | // List bloodGroupList = patientRepository.countEachBloodGroupType(); 57 | // for(Object[] objects: bloodGroupList) { 58 | // System.out.println(objects[0] +" "+ objects[1]); 59 | // } 60 | 61 | // int rowsUpdated = patientRepository.updateNameWithId("Arav Sharma", 1L); 62 | // System.out.println(rowsUpdated); 63 | 64 | // List bloodGroupList = patientRepository.countEachBloodGroupType(); 65 | // for(BloodGroupCountResponseEntity bloodGroupCountResponse: bloodGroupList) { 66 | // System.out.println(bloodGroupCountResponse); 67 | // } 68 | } 69 | } 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/security/AuthUtil.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.security; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.User; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.type.AuthProviderType; 5 | import io.jsonwebtoken.Claims; 6 | import io.jsonwebtoken.Jwts; 7 | import io.jsonwebtoken.security.Keys; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.beans.factory.annotation.Value; 10 | import org.springframework.security.oauth2.core.user.OAuth2User; 11 | import org.springframework.stereotype.Component; 12 | 13 | import javax.crypto.SecretKey; 14 | import java.nio.charset.StandardCharsets; 15 | import java.util.Date; 16 | 17 | @Component 18 | @Slf4j 19 | public class AuthUtil { 20 | 21 | @Value("${jwt.secretKey}") 22 | private String jwtSecretKey; 23 | 24 | private SecretKey getSecretKey() { 25 | return Keys.hmacShaKeyFor(jwtSecretKey.getBytes(StandardCharsets.UTF_8)); 26 | } 27 | 28 | public String generateAccessToken(User user) { 29 | return Jwts.builder() 30 | .subject(user.getUsername()) 31 | .claim("userId", user.getId().toString()) 32 | .issuedAt(new Date()) 33 | .expiration(new Date(System.currentTimeMillis() + 1000*60*10)) 34 | .signWith(getSecretKey()) 35 | .compact(); 36 | } 37 | 38 | public String getUsernameFromToken(String token) { 39 | Claims claims = Jwts.parser() 40 | .verifyWith(getSecretKey()) 41 | .build() 42 | .parseSignedClaims(token) 43 | .getPayload(); 44 | return claims.getSubject(); 45 | } 46 | 47 | public AuthProviderType getProviderTypeFromRegistrationId(String registrationId) { 48 | return switch (registrationId.toLowerCase()) { 49 | case "google" -> AuthProviderType.GOOGLE; 50 | case "github" -> AuthProviderType.GITHUB; 51 | case "facebook" -> AuthProviderType.FACEBOOK; 52 | default -> throw new IllegalArgumentException("Unsupported OAuth2 provider: " + registrationId); 53 | }; 54 | } 55 | 56 | 57 | public String determineProviderIdFromOAuth2User(OAuth2User oAuth2User, String registrationId) { 58 | String providerId = switch (registrationId.toLowerCase()) { 59 | case "google" -> oAuth2User.getAttribute("sub"); 60 | case "github" -> oAuth2User.getAttribute("id").toString(); 61 | 62 | default -> { 63 | log.error("Unsupported OAuth2 provider: {}", registrationId); 64 | throw new IllegalArgumentException("Unsupported OAuth2 provider: " + registrationId); 65 | } 66 | }; 67 | 68 | if (providerId == null || providerId.isBlank()) { 69 | log.error("Unable to determine providerId for provider: {}", registrationId); 70 | throw new IllegalArgumentException("Unable to determine providerId for OAuth2 login"); 71 | } 72 | return providerId; 73 | } 74 | 75 | public String determineUsernameFromOAuth2User(OAuth2User oAuth2User, String registrationId, String providerId) { 76 | String email = oAuth2User.getAttribute("email"); 77 | if (email != null && !email.isBlank()) { 78 | return email; 79 | } 80 | return switch (registrationId.toLowerCase()) { 81 | case "google" -> oAuth2User.getAttribute("sub"); 82 | case "github" -> oAuth2User.getAttribute("login"); 83 | default -> providerId; 84 | }; 85 | } 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 3.5.3 9 | 10 | 11 | com.codingshuttle.youtube 12 | hospitalManagement 13 | 0.0.1-SNAPSHOT 14 | hospitalManagement 15 | Hospital management system project for Spring Boot 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 21 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-data-jpa 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-web 40 | 41 | 42 | 43 | org.postgresql 44 | postgresql 45 | runtime 46 | 47 | 48 | org.projectlombok 49 | lombok 50 | true 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-test 55 | test 56 | 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-starter-security 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-oauth2-client 65 | 66 | 67 | 68 | io.jsonwebtoken 69 | jjwt-api 70 | 0.12.6 71 | 72 | 73 | io.jsonwebtoken 74 | jjwt-impl 75 | 0.12.6 76 | runtime 77 | 78 | 79 | io.jsonwebtoken 80 | jjwt-jackson 81 | 0.12.6 82 | runtime 83 | 84 | 85 | 86 | org.modelmapper 87 | modelmapper 88 | 3.2.0 89 | 90 | 91 | 92 | 93 | 94 | 95 | org.apache.maven.plugins 96 | maven-compiler-plugin 97 | 98 | 99 | 100 | org.projectlombok 101 | lombok 102 | 103 | 104 | 105 | 106 | 107 | org.springframework.boot 108 | spring-boot-maven-plugin 109 | 110 | 111 | 112 | org.projectlombok 113 | lombok 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/service/AppointmentService.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.service; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.AppointmentResponseDto; 4 | import com.codingshuttle.youtube.hospitalManagement.dto.CreateAppointmentRequestDto; 5 | import com.codingshuttle.youtube.hospitalManagement.entity.Appointment; 6 | import com.codingshuttle.youtube.hospitalManagement.entity.Doctor; 7 | import com.codingshuttle.youtube.hospitalManagement.entity.Patient; 8 | import com.codingshuttle.youtube.hospitalManagement.repository.AppointmentRepository; 9 | import com.codingshuttle.youtube.hospitalManagement.repository.DoctorRepository; 10 | import com.codingshuttle.youtube.hospitalManagement.repository.PatientRepository; 11 | import jakarta.persistence.EntityNotFoundException; 12 | import jakarta.transaction.Transactional; 13 | import lombok.RequiredArgsConstructor; 14 | import org.modelmapper.ModelMapper; 15 | import org.springframework.security.access.annotation.Secured; 16 | import org.springframework.security.access.prepost.PreAuthorize; 17 | import org.springframework.stereotype.Service; 18 | 19 | import java.util.List; 20 | import java.util.stream.Collectors; 21 | 22 | @Service 23 | @RequiredArgsConstructor 24 | public class AppointmentService { 25 | 26 | private final AppointmentRepository appointmentRepository; 27 | private final DoctorRepository doctorRepository; 28 | private final PatientRepository patientRepository; 29 | private final ModelMapper modelMapper; 30 | 31 | @Transactional 32 | @Secured("ROLE_PATIENT") 33 | public AppointmentResponseDto createNewAppointment(CreateAppointmentRequestDto createAppointmentRequestDto) { 34 | Long doctorId = createAppointmentRequestDto.getDoctorId(); 35 | Long patientId = createAppointmentRequestDto.getPatientId(); 36 | 37 | Patient patient = patientRepository.findById(patientId) 38 | .orElseThrow(() -> new EntityNotFoundException("Patient not found with ID: " + patientId)); 39 | Doctor doctor = doctorRepository.findById(doctorId) 40 | .orElseThrow(() -> new EntityNotFoundException("Doctor not found with ID: " + doctorId)); 41 | Appointment appointment = Appointment.builder() 42 | .reason(createAppointmentRequestDto.getReason()) 43 | .appointmentTime(createAppointmentRequestDto.getAppointmentTime()) 44 | .build(); 45 | 46 | appointment.setPatient(patient); 47 | appointment.setDoctor(doctor); 48 | patient.getAppointments().add(appointment); // to maintain consistency 49 | 50 | appointment = appointmentRepository.save(appointment); 51 | return modelMapper.map(appointment, AppointmentResponseDto.class); 52 | } 53 | 54 | @Transactional 55 | @PreAuthorize("hasAuthority('appointment:write') or #doctorId == authentication.principal.id") 56 | public Appointment reAssignAppointmentToAnotherDoctor(Long appointmentId, Long doctorId) { 57 | Appointment appointment = appointmentRepository.findById(appointmentId).orElseThrow(); 58 | Doctor doctor = doctorRepository.findById(doctorId).orElseThrow(); 59 | 60 | appointment.setDoctor(doctor); // this will automatically call the update, because it is dirty 61 | 62 | doctor.getAppointments().add(appointment); // just for bidirectional consistency 63 | 64 | return appointment; 65 | } 66 | 67 | @PreAuthorize("hasRole('ADMIN') OR (hasRole('DOCTOR') AND #doctorId == authentication.principal.id)") 68 | public List getAllAppointmentsOfDoctor(Long doctorId) { 69 | Doctor doctor = doctorRepository.findById(doctorId).orElseThrow(); 70 | 71 | return doctor.getAppointments() 72 | .stream() 73 | .map(appointment -> modelMapper.map(appointment, AppointmentResponseDto.class)) 74 | .collect(Collectors.toList()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/security/WebSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.security; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.entity.type.PermissionType; 4 | import com.codingshuttle.youtube.hospitalManagement.entity.type.RoleType; 5 | import jakarta.servlet.ServletException; 6 | import jakarta.servlet.http.HttpServletRequest; 7 | import jakarta.servlet.http.HttpServletResponse; 8 | import lombok.RequiredArgsConstructor; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.http.HttpMethod; 13 | import org.springframework.security.access.AccessDeniedException; 14 | import org.springframework.security.config.Customizer; 15 | import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; 16 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 17 | import org.springframework.security.config.annotation.web.configurers.ExceptionHandlingConfigurer; 18 | import org.springframework.security.config.http.SessionCreationPolicy; 19 | import org.springframework.security.core.Authentication; 20 | import org.springframework.security.core.AuthenticationException; 21 | import org.springframework.security.web.SecurityFilterChain; 22 | import org.springframework.security.web.access.AccessDeniedHandler; 23 | import org.springframework.security.web.authentication.AuthenticationFailureHandler; 24 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler; 25 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; 26 | import org.springframework.web.servlet.HandlerExceptionResolver; 27 | 28 | import java.io.IOException; 29 | 30 | import static com.codingshuttle.youtube.hospitalManagement.entity.type.PermissionType.*; 31 | import static com.codingshuttle.youtube.hospitalManagement.entity.type.RoleType.*; 32 | 33 | @Configuration 34 | @RequiredArgsConstructor 35 | @Slf4j 36 | @EnableMethodSecurity 37 | public class WebSecurityConfig { 38 | 39 | private final JwtAuthFilter jwtAuthFilter; 40 | private final OAuth2SuccessHandler oAuth2SuccessHandler; 41 | private final HandlerExceptionResolver handlerExceptionResolver; 42 | 43 | @Bean 44 | public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { 45 | httpSecurity 46 | .csrf(csrfConfig -> csrfConfig.disable()) 47 | .sessionManagement(sessionConfig -> 48 | sessionConfig.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) 49 | .authorizeHttpRequests(auth -> auth 50 | .requestMatchers("/public/**", "/auth/**").permitAll() 51 | .requestMatchers(HttpMethod.DELETE, "/admin/**") 52 | .hasAnyAuthority(APPOINTMENT_DELETE.name(), 53 | USER_MANAGE.name()) 54 | .requestMatchers("/admin/**").hasRole(ADMIN.name()) 55 | .requestMatchers("/doctors/**").hasAnyRole(DOCTOR.name(), ADMIN.name()) 56 | .anyRequest().authenticated() 57 | ) 58 | .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class) 59 | .oauth2Login(oAuth2 -> oAuth2 60 | .failureHandler((request, response, exception) -> { 61 | log.error("OAuth2 error: {}", exception.getMessage()); 62 | handlerExceptionResolver.resolveException(request, response, null, exception); 63 | }) 64 | .successHandler(oAuth2SuccessHandler) 65 | ) 66 | .exceptionHandling(exceptionHandlingConfigurer -> 67 | exceptionHandlingConfigurer.accessDeniedHandler((request, response, accessDeniedException) -> { 68 | handlerExceptionResolver.resolveException(request, response, null, accessDeniedException); 69 | })); 70 | 71 | // .formLogin(); 72 | return httpSecurity.build(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/codingshuttle/youtube/hospitalManagement/security/AuthService.java: -------------------------------------------------------------------------------- 1 | package com.codingshuttle.youtube.hospitalManagement.security; 2 | 3 | import com.codingshuttle.youtube.hospitalManagement.dto.LoginRequestDto; 4 | import com.codingshuttle.youtube.hospitalManagement.dto.LoginResponseDto; 5 | import com.codingshuttle.youtube.hospitalManagement.dto.SignUpRequestDto; 6 | import com.codingshuttle.youtube.hospitalManagement.dto.SignupResponseDto; 7 | import com.codingshuttle.youtube.hospitalManagement.entity.Patient; 8 | import com.codingshuttle.youtube.hospitalManagement.entity.User; 9 | import com.codingshuttle.youtube.hospitalManagement.entity.type.AuthProviderType; 10 | import com.codingshuttle.youtube.hospitalManagement.entity.type.RoleType; 11 | import com.codingshuttle.youtube.hospitalManagement.repository.PatientRepository; 12 | import com.codingshuttle.youtube.hospitalManagement.repository.UserRepository; 13 | import jakarta.transaction.Transactional; 14 | import lombok.RequiredArgsConstructor; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.security.authentication.AuthenticationManager; 17 | import org.springframework.security.authentication.BadCredentialsException; 18 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 19 | import org.springframework.security.core.Authentication; 20 | import org.springframework.security.crypto.password.PasswordEncoder; 21 | import org.springframework.security.oauth2.core.user.OAuth2User; 22 | import org.springframework.stereotype.Service; 23 | 24 | import java.util.Set; 25 | 26 | @Service 27 | @RequiredArgsConstructor 28 | public class AuthService { 29 | 30 | private final AuthenticationManager authenticationManager; 31 | private final AuthUtil authUtil; 32 | private final UserRepository userRepository; 33 | private final PasswordEncoder passwordEncoder; 34 | private final PatientRepository patientRepository; 35 | 36 | public LoginResponseDto login(LoginRequestDto loginRequestDto) { 37 | 38 | Authentication authentication = authenticationManager.authenticate( 39 | new UsernamePasswordAuthenticationToken(loginRequestDto.getUsername(), loginRequestDto.getPassword()) 40 | ); 41 | 42 | User user = (User) authentication.getPrincipal(); 43 | 44 | String token = authUtil.generateAccessToken(user); 45 | 46 | return new LoginResponseDto(token, user.getId()); 47 | } 48 | 49 | public User signUpInternal(SignUpRequestDto signupRequestDto, AuthProviderType authProviderType, String providerId) { 50 | User user = userRepository.findByUsername(signupRequestDto.getUsername()).orElse(null); 51 | 52 | if(user != null) throw new IllegalArgumentException("User already exists"); 53 | 54 | user = User.builder() 55 | .username(signupRequestDto.getUsername()) 56 | .providerId(providerId) 57 | .providerType(authProviderType) 58 | .roles(signupRequestDto.getRoles()) // Role.PATIENT 59 | .build(); 60 | 61 | if(authProviderType == AuthProviderType.EMAIL) { 62 | user.setPassword(passwordEncoder.encode(signupRequestDto.getPassword())); 63 | } 64 | 65 | user = userRepository.save(user); 66 | 67 | Patient patient = Patient.builder() 68 | .name(signupRequestDto.getName()) 69 | .email(signupRequestDto.getUsername()) 70 | .user(user) 71 | .build(); 72 | patientRepository.save(patient); 73 | 74 | return user; 75 | } 76 | 77 | // login controller 78 | public SignupResponseDto signup(SignUpRequestDto signupRequestDto) { 79 | User user = signUpInternal(signupRequestDto, AuthProviderType.EMAIL, null); 80 | return new SignupResponseDto(user.getId(), user.getUsername()); 81 | } 82 | 83 | @Transactional 84 | public ResponseEntity handleOAuth2LoginRequest(OAuth2User oAuth2User, String registrationId) { 85 | AuthProviderType providerType = authUtil.getProviderTypeFromRegistrationId(registrationId); 86 | String providerId = authUtil.determineProviderIdFromOAuth2User(oAuth2User, registrationId); 87 | 88 | User user = userRepository.findByProviderIdAndProviderType(providerId, providerType).orElse(null); 89 | String email = oAuth2User.getAttribute("email"); 90 | String name = oAuth2User.getAttribute("name"); 91 | 92 | User emailUser = userRepository.findByUsername(email).orElse(null); 93 | 94 | if(user == null && emailUser == null) { 95 | // signup flow: 96 | String username = authUtil.determineUsernameFromOAuth2User(oAuth2User, registrationId, providerId); 97 | user = signUpInternal(new SignUpRequestDto(username, null, name, Set.of(RoleType.PATIENT)), providerType, providerId); 98 | } else if(user != null) { 99 | if(email != null && !email.isBlank() && !email.equals(user.getUsername())) { 100 | user.setUsername(email); 101 | userRepository.save(user); 102 | } 103 | } else { 104 | throw new BadCredentialsException("This email is already registered with provider "+emailUser.getProviderType()); 105 | } 106 | 107 | LoginResponseDto loginResponseDto = new LoginResponseDto(authUtil.generateAccessToken(user), user.getId()); 108 | return ResponseEntity.ok(loginResponseDto); 109 | } 110 | } 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | <# : batch portion 2 | @REM ---------------------------------------------------------------------------- 3 | @REM Licensed to the Apache Software Foundation (ASF) under one 4 | @REM or more contributor license agreements. See the NOTICE file 5 | @REM distributed with this work for additional information 6 | @REM regarding copyright ownership. The ASF licenses this file 7 | @REM to you under the Apache License, Version 2.0 (the 8 | @REM "License"); you may not use this file except in compliance 9 | @REM with the License. You may obtain a copy of the License at 10 | @REM 11 | @REM http://www.apache.org/licenses/LICENSE-2.0 12 | @REM 13 | @REM Unless required by applicable law or agreed to in writing, 14 | @REM software distributed under the License is distributed on an 15 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | @REM KIND, either express or implied. See the License for the 17 | @REM specific language governing permissions and limitations 18 | @REM under the License. 19 | @REM ---------------------------------------------------------------------------- 20 | 21 | @REM ---------------------------------------------------------------------------- 22 | @REM Apache Maven Wrapper startup batch script, version 3.3.2 23 | @REM 24 | @REM Optional ENV vars 25 | @REM MVNW_REPOURL - repo url base for downloading maven distribution 26 | @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 27 | @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output 28 | @REM ---------------------------------------------------------------------------- 29 | 30 | @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) 31 | @SET __MVNW_CMD__= 32 | @SET __MVNW_ERROR__= 33 | @SET __MVNW_PSMODULEP_SAVE=%PSModulePath% 34 | @SET PSModulePath= 35 | @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( 36 | IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) 37 | ) 38 | @SET PSModulePath=%__MVNW_PSMODULEP_SAVE% 39 | @SET __MVNW_PSMODULEP_SAVE= 40 | @SET __MVNW_ARG0_NAME__= 41 | @SET MVNW_USERNAME= 42 | @SET MVNW_PASSWORD= 43 | @IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) 44 | @echo Cannot start maven from wrapper >&2 && exit /b 1 45 | @GOTO :EOF 46 | : end batch / begin powershell #> 47 | 48 | $ErrorActionPreference = "Stop" 49 | if ($env:MVNW_VERBOSE -eq "true") { 50 | $VerbosePreference = "Continue" 51 | } 52 | 53 | # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties 54 | $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl 55 | if (!$distributionUrl) { 56 | Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" 57 | } 58 | 59 | switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { 60 | "maven-mvnd-*" { 61 | $USE_MVND = $true 62 | $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" 63 | $MVN_CMD = "mvnd.cmd" 64 | break 65 | } 66 | default { 67 | $USE_MVND = $false 68 | $MVN_CMD = $script -replace '^mvnw','mvn' 69 | break 70 | } 71 | } 72 | 73 | # apply MVNW_REPOURL and calculate MAVEN_HOME 74 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 75 | if ($env:MVNW_REPOURL) { 76 | $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } 77 | $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" 78 | } 79 | $distributionUrlName = $distributionUrl -replace '^.*/','' 80 | $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' 81 | $MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" 82 | if ($env:MAVEN_USER_HOME) { 83 | $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" 84 | } 85 | $MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' 86 | $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" 87 | 88 | if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { 89 | Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" 90 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 91 | exit $? 92 | } 93 | 94 | if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { 95 | Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" 96 | } 97 | 98 | # prepare tmp dir 99 | $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile 100 | $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" 101 | $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null 102 | trap { 103 | if ($TMP_DOWNLOAD_DIR.Exists) { 104 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 105 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 106 | } 107 | } 108 | 109 | New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null 110 | 111 | # Download and Install Apache Maven 112 | Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 113 | Write-Verbose "Downloading from: $distributionUrl" 114 | Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 115 | 116 | $webclient = New-Object System.Net.WebClient 117 | if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { 118 | $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) 119 | } 120 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 121 | $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null 122 | 123 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 124 | $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum 125 | if ($distributionSha256Sum) { 126 | if ($USE_MVND) { 127 | Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." 128 | } 129 | Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash 130 | if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { 131 | Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." 132 | } 133 | } 134 | 135 | # unzip and move 136 | Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null 137 | Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null 138 | try { 139 | Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null 140 | } catch { 141 | if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { 142 | Write-Error "fail to move MAVEN_HOME" 143 | } 144 | } finally { 145 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 146 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 147 | } 148 | 149 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 150 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.3.2 23 | # 24 | # Optional ENV vars 25 | # ----------------- 26 | # JAVA_HOME - location of a JDK home dir, required when download maven via java source 27 | # MVNW_REPOURL - repo url base for downloading maven distribution 28 | # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 29 | # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output 30 | # ---------------------------------------------------------------------------- 31 | 32 | set -euf 33 | [ "${MVNW_VERBOSE-}" != debug ] || set -x 34 | 35 | # OS specific support. 36 | native_path() { printf %s\\n "$1"; } 37 | case "$(uname)" in 38 | CYGWIN* | MINGW*) 39 | [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" 40 | native_path() { cygpath --path --windows "$1"; } 41 | ;; 42 | esac 43 | 44 | # set JAVACMD and JAVACCMD 45 | set_java_home() { 46 | # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched 47 | if [ -n "${JAVA_HOME-}" ]; then 48 | if [ -x "$JAVA_HOME/jre/sh/java" ]; then 49 | # IBM's JDK on AIX uses strange locations for the executables 50 | JAVACMD="$JAVA_HOME/jre/sh/java" 51 | JAVACCMD="$JAVA_HOME/jre/sh/javac" 52 | else 53 | JAVACMD="$JAVA_HOME/bin/java" 54 | JAVACCMD="$JAVA_HOME/bin/javac" 55 | 56 | if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then 57 | echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 58 | echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 59 | return 1 60 | fi 61 | fi 62 | else 63 | JAVACMD="$( 64 | 'set' +e 65 | 'unset' -f command 2>/dev/null 66 | 'command' -v java 67 | )" || : 68 | JAVACCMD="$( 69 | 'set' +e 70 | 'unset' -f command 2>/dev/null 71 | 'command' -v javac 72 | )" || : 73 | 74 | if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then 75 | echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 76 | return 1 77 | fi 78 | fi 79 | } 80 | 81 | # hash string like Java String::hashCode 82 | hash_string() { 83 | str="${1:-}" h=0 84 | while [ -n "$str" ]; do 85 | char="${str%"${str#?}"}" 86 | h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) 87 | str="${str#?}" 88 | done 89 | printf %x\\n $h 90 | } 91 | 92 | verbose() { :; } 93 | [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } 94 | 95 | die() { 96 | printf %s\\n "$1" >&2 97 | exit 1 98 | } 99 | 100 | trim() { 101 | # MWRAPPER-139: 102 | # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. 103 | # Needed for removing poorly interpreted newline sequences when running in more 104 | # exotic environments such as mingw bash on Windows. 105 | printf "%s" "${1}" | tr -d '[:space:]' 106 | } 107 | 108 | # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties 109 | while IFS="=" read -r key value; do 110 | case "${key-}" in 111 | distributionUrl) distributionUrl=$(trim "${value-}") ;; 112 | distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; 113 | esac 114 | done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" 115 | [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" 116 | 117 | case "${distributionUrl##*/}" in 118 | maven-mvnd-*bin.*) 119 | MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ 120 | case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in 121 | *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; 122 | :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; 123 | :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; 124 | :Linux*x86_64*) distributionPlatform=linux-amd64 ;; 125 | *) 126 | echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 127 | distributionPlatform=linux-amd64 128 | ;; 129 | esac 130 | distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" 131 | ;; 132 | maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; 133 | *) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; 134 | esac 135 | 136 | # apply MVNW_REPOURL and calculate MAVEN_HOME 137 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 138 | [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" 139 | distributionUrlName="${distributionUrl##*/}" 140 | distributionUrlNameMain="${distributionUrlName%.*}" 141 | distributionUrlNameMain="${distributionUrlNameMain%-bin}" 142 | MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" 143 | MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" 144 | 145 | exec_maven() { 146 | unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : 147 | exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" 148 | } 149 | 150 | if [ -d "$MAVEN_HOME" ]; then 151 | verbose "found existing MAVEN_HOME at $MAVEN_HOME" 152 | exec_maven "$@" 153 | fi 154 | 155 | case "${distributionUrl-}" in 156 | *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; 157 | *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; 158 | esac 159 | 160 | # prepare tmp dir 161 | if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then 162 | clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } 163 | trap clean HUP INT TERM EXIT 164 | else 165 | die "cannot create temp dir" 166 | fi 167 | 168 | mkdir -p -- "${MAVEN_HOME%/*}" 169 | 170 | # Download and Install Apache Maven 171 | verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 172 | verbose "Downloading from: $distributionUrl" 173 | verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 174 | 175 | # select .zip or .tar.gz 176 | if ! command -v unzip >/dev/null; then 177 | distributionUrl="${distributionUrl%.zip}.tar.gz" 178 | distributionUrlName="${distributionUrl##*/}" 179 | fi 180 | 181 | # verbose opt 182 | __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' 183 | [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v 184 | 185 | # normalize http auth 186 | case "${MVNW_PASSWORD:+has-password}" in 187 | '') MVNW_USERNAME='' MVNW_PASSWORD='' ;; 188 | has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; 189 | esac 190 | 191 | if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then 192 | verbose "Found wget ... using wget" 193 | wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" 194 | elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then 195 | verbose "Found curl ... using curl" 196 | curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" 197 | elif set_java_home; then 198 | verbose "Falling back to use Java to download" 199 | javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" 200 | targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" 201 | cat >"$javaSource" <<-END 202 | public class Downloader extends java.net.Authenticator 203 | { 204 | protected java.net.PasswordAuthentication getPasswordAuthentication() 205 | { 206 | return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); 207 | } 208 | public static void main( String[] args ) throws Exception 209 | { 210 | setDefault( new Downloader() ); 211 | java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); 212 | } 213 | } 214 | END 215 | # For Cygwin/MinGW, switch paths to Windows format before running javac and java 216 | verbose " - Compiling Downloader.java ..." 217 | "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" 218 | verbose " - Running Downloader.java ..." 219 | "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" 220 | fi 221 | 222 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 223 | if [ -n "${distributionSha256Sum-}" ]; then 224 | distributionSha256Result=false 225 | if [ "$MVN_CMD" = mvnd.sh ]; then 226 | echo "Checksum validation is not supported for maven-mvnd." >&2 227 | echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 228 | exit 1 229 | elif command -v sha256sum >/dev/null; then 230 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then 231 | distributionSha256Result=true 232 | fi 233 | elif command -v shasum >/dev/null; then 234 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then 235 | distributionSha256Result=true 236 | fi 237 | else 238 | echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 239 | echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 240 | exit 1 241 | fi 242 | if [ $distributionSha256Result = false ]; then 243 | echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 244 | echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 245 | exit 1 246 | fi 247 | fi 248 | 249 | # unzip and move 250 | if command -v unzip >/dev/null; then 251 | unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" 252 | else 253 | tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" 254 | fi 255 | printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" 256 | mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" 257 | 258 | clean || : 259 | exec_maven "$@" 260 | --------------------------------------------------------------------------------