1
0
forked from PGL/Clyde

cleaned the services

This commit is contained in:
2024-03-16 20:25:35 +01:00
parent 97b57b361d
commit 382d3c203a
16 changed files with 67 additions and 87 deletions

View File

@@ -39,18 +39,7 @@ public class AuthenticatorService {
return inscriptionService.save(inscriptionRequest);
}
public boolean isNotSecretaryOrAdmin(String authorization){
if (authorization ==null)
return true;
User poster = getUserFromToken(authorization);
if (poster == null) return true;
return poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin;
}
public boolean IsNotIn(Role[] roles, String token){
public boolean isNotIn(Role[] roles, String token){
if (token == null)
return true;

View File

@@ -5,7 +5,6 @@ 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
@@ -25,21 +24,25 @@ public class CourseService {
return courseRepo.findById(id);
}
public Course modifyData(long id, Map<String, Object> updates, Role role) {
public boolean modifyData(long id, Map<String, Object> updates, Role role) {
Course target = courseRepo.findById(id);
if (target == null)
return 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());
return courseRepo.save(target);
courseRepo.save(target);
return true;
}
}
}
if (role != Role.Secretary)
return false;
for (Map.Entry<String ,Object> entry: updates.entrySet()){
switch (entry.getKey()){
case "title":
@@ -49,14 +52,14 @@ public class CourseService {
target.setCredits((Integer) entry.getValue());
break;
case "owner":
target.setOwner((User) entry.getValue()); //todo check if is a teacher !
if (((User) entry.getValue() ).getRole() != Role.Teacher)
break;
target.setOwner((User) entry.getValue());
break;
}
}
return courseRepo.save(target);
}
public void delete(Long id) {
courseRepo.deleteById(id);
courseRepo.save(target);
return true;
}
}

View File

@@ -1,13 +1,10 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.CourseRepository;
import ovh.herisson.Clyde.Repositories.CurriculumCourseRepository;
import ovh.herisson.Clyde.Repositories.CurriculumRepository;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Curriculum;
import ovh.herisson.Clyde.Tables.CurriculumCourse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@@ -17,27 +14,21 @@ public class CurriculumCourseService {
private final CurriculumCourseRepository curriculumCourseRepo;
private final CourseRepository courseRepo;
private final CurriculumRepository curriculumRepo;
public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository, CourseRepository courseRepo, CurriculumRepository curriculumRepo) {
public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository) {
this.curriculumCourseRepo = curriculumCourseRepository;
this.courseRepo = courseRepo;
this.curriculumRepo = curriculumRepo;
}
public void save(CurriculumCourse curriculumCourse){
curriculumCourseRepo.save(curriculumCourse);
}
public Iterable<CurriculumCourse> findAll(){
return curriculumCourseRepo.findAll();
}
public Map<String, Object> getDepthCurriculum(Curriculum curriculum){
if (curriculum == null)
return null;
HashMap<String ,Object> toReturn = new HashMap<>();
ArrayList<Course> courses = new ArrayList<>();
for (Course c: curriculumCourseRepo.findCoursesByCurriculum(curriculum)){
@@ -61,8 +52,4 @@ public class CurriculumCourseService {
}
return toReturn;
}
}

View File

@@ -15,12 +15,8 @@ public class CurriculumService {
public Curriculum save(Curriculum curriculum){
return curriculumRepo.save(curriculum);
}
public Curriculum findById(long id){
return curriculumRepo.findById(id);
}
public void delete(Long id) {
curriculumRepo.deleteById(id);
}
}
}

View File

@@ -35,6 +35,9 @@ public class StorageService {
public StorageFile store(MultipartFile file, FileType fileType) {
if (file == null || file.getOriginalFilename() == null)
return null;
if (file.getOriginalFilename().isEmpty()){return null;}
UUID uuid = UUID.randomUUID();

View File

@@ -22,21 +22,23 @@ public class TeacherCourseService {
public boolean saveAll(Iterable<Long> teacherIds, Course course){
if (course == null)
if (course == null || teacherIds == null)
return false;
ArrayList<Long> addedIds = new ArrayList<>();
ArrayList<User> toAdd = new ArrayList<>();
for (Long teacherId : teacherIds){
User teacher = userRepo.findById((long) teacherId);
if ( teacher== null){
return false;
}
if (!addedIds.contains(teacherId))
if (!toAdd.contains(teacher))
{
teacherCourseRepo.save(new TeacherCourse(teacher,course));
addedIds.add(teacherId);
toAdd.add(teacher);
}
}
for (User teacher: toAdd){
teacherCourseRepo.save(new TeacherCourse(teacher,course));
}
return true;
}
}

View File

@@ -40,16 +40,19 @@ public class TokenService {
public User getUserFromToken(String token) {
Token tokenRep = tokenRepo.getByToken(token);
if (tokenRep == null) return null;
if (tokenRep == null)
return null;
return tokenRep.getUser();
}
public void saveToken(Token token){
//Si l'utilisateur a déja 5 token delete celui qui devait expirer le plus vite
ArrayList<Token> tokenList = tokenRepo.getByUserOrderByExpirationDate(token.getUser());
while(tokenList.size() >= 5){
tokenRepo.delete(tokenList.get(0));
tokenList.remove(tokenList.get(0));
tokenRepo.delete(tokenList.getFirst());
tokenList.remove(tokenList.getFirst());
}
tokenRepo.save(token);
}
@@ -67,5 +70,5 @@ public class TokenService {
tokenRepo.delete(t);
}
}
};
}
}

View File

@@ -17,8 +17,15 @@ public class UserService {
}
/** return the user identified by th identifier
*
* @param identifier can be an email or the RegNo
* @return the identified user
*/
public User getUser(String identifier){
if (identifier == null) return null;
if (identifier == null)
return null;
try {
int id = Integer.parseInt(identifier);
return userRepo.findById(id);
@@ -33,7 +40,7 @@ public class UserService {
*
* @param poster the user wanting to modify target's data
* @param updates the changes to be made
* @param target the user to update
* @param targetId the id of the user to update
* @return if the changes were done or not
*/
public boolean modifyData(long targetId, Map<String ,Object> updates, User poster){
@@ -45,8 +52,6 @@ public class UserService {
if (poster.getRegNo().equals(target.getRegNo())){
for (Map.Entry<String, Object> entry : updates.entrySet()){
if ( entry.getKey().equals("regNo") || entry.getKey().equals("role")) {return false;}
switch (entry.getKey()){
case "firstName":
target.setFirstName((String) entry.getValue());
@@ -82,13 +87,14 @@ public class UserService {
{
for (Map.Entry<String, Object> entry : updates.entrySet()){
if ( !entry.getKey().equals("role")) {return false;}
if ( entry.getKey().equals("role")) {
if (entry.getValue() == Role.Admin) {return false;}
if (entry.getValue() == Role.Admin) {return false;}
target.setRole((Role) entry.getValue());
userRepo.save(target);
return true;
target.setRole((Role) entry.getValue());
userRepo.save(target);
return true;
}
}
}
return false;