Compare commits

...

3 Commits

3 changed files with 41 additions and 26 deletions

View File

@ -9,6 +9,8 @@ import ovh.herisson.Clyde.Services.CourseService;
import ovh.herisson.Clyde.Services.TeacherCourseService; import ovh.herisson.Clyde.Services.TeacherCourseService;
import ovh.herisson.Clyde.Tables.Course; import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Role;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
@RestController @RestController
@ -28,7 +30,7 @@ public class CourseController {
} }
@GetMapping("/course/{id}") @GetMapping("/course/{id}")
public ResponseEntity<Course> getCourse(@RequestHeader("Authorization") String token, @PathVariable long id){ public ResponseEntity<HashMap<String,Object>> getCourse(@RequestHeader("Authorization") String token, @PathVariable long id){
if (authServ.getUserFromToken(token) == null) if (authServ.getUserFromToken(token) == null)
return new UnauthorizedResponse<>(null); return new UnauthorizedResponse<>(null);
@ -37,7 +39,7 @@ public class CourseController {
if (foundCourse == null) if (foundCourse == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(foundCourse, HttpStatus.OK); return new ResponseEntity<>(courseWithoutPassword(foundCourse), HttpStatus.OK);
} }
@ -85,4 +87,16 @@ public class CourseController {
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
private HashMap<String,Object> courseWithoutPassword(Course course){
HashMap<String ,Object> toReturn = new HashMap<>();
toReturn.put("courseId",course.getCourseID());
toReturn.put("credits",course.getCredits());
toReturn.put("title", course.getTitle());
toReturn.put("owner", authServ.userWithoutPassword(course.getOwner()));
return toReturn;
}
} }

View File

@ -35,7 +35,7 @@ public class UserController {
User user = authServ.getUserFromToken(token); User user = authServ.getUserFromToken(token);
if (user == null) return new UnauthorizedResponse<>(null); if (user == null) return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(userWithoutPassword(user), HttpStatus.OK); return new ResponseEntity<>(authServ.userWithoutPassword(user), HttpStatus.OK);
} }
@PostMapping("/user") @PostMapping("/user")
@ -44,7 +44,7 @@ public class UserController {
if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService,Role.Secretary},token)) if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService,Role.Secretary},token))
return new UnauthorizedResponse<>(null); return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(userWithoutPassword(userService.save(user)),HttpStatus.CREATED); return new ResponseEntity<>(authServ.userWithoutPassword(userService.save(user)),HttpStatus.CREATED);
} }
@GetMapping("/users") @GetMapping("/users")
@ -57,7 +57,7 @@ public class UserController {
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>(); ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
for (User u :users){ for (User u :users){
withoutPassword.add(userWithoutPassword(u)); withoutPassword.add(authServ.userWithoutPassword(u));
} }
return new ResponseEntity<>(withoutPassword, HttpStatus.OK); return new ResponseEntity<>(withoutPassword, HttpStatus.OK);
} }
@ -95,30 +95,10 @@ public class UserController {
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>(); ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
for (User t: teachers){ for (User t: teachers){
withoutPassword.add(userWithoutPassword(t)); withoutPassword.add(authServ.userWithoutPassword(t));
} }
return new ResponseEntity<>(withoutPassword, HttpStatus.OK); return new ResponseEntity<>(withoutPassword, HttpStatus.OK);
} }
/** return user's data except password
* @param user the user to return
* @return all the user data without the password
*/
private HashMap<String,Object> userWithoutPassword(User user){
HashMap<String,Object> toReturn = new HashMap<>();
toReturn.put("regNo",user.getRegNo());
toReturn.put("lastName",user.getLastName());
toReturn.put("firstName",user.getFirstName());
toReturn.put("email", user.getEmail());
toReturn.put("address",user.getAddress());
toReturn.put("birthDate",user.getBirthDate());
toReturn.put("country",user.getCountry());
toReturn.put("profilePictureUrl",user.getProfilePictureUrl());
toReturn.put("role",user.getRole());
return toReturn;
}
} }

View File

@ -7,6 +7,7 @@ import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
@Service @Service
public class AuthenticatorService { public class AuthenticatorService {
@ -52,5 +53,25 @@ public class AuthenticatorService {
} }
return true; return true;
} }
/** return user's data except password
* @param user the user to return
* @return all the user data without the password
*/
public HashMap<String,Object> userWithoutPassword(User user){
HashMap<String,Object> toReturn = new HashMap<>();
toReturn.put("regNo",user.getRegNo());
toReturn.put("lastName",user.getLastName());
toReturn.put("firstName",user.getFirstName());
toReturn.put("email", user.getEmail());
toReturn.put("address",user.getAddress());
toReturn.put("birthDate",user.getBirthDate());
toReturn.put("country",user.getCountry());
toReturn.put("profilePictureUrl",user.getProfilePictureUrl());
toReturn.put("role",user.getRole());
return toReturn;
}
} }