Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@
public class ReportController {
private final ReportService reportService;


// Он должен формировать JSON-файл со статистикой по отделам:
// А также сохранять содержимое файла в базу данных.
// Метод возвращает целочисленный идентификатор сохраненного в базе данных файла.
@PostMapping
public Integer createReport() {
return reportService.createReports();
}


// Он должен находить и возвращать созданный ранее файл в формате JSON по переданному уникальному идентификатору.
@GetMapping("/{id}")
public Report getReportById(@PathVariable("id") Integer id) {
public String getReportById(@PathVariable("id") Integer id) {
return reportService.getReportById(id);
}
}
}
5 changes: 0 additions & 5 deletions src/main/java/com/example/weblibrary/model/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,4 @@ public class Position implements Serializable {
private Integer positionId;
private String positionName;

// @OneToMany(mappedBy = "employee")
// @JoinColumn(name = "employee_id")
// private List<Employee> employees;


}
9 changes: 8 additions & 1 deletion src/main/java/com/example/weblibrary/model/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import lombok.*;
import lombok.experimental.Accessors;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Path;

@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -17,16 +20,20 @@ public class Report implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "report_id")
private Integer id;
// Название отдела.

@Lob
@Column(name = "data", columnDefinition = "text")
@Transient
private String data;

private String pathFile;

public Report(String data) {
this.data = data;
}

public Report(String positionName, Long count, Integer max, Integer min, Double avg) {
this.data = "Position: " + positionName + ", countOfEmployee: " + count + ", maxSalary: " + max + ", minSalary: " + min + ", avgSalary: " + avg;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import java.util.List;

public interface ReportRepository extends CrudRepository<Report, Integer> {
// @Query(value = "SELECT new com.example.weblibrary.DTO.ReportDTO(p.positionName, max(e.salary), min(e.salary), avg(e.salary)) FROM Employee e join fetch Position p WHERE e.position = p GROUP BY p.positionId")
// List<ReportDTO> createReport();
@Query("SELECT new com.example.weblibrary.model.Report(" +
"p.positionName, COUNT (e.id), MAX (e.salary), MIN (e.salary), AVG (e.salary)) " +
"FROM Employee e join fetch Position p " +
"where e.position = p " +
"Group by p.positionId " )
List<Report> createReport();

}
@Query("SELECT new com.example.weblibrary.model.Report(" +
"p.positionName, COUNT (e.id), MAX (e.salary), MIN (e.salary), AVG (e.salary)) " +
"FROM Employee e join fetch Position p " +
"where e.position = p " +
"Group by p.positionId " )
List<Report> createReport();

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.example.weblibrary.service;

import com.example.weblibrary.model.Report;

public interface ReportService {
Report getReportById(Integer id);
String getReportById(Integer id);

Integer createReports();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import com.example.weblibrary.exceptions.ReportNotFoundException;
import com.example.weblibrary.model.Report;
import com.example.weblibrary.repository.EmployeeRepository;
import com.example.weblibrary.repository.ReportRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RequiredArgsConstructor
Expand All @@ -20,23 +25,50 @@ public class ReportServiceImpl implements ReportService {
ObjectMapper objectMapper = new ObjectMapper();

@Override
public Report getReportById(Integer id) {
return reportRepository.findById(id)
public String getReportById(Integer id) {
Report report = reportRepository.findById(id)
.orElseThrow(()-> new ReportNotFoundException(id));
return report.getPathFile();
}

@Override
public Integer createReports() {
List<Report> reportDTOS = reportRepository.createReport();
List<Report> reports = reportRepository.createReport();






try {
String json = objectMapper.writeValueAsString(reportDTOS);
Report report = new Report(json);
String text = objectMapper.writeValueAsString(reports);
Report report = new Report(text);
reportRepository.save(report);

String fileName =
"c:\\Users\\Ruslan\\" +
"report" + report.getId() + ".json";
File file = new File(fileName);
file.createNewFile();
report.setPathFile(fileName);
writeTextToFile(text, fileName);
reportRepository.save(report);


return report.getId();
} catch (IOException e) {
e.printStackTrace();
} return 0;
}


}
private static void writeTextToFile(String text, String fileName) {
Path path = Paths.get(fileName);
try {
Files.write(path, text.getBytes(StandardCharsets.UTF_8));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}

}