84 lines
2.2 KiB
Java
84 lines
2.2 KiB
Java
package ovh.herisson.Clyde.Services;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import ovh.herisson.Clyde.Repositories.CourseRepository;
|
|
import ovh.herisson.Clyde.Tables.Course;
|
|
import ovh.herisson.Clyde.Tables.Role;
|
|
import ovh.herisson.Clyde.Tables.User;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class CourseService {
|
|
|
|
private final CourseRepository courseRepo;
|
|
|
|
public CourseService(CourseRepository courseRepo) {
|
|
this.courseRepo = courseRepo;
|
|
}
|
|
|
|
public Course save(Course course){
|
|
if (course.getOwner().getRole() != Role.Teacher)
|
|
return null;
|
|
return courseRepo.save(course);
|
|
}
|
|
|
|
public Course findById(long id){
|
|
return courseRepo.findById(id);
|
|
}
|
|
|
|
|
|
public Iterable<Course> findAll() {
|
|
return courseRepo.findAll();
|
|
}
|
|
|
|
|
|
public Iterable<Course> findOwnedCourses(User userFromToken) {
|
|
return courseRepo.findAllOwnedCoures(userFromToken);
|
|
}
|
|
|
|
|
|
|
|
public boolean modifyData(long id, Map<String, Object> updates, Role role) {
|
|
Course target = courseRepo.findById(id);
|
|
|
|
if (target == null)
|
|
return false;
|
|
|
|
if (role == Role.Teacher){
|
|
for (Map.Entry<String, Object> entry : updates.entrySet()){
|
|
if (entry.getKey().equals("title")){
|
|
target.setTitle((String) entry.getValue());
|
|
courseRepo.save(target);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (role != Role.Secretary)
|
|
return false;
|
|
|
|
for (Map.Entry<String ,Object> entry: updates.entrySet()){
|
|
switch (entry.getKey()){
|
|
case "title":
|
|
target.setTitle((String) entry.getValue());
|
|
break;
|
|
case "credits":
|
|
target.setCredits((Integer) entry.getValue());
|
|
break;
|
|
case "owner":
|
|
if (((User) entry.getValue() ).getRole() != Role.Teacher)
|
|
break;
|
|
|
|
target.setOwner((User) entry.getValue());
|
|
break;
|
|
}
|
|
}
|
|
courseRepo.save(target);
|
|
return true;
|
|
}
|
|
|
|
public void delete(Course course) {
|
|
courseRepo.delete(course);
|
|
}
|
|
}
|