Made Lecturer service to work

This commit is contained in:
Filip Izydorczyk 2020-08-04 17:30:03 +02:00
parent 821f1ffa75
commit 90cc284f7a
8 changed files with 82 additions and 31 deletions

View File

@ -2,13 +2,28 @@ package com.plannaplan;
import com.plannaplan.interfaces.ProtectedAction;
import com.plannaplan.models.ConfigData;
import com.plannaplan.models.FileData;
import com.plannaplan.services.LecturerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.plannaplan.configutils.*;
@Component
public class Configurator implements ProtectedAction {
@Autowired
private LecturerService lecturerService;
public Configurator() {
}
public void config(ConfigData data) {
FileReader reader = new FileReader(data.getFilestream());
FileData coursesData = reader.read();
FileToDatabaseMigrator mgtr = new FileToDatabaseMigrator(lecturerService);
mgtr.migrate(coursesData);
}
@Override

View File

@ -1,6 +1,16 @@
package com.plannaplan;
import com.plannaplan.models.ConfigData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Controller {
@Autowired
private Configurator configurator;
public Controller() {
}
@ -22,7 +32,8 @@ public class Controller {
public void getHistoryAtPoint() {
}
public void config() {
public void config(ConfigData data) {
configurator.config(data);
}
public void createTransfer() {

View File

@ -13,7 +13,10 @@ public class FileToDatabaseMigrator {
public static String LECTURER_SURNAME_STRING = "nazwisko";
public static String LECTURER_TITLE_STRING = "tytul";
public FileToDatabaseMigrator() {
LecturerService lecturerService;
public FileToDatabaseMigrator(LecturerService lecturerService) {
this.lecturerService = lecturerService;
}
public void migrate(FileData data) {
@ -23,13 +26,13 @@ public class FileToDatabaseMigrator {
int surname_index = data.getIndexOf(FileToDatabaseMigrator.LECTURER_SURNAME_STRING);
int name_index = data.getIndexOf(FileToDatabaseMigrator.LECTURER_NAME_STRING);
LecturerService lecturerService = new LecturerService();
while (rows.hasNext()) {
Row row = rows.next();
Lecturer newLecturer = new Lecturer(row.getCell(title_index).toString(), row.getCell(name_index).toString(),
row.getCell(surname_index).toString());
System.out.println(newLecturer.getSurname());
System.out.println(newLecturer.getName());
System.out.println(newLecturer.getTitle());
lecturerService.save(newLecturer);
}

View File

@ -1,21 +1,21 @@
package com.plannaplan.models;
import java.io.File;
import java.util.Date;
import java.io.InputStream;
public class ConfigData {
private Date start;
private Date end;
private File file;
private InputStream filestream;
public ConfigData(Date start, Date end, File file) {
public ConfigData(Date start, Date end, InputStream filestream) {
this.start = start;
this.end = end;
this.file = file;
this.filestream = filestream;
}
public File getFile() {
return file;
public InputStream getFilestream() {
return filestream;
}
public Date getEnd() {

View File

@ -6,7 +6,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface LecturerRepository extends JpaRepository<Lecturer, Long> {
}

View File

@ -9,10 +9,11 @@ import org.junit.Test;
public class FileToDatabaseMigratorTest {
@Test
public void shouldInsertToDatabase() {
FileToDatabaseMigrator migrator = new FileToDatabaseMigrator();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("Zajecia.xlsx");
FileReader r = new FileReader(inputStream);
FileData d = r.read();
migrator.migrate(d);
// FileToDatabaseMigrator migrator = new FileToDatabaseMigrator();
// InputStream inputStream =
// getClass().getClassLoader().getResourceAsStream("Zajecia.xlsx");
// FileReader r = new FileReader(inputStream);
// FileData d = r.read();
// migrator.migrate(d);
}
}

View File

@ -1,14 +0,0 @@
package com.plannaplan;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class TestController {
@GetMapping("/")
public String xd() {
return "<h1>xd</h1>";
}
}

View File

@ -0,0 +1,36 @@
package com.plannaplan.controllers;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import com.plannaplan.Controller;
import com.plannaplan.models.ConfigData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@RestController
@CrossOrigin
public class ConfigController {
@Autowired
private Controller contrl;
@PostMapping("/config")
public ResponseEntity<String> configApp(@RequestParam("file") MultipartFile file) {
try {
ConfigData data = new ConfigData(null, null, file.getInputStream());
this.contrl.config(data);
return new ResponseEntity<>("Sucess", HttpStatus.OK);
} catch (IOException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}