Skip to content

Commit

Permalink
Merge branch 'main' into hospitals-doctors
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinnsh authored Dec 12, 2023
2 parents 29665f5 + c51346a commit 79b3377
Show file tree
Hide file tree
Showing 43 changed files with 4,004 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
.antMatchers("/documents/passport/passport-file").authenticated()
.antMatchers("/documents/passport/passport-file/**").authenticated()
.antMatchers("/profile").authenticated()
.antMatchers("/profile").authenticated()
.antMatchers("/admin").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.personal.patient.account.models.RegistrationUser;
import com.personal.patient.account.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -14,6 +17,11 @@
public class AuthController {
private final AuthService authService;

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<String> handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Ошибка валидации: email не уникальный");
}

@PostMapping("/auth")
public ResponseEntity<?> createAuthToken(@RequestBody JwtRequest authRequest){
return authService.createAuthToken(authRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.personal.patient.account.entities.User;
import com.personal.patient.account.entities.Profile;
import com.personal.patient.account.exceptions.AlreadyExistException;
import com.personal.patient.account.exceptions.NotFoundException;
import com.personal.patient.account.models.ProfileRepresentation;
import com.personal.patient.account.models.FullProfileRepresentation;
Expand All @@ -25,12 +26,20 @@ protected ResponseEntity<Object> handleNotFoundException(NotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}

@ExceptionHandler(AlreadyExistException.class)
protected ResponseEntity<Object> handleAlreadyExistException(AlreadyExistException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}


@PostMapping("")
public ResponseEntity<?> saveOrChangeProfile(@RequestBody ProfileRepresentation profileRequest, Principal principal){
User user = userService.getUserByPrincipal(principal);
profileService.createOrChangeProfile(profileRequest, user);
return ResponseEntity.ok(new FullProfileRepresentation(profileRequest, user.getId()));
return ResponseEntity.ok(profileService.createProfile(profileRequest, principal));
}

@PutMapping("")
public ResponseEntity<?> changeProfile(@RequestBody ProfileRepresentation profileRequest, Principal principal){
return ResponseEntity.ok(profileService.changeProfile(profileRequest, principal));
}

@GetMapping("")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.personal.patient.account.exceptions;

public class AlreadyExistException extends RuntimeException {
public AlreadyExistException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Date;


@Data
@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -18,40 +20,44 @@ public class CreatingPassportRequest {

private PassportGender passportGender;

private Date dateOfBirth;
private String dateOfBirth;

private String placeOfBirth;

private Integer series;

private Integer number;

private Date dateOfIssue;
private String dateOfIssue;

private Integer divisionCode;

private String issuedBy;

//TO DO поменять убрать время
public CreatingPassportRequest(Passport passport){
this.id = passport.getId();

this.fullName = passport.getFullName();

this.passportGender = passport.getPassportGender();

this.dateOfBirth = passport.getDateOfBirth();
this.dateOfBirth = parseDateToString(passport.getDateOfBirth());

this.placeOfBirth = passport.getPlaceOfBirth();

this.series = passport.getSeries();

this.number = passport.getNumber();

this.dateOfIssue = passport.getDateOfIssue();
this.dateOfIssue = parseDateToString(passport.getDateOfIssue());

this.divisionCode = passport.getDivisionCode();

this.issuedBy = passport.getIssuedBy();
}

public String parseDateToString(Date date){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(date);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.personal.patient.account.models;

import com.personal.patient.account.models.enums.PassportGender;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;

@Data
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
public class JwtRequest {
private String email;
private String password;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,30 @@ public class AuthService {
private final JwtTokenUtils jwtTokenUtils;
private final AuthenticationManager authenticationManager;

public ResponseEntity<?> createAuthToken(JwtRequest authRequest){
try{
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authRequest.getEmail(), authRequest.getPassword()));
public ResponseEntity<?> createAuthToken(JwtRequest authRequest) {
try {
authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(authRequest.getEmail(), authRequest.getPassword()));
} catch (BadCredentialsException e) {
return new ResponseEntity<>(new AppError(HttpStatus.UNAUTHORIZED.value(), "Неправильный логин или пароль"),
HttpStatus.UNAUTHORIZED);
}
catch (BadCredentialsException e){
return new ResponseEntity<>(new AppError(HttpStatus.UNAUTHORIZED.value(), "Неправильный логин или пароль"), HttpStatus.UNAUTHORIZED);}
UserDetails userDetails = userService.loadUserByUsername(authRequest.getEmail());
String token = jwtTokenUtils.generateToken(userDetails);
return ResponseEntity.ok(new JwtResponse(token));
}

public ResponseEntity<?> createNewUser(RegistrationUser registrationUser){
if(!registrationUser.getPassword().equals(registrationUser.getConfirmPassword())){
return new ResponseEntity<>(new AppError(HttpStatus.BAD_REQUEST.value(), "Пароли не совпадают"), HttpStatus.BAD_REQUEST);
public ResponseEntity<?> createNewUser(RegistrationUser registrationUser) {
if (!registrationUser.getPassword().equals(registrationUser.getConfirmPassword())) {
return new ResponseEntity<>(new AppError(HttpStatus.BAD_REQUEST.value(), "Пароли не совпадают"),
HttpStatus.BAD_REQUEST);
}
if(userService.findByUsername(registrationUser.getEmail()).isPresent()){
return new ResponseEntity<>(new AppError(HttpStatus.BAD_REQUEST.value(), "Пользовательс указанными именем уже существует"), HttpStatus.BAD_REQUEST);
if (userService.findByUsername(registrationUser.getEmail()).isPresent()) {
return new ResponseEntity<>(
new AppError(HttpStatus.BAD_REQUEST.value(), "Пользователь указанными именем уже существует"),
HttpStatus.BAD_REQUEST);
}
User user = userService.createNewUser(registrationUser);
return ResponseEntity.ok(new UserResponse(user.getId(), user.getEmail()));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.personal.patient.account.entities.User;
import com.personal.patient.account.entities.Profile;
import com.personal.patient.account.exceptions.AlreadyExistException;
import com.personal.patient.account.exceptions.NotFoundException;
import com.personal.patient.account.models.FullProfileRepresentation;
import com.personal.patient.account.models.ProfileRepresentation;
import com.personal.patient.account.models.enums.Gender;
import com.personal.patient.account.repositories.ProfileRepository;
Expand All @@ -10,22 +13,25 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.security.Principal;
import java.util.Date;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class ProfileService {

private final ProfileRepository profileRepository;

private final UserRepository userRepository;
private final UserService userService;

private final DateUtils dateUtils;

public void createOrChangeProfile(ProfileRepresentation profileRequest, User user){
Optional<Profile> existProfile = profileRepository.findByUser(user);
Profile profile = existProfile.orElseGet(Profile::new);
public FullProfileRepresentation createProfile(ProfileRepresentation profileRequest, Principal principal){
User user = userService.getUserByPrincipal(principal);
if(profileRepository.findByUser(user).isPresent()){
throw new AlreadyExistException("user with email " + user.getEmail() + " alredy have profile");
}
Profile profile = new Profile();

profile.setUser(user);

Expand All @@ -40,7 +46,31 @@ public void createOrChangeProfile(ProfileRepresentation profileRequest, User use
profile.setAddress(profileRequest.getAddress());
profileRepository.save(profile);
user.setProfile(profile);
userRepository.save(user);
userService.save(user);
return new FullProfileRepresentation(profileRequest, user.getId());
}

public FullProfileRepresentation changeProfile(ProfileRepresentation profileRequest, Principal principal){
User user = userService.getUserByPrincipal(principal);
Profile profile = profileRepository.findByUser(user).orElseThrow(
() ->new NotFoundException("user with email " + user.getEmail() + " did not have profile")
);

profile.setUser(user);

profile.setFirstName(profileRequest.getFirstName());
profile.setMiddleName(profileRequest.getMiddleName());
profile.setLastName(profileRequest.getLastName());

Date date = dateUtils.parseStringToDate(profileRequest.getDateOfBirth());
profile.setDateOfBirth(date);
profile.setPhoneNumber(profileRequest.getPhoneNumber());
profile.setGender(Gender.fromValue(profileRequest.getGender()));
profile.setAddress(profileRequest.getAddress());
profileRepository.save(profile);
user.setProfile(profile);
userService.save(user);
return new FullProfileRepresentation(profileRequest, user.getId());
}

public Profile getProfileByUser(User user){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public ResultCard createResultCard(CreatingResultCardResponse resultCardResponse
resultCard.setDateOfMake(new Date());

Date dateOfShouldReady = dateUtils.parseStringToDate(resultCardResponse.getDateOfShouldReady());
resultCard.setName(resultCardResponse.getName());
resultCard.setDateOfShouldReady(dateOfShouldReady);
resultCard.setDescription(resultCardResponse.getDescription());
resultCard.setHospitalAddress(resultCardResponse.getHospitalAddress());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ public User getUserByPrincipal(Principal principal) {
public User getUserByEmail(String email){
return userRepository.findByEmail(email).orElseThrow(() -> new NotFoundException("user with email " + email + " not found"));
}

public User save(User user){
return userRepository.save(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ public Date parseStringToTime(String stringTime){
e.printStackTrace();
}
return date;

public String parseDateToString(Date date){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
Expand Down
8 changes: 4 additions & 4 deletions client/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"node_modules/ngx-toastr/toastr.css",
"node_modules/ngx-spinner/animations/ball-scale-multiple.css",
"src/styles.scss"
],
"scripts": [
"node_modules/bootstrap/dist/js/bootstrap.min.js",
"./node_modules/@lottiefiles/lottie-player/dist/lottie-player.js"

]
},
"configurations": {
Expand All @@ -50,8 +50,8 @@
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
"maximumWarning": "60kb",
"maximumError": "100kb"
}
],
"outputHashing": "all"
Expand Down Expand Up @@ -179,4 +179,4 @@
"cli": {
"analytics": "ee748ae0-81b8-4b6e-b8cd-93d2437bd568"
}
}
}
Loading

0 comments on commit 79b3377

Please sign in to comment.