Skip to content

Commit

Permalink
Merge pull request #38 from Incredible-Technologies/patient-documents
Browse files Browse the repository at this point in the history
Patient documents
  • Loading branch information
DanilPomortsev authored Nov 29, 2023
2 parents c71795f + 6957a36 commit 5525d04
Show file tree
Hide file tree
Showing 29 changed files with 735 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
.antMatchers("/all-not-done-cards").authenticated()
.antMatchers("/all-done-cards").authenticated()
.antMatchers("/files/result-file/**").authenticated()
.antMatchers("/files/passport-file/**").authenticated()
.antMatchers("/documents/snils").authenticated()
.antMatchers("/documents/passport").authenticated()
.antMatchers("/documents/passport/passport-file").authenticated()
.antMatchers("/documents/passport/passport-file/**").authenticated()
.antMatchers("/profile").authenticated()
.antMatchers("/admin").hasRole("ADMIN")
.anyRequest().permitAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.personal.patient.account.controllers;

import com.personal.patient.account.entities.PassportFile;
import com.personal.patient.account.entities.ResultCard;
import com.personal.patient.account.entities.ResultFile;
import com.personal.patient.account.exceptions.ForbiddenException;
import com.personal.patient.account.exceptions.NotFoundException;
import com.personal.patient.account.service.PassportFileService;
import com.personal.patient.account.service.ResultCardService;
import com.personal.patient.account.service.ResultFileService;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,6 +25,7 @@ public class FileController {

private final ResultCardService resultCardService;
private final ResultFileService resultFileService;
private final PassportFileService passportFileService;

@ExceptionHandler(NotFoundException.class)
protected ResponseEntity<Object> handleNotFoundException(NotFoundException ex) {
Expand All @@ -47,4 +50,17 @@ private ResponseEntity<?> getResultFile(@PathVariable Long cardId, Principal pri
.contentLength(resultFile.getSize())
.body(new InputStreamResource(new ByteArrayInputStream(resultFile.getBytes())));
}

@GetMapping("/passport-file/{passportFileId}")
private ResponseEntity<?> getPassportFile(@PathVariable Long passportFileId, Principal principal){
PassportFile passportFile = passportFileService.findById(passportFileId);
if(!passportFile.getPassport().getUser().getEmail().equals(principal.getName())){
throw new ForbiddenException("Access denied");
}
return ResponseEntity.ok()
.header("fileName", passportFile.getOriginalFileName())
.contentType(MediaType.valueOf(passportFile.getContentType()))
.contentLength(passportFile.getSize())
.body(new InputStreamResource(new ByteArrayInputStream(passportFile.getBytes())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.personal.patient.account.controllers;

import com.personal.patient.account.exceptions.ForbiddenException;
import com.personal.patient.account.exceptions.NotFoundException;
import com.personal.patient.account.models.CreatingPassportResponse;
import com.personal.patient.account.models.DeletePassportFile;
import com.personal.patient.account.service.PassportFileService;
import com.personal.patient.account.service.PassportService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.Principal;

@RestController
@RequiredArgsConstructor
@RequestMapping("/documents")
public class PassportController {

@ExceptionHandler(NotFoundException.class)
protected ResponseEntity<Object> handleNotFoundException(NotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}

@ExceptionHandler(ForbiddenException.class)
protected ResponseEntity<Object> handleForbiddenException(ForbiddenException ex) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ex.getMessage());
}


@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<String> handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Ошибка валидации: поле user_id уже существует");
}
private final PassportService passportService;

private final PassportFileService passportFileService;


@PostMapping("/passport")
public ResponseEntity<?> creatingPassport(@RequestBody CreatingPassportResponse creatingPassportResponse, Principal principal){
return ResponseEntity.ok(passportService.createPassport(creatingPassportResponse, principal));
}

@GetMapping("/passport")
public ResponseEntity<?> getPassport(Principal principal){
return ResponseEntity.ok(passportService.getPassport(principal));
}

@PutMapping("/passport")
public ResponseEntity<?> changePassport(@RequestBody CreatingPassportResponse creatingPassportResponse, Principal principal) {
return ResponseEntity.ok(passportService.changePassport(creatingPassportResponse, principal));
}


@PostMapping("/passport/passport-file")
public ResponseEntity<?> createPassportFile(@RequestParam("file") MultipartFile file, Principal principal) throws IOException {
return ResponseEntity.ok(passportFileService.addPassportFileRepository(principal, file));
}


@GetMapping("/passport/passport-file")
public ResponseEntity<?> getPassportFiles(Principal principal){
return ResponseEntity.ok(passportFileService.getPassportFiles(principal));
}


@DeleteMapping("/passport/passport-file/{deleteId}")
public ResponseEntity<?> DeletePassportFile(Principal principal, @PathVariable Long deleteId){
DeletePassportFile deletePassportFile = passportFileService.deletePassportFile(deleteId, principal);
return ResponseEntity.ok(deletePassportFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.personal.patient.account.controllers;

import com.personal.patient.account.exceptions.NotFoundException;
import com.personal.patient.account.models.CreatingSnilsResponse;
import com.personal.patient.account.service.SnilsService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@RestController
@RequiredArgsConstructor
@RequestMapping("/documents")
public class SnilsController {

@ExceptionHandler(NotFoundException.class)
protected ResponseEntity<Object> handleNotFoundException(NotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}


@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<String> handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Ошибка валидации: поле user_id уже существует");
}
private final SnilsService snilsService;

@PostMapping("/snils")
public ResponseEntity<?> creatingSnils(@RequestBody CreatingSnilsResponse creatingSnilsResponse, Principal principal){
return ResponseEntity.ok(snilsService.createSnils(creatingSnilsResponse, principal));
}

@GetMapping("/snils")
public ResponseEntity<?> getSnils(Principal principal){
return ResponseEntity.ok(snilsService.getSnils(principal));
}

@PutMapping("/snils")
public ResponseEntity<?> changeSnils(@RequestBody CreatingSnilsResponse creatingSnilsResponse, Principal principal){
return ResponseEntity.ok(snilsService.changeSnils(creatingSnilsResponse,principal));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.personal.patient.account.entities;


import com.personal.patient.account.models.enums.PassportGender;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(
name="passport",
uniqueConstraints = {
@UniqueConstraint(columnNames = "user_id")
}
)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Passport {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;

@Column(name="full_name")
private String fullName;

@Enumerated(EnumType.STRING)
private PassportGender passportGender;

@Column(name="date_of_birt")
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "dd-MM-yyyy")
private Date dateOfBirth;

@Column(name="place_of_birth")
private String placeOfBirth;

@Column(name="series")
private Integer series;

@Column(name="number")
private Integer number;

@Column(name="date_of_issue")
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "dd-MM-yyyy")
private Date dateOfIssue;

@Column(name="division_code")
private Integer divisionCode;

@Column(name="issued_by")
private String issuedBy;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "passport")
private List<PassportFile> passportFiles = new ArrayList<>();

@Override
public String toString() {
return "Passport";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.personal.patient.account.entities;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name="passport_file")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PassportFile {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;

@Column(name="name")
private String name;

@Column(name = "originalFileName")
private String originalFileName;

@Column(name="size")
private Long size;

@Column(name="contentType")
private String contentType;

@Lob
private byte[] bytes;

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="passport_id")
private Passport passport;

@Override
public String toString() {
return "PassportFile";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public class Profile {

@Enumerated(EnumType.STRING)
private Gender gender;

@Override
public String toString() {
return "Profile";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public class ResultFile {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "result_card_id")
private ResultCard resultCard;

@Override
public String toString() {
return "ResultFile";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public class Role {

@Column(name="name")
private String name;

@Override
public String toString() {
return "Role";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.personal.patient.account.entities;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(
name="snils",
uniqueConstraints = {
@UniqueConstraint(columnNames = "user_id")
}
)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Snils {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;

@Column(name="number")
private Long number;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@Override
public String toString(){
return "snils";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ public class User {
)
private Profile profile;

@OneToOne(
mappedBy = "user", cascade = CascadeType.ALL,
fetch = FetchType.LAZY
)
private Snils snils;

@OneToOne(
mappedBy = "user", cascade = CascadeType.ALL,
fetch = FetchType.LAZY
)
private Passport passport;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user")
private List<ResultCard> results = new ArrayList<>();

Expand Down
Loading

0 comments on commit 5525d04

Please sign in to comment.