Major refactor of reading from excel to db

This commit is contained in:
Maciek Głowacki
2020-09-21 17:45:52 +02:00
parent e91965e9b5
commit c449bc22e1
16 changed files with 158 additions and 167 deletions

View File

@ -1,6 +1,7 @@
package com.plannaplan.services;
import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.Course;
import com.plannaplan.repositories.CourseRepository;
@ -13,7 +14,7 @@ public class CourseService {
@Autowired
private CourseRepository repo;
public Course getCourseByName(String name) {
public Optional<Course> getCourseByName(String name) {
return this.repo.findByName(name);
}
@ -21,15 +22,16 @@ public class CourseService {
return this.repo.findAll();
}
public void save(Course course) {
public Course save(Course course) {
this.repo.save(course);
return course;
}
public void delete(Course course){
public void delete(Course course) {
this.repo.delete(course);
}
public int getCoursesAmmount(){
return (int)this.repo.count();
public int getCoursesAmmount() {
return (int) this.repo.count();
}
}

View File

@ -1,6 +1,7 @@
package com.plannaplan.services;
import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.Groups;
import com.plannaplan.repositories.GroupRepository;
@ -16,7 +17,7 @@ public class GroupService {
public GroupService() {
}
public Groups find(int time, int capacity, String room) {
public Optional<Groups> find(int time, int capacity, String room) {
return this.repo.find(time, room, capacity);
}
@ -24,15 +25,16 @@ public class GroupService {
return this.repo.getByCourse(id);
}
public void save(Groups group) {
public Groups save(Groups group) {
this.repo.save(group);
return group;
}
public void delete(Groups groups){
public void delete(Groups groups) {
this.repo.delete(groups);
}
public int getGroupsAmmount(){
return (int)this.repo.count();
public int getGroupsAmmount() {
return (int) this.repo.count();
}
}

View File

@ -1,5 +1,7 @@
package com.plannaplan.services;
import java.util.Optional;
import com.plannaplan.entities.Lecturer;
import com.plannaplan.repositories.LecturerRepository;
@ -11,19 +13,20 @@ public class LecturerService {
@Autowired
private LecturerRepository repo;
public Lecturer getLecturer(String title, String name, String surname) {
public Optional<Lecturer> getLecturer(String title, String name, String surname) {
return repo.find(title, name, surname);
}
public void save(Lecturer lecturer) {
public Lecturer save(Lecturer lecturer) {
repo.save(lecturer);
return lecturer;
}
public void delete(Lecturer lecturer) {
repo.delete(lecturer);
}
public int getLecturersAmmount(){
return (int)this.repo.count();
public int getLecturersAmmount() {
return (int) this.repo.count();
}
}

View File

@ -19,11 +19,11 @@ public class UserService extends EventWatcher {
super();
}
// this code is more idiomatic to java using java 8 stream API
public String login(String authority) throws UserNotFoundException {
User user = this.repo.getByAuthority(authority.replace("\n", "").trim());
if (user == null) {
throw new UserNotFoundException("Can not find user with given authority");
}
User user = this.repo.getByAuthority(authority.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Can not find user with given authority"));
String token = UUID.randomUUID().toString();
user.setToken(token);
this.repo.save(user);
@ -34,10 +34,14 @@ public class UserService extends EventWatcher {
this.repo.save(user);
}
public User getUserByEmail(String email) {
return this.repo.getByAuthority(email.replace("\n", "").trim());
// this code is more idiomatic to java
public User getUserByEmail(String email) throws UserNotFoundException {
return this.repo.getByAuthority(email.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Cannot find user with given authority"));
}
// why is not throwing exception?
public User getByToken(String token) {
return this.repo.getByToken(token);
}