1
0
forked from PGL/Clyde

63 lines
1.8 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){
return courseRepo.save(course);
}
public Course findById(long id){
return courseRepo.findById(id);
}
public Course modifyData(long id, Map<String, Object> updates, Role role) {
Course target = courseRepo.findById(id);
if (target == null)
return null;
if (role == Role.Teacher){
for (Map.Entry<String, Object> entry : updates.entrySet()){
if (entry.getKey().equals("title")){
target.setTitle((String) entry.getValue());
return courseRepo.save(target);
}
}
}
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":
target.setOwner((User) entry.getValue()); //todo check if is a teacher !
break;
}
}
return courseRepo.save(target);
}
public void delete(Long id) {
courseRepo.deleteById(id);
}
}