Added courses saveing

This commit is contained in:
Filip Izydorczyk 2020-08-06 14:32:41 +02:00
parent bb3e9c536d
commit 368b4066c0
3 changed files with 25 additions and 3 deletions

View File

@ -3,6 +3,8 @@ package com.plannaplan;
import com.plannaplan.interfaces.ProtectedAction;
import com.plannaplan.models.ConfigData;
import com.plannaplan.models.FileData;
import com.plannaplan.services.CourseService;
import com.plannaplan.services.GroupService;
import com.plannaplan.services.LecturerService;
import org.springframework.beans.factory.annotation.Autowired;
@ -16,13 +18,19 @@ public class Configurator implements ProtectedAction {
@Autowired
private LecturerService lecturerService;
@Autowired
private CourseService courseService;
@Autowired
private GroupService groupService;
public Configurator() {
}
public void config(ConfigData data) {
FileReader reader = new FileReader(data.getFilestream());
FileData coursesData = reader.read();
FileToDatabaseMigrator mgtr = new FileToDatabaseMigrator(lecturerService);
FileToDatabaseMigrator mgtr = new FileToDatabaseMigrator(lecturerService, courseService, groupService);
mgtr.migrate(coursesData);
}

View File

@ -24,8 +24,11 @@ public class FileToDatabaseMigrator {
CourseService courseService;
GroupService groupService;
public FileToDatabaseMigrator(LecturerService lecturerService) {
public FileToDatabaseMigrator(LecturerService lecturerService, CourseService courseService,
GroupService groupService) {
this.lecturerService = lecturerService;
this.groupService = groupService;
this.courseService = courseService;
}
public void migrate(FileData data) {
@ -52,12 +55,19 @@ public class FileToDatabaseMigrator {
String lecturer_surname = "";
String lecturer_name = "";
Course course = this.courseService.getCourseByName("");
String course_name = course_name_cell.toString();
String sym_str = sym.toString();
Course course = this.courseService.getCourseByName(course_name);
if (course == null) {
course = new Course();
course.setName(course_name);
course.setSymbol(sym_str);
}
courseService.save(course);
if (title_cell != null) {
lecturer_title = title_cell.toString();
}

View File

@ -14,4 +14,8 @@ public class CourseService {
public Course getCourseByName(String name) {
return this.repo.findByName(name);
}
public void save(Course course) {
this.repo.save(course);
}
}