Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #153 from sef-global/development
Browse files Browse the repository at this point in the history
ScholarX - v1.2 Release
  • Loading branch information
Gravewalker666 authored Jun 2, 2021
2 parents dd51ea0 + 397e8be commit d74aff9
Show file tree
Hide file tree
Showing 18 changed files with 562 additions and 130 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ deploy:
repo: sef-global/scholarx
branch: master
skip_cleanup: true

32 changes: 32 additions & 0 deletions src/main/java/org/sefglobal/scholarx/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.ImmutableList;
import org.sefglobal.scholarx.oauth.AuthAccessTokenResponseConverter;
import org.sefglobal.scholarx.oauth.OAuthAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -28,13 +31,20 @@
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
Expand Down Expand Up @@ -78,6 +88,28 @@ public void onLogoutSuccess(HttpServletRequest request,
.accessTokenResponseClient(authorizationCodeTokenResponseClient());
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();

configuration.setAllowedMethods(ImmutableList.of("GET", "POST", "PUT", "DELETE"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
configuration.setAllowedOrigins(ImmutableList.of("http://localhost:3000"));
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
}

private OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeTokenResponseClient() {
OAuth2AccessTokenResponseHttpMessageConverter converter = new OAuth2AccessTokenResponseHttpMessageConverter();
converter.setTokenResponseConverter(new AuthAccessTokenResponseConverter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import org.sefglobal.scholarx.exception.BadRequestException;
import org.sefglobal.scholarx.exception.NoContentException;
import org.sefglobal.scholarx.exception.ResourceNotFoundException;
import org.sefglobal.scholarx.model.Mentor;
import org.sefglobal.scholarx.model.Profile;
import org.sefglobal.scholarx.model.Program;
import org.sefglobal.scholarx.model.*;
import org.sefglobal.scholarx.service.ProgramService;
import org.sefglobal.scholarx.util.EnrolmentState;
import org.sefglobal.scholarx.util.ProgramState;
import org.sefglobal.scholarx.util.QuestionCategory;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -61,11 +60,11 @@ public List<Mentor> getAllMentorsByProgramId(
public Mentor applyAsMentor(
@PathVariable long id,
Authentication authentication,
@Valid @RequestBody Mentor mentor
@Valid @RequestBody List<MentorResponse> responses
)
throws ResourceNotFoundException, BadRequestException {
Profile profile = (Profile) authentication.getPrincipal();
return programService.applyAsMentor(id, profile.getId(), mentor);
return programService.applyAsMentor(id, profile.getId(), responses);
}

@GetMapping("/{id}/mentor")
Expand All @@ -79,18 +78,6 @@ public Mentor getLoggedInMentor(
return programService.getLoggedInMentor(id, profile.getId());
}

@PutMapping("/{id}/application")
@ResponseStatus(HttpStatus.OK)
public Mentor updateMentorData(
@PathVariable long id,
Authentication authentication,
@Valid @RequestBody Mentor mentor
)
throws ResourceNotFoundException, BadRequestException {
Profile profile = (Profile) authentication.getPrincipal();
return programService.updateMentorData(profile.getId(), id, mentor);
}

@GetMapping("/{id}/mentee/mentors")
@ResponseStatus(HttpStatus.OK)
public List<Mentor> getAppliedMentors(
Expand All @@ -117,4 +104,37 @@ public Mentor getSelectedMentor(
Profile profile = (Profile) authentication.getPrincipal();
return programService.getSelectedMentor(id, profile.getId());
}

@GetMapping("/{id}/questions/{category}")
@ResponseStatus(HttpStatus.OK)
public List<Question> getQuestions(@PathVariable long id,
@PathVariable QuestionCategory category) throws ResourceNotFoundException {
return programService.getQuestions(id, category);
}

@GetMapping("/{id}/responses/mentor")
@ResponseStatus(HttpStatus.OK)
public List<MentorResponse> getMentorResponses(
@PathVariable long id,
Authentication authentication,
@RequestParam(required = false) Long mentorId
)
throws ResourceNotFoundException {
if (mentorId != null) {
return programService.getMentorResponses(mentorId);
}
Profile profile = (Profile) authentication.getPrincipal();
return programService.getMentorResponses(id, profile.getId());
}

@PutMapping("/{id}/responses/mentor")
public List<MentorResponse> editMentorResponses(
@PathVariable long id,
Authentication authentication,
@RequestBody List<MentorResponse> responses
)
throws ResourceNotFoundException{
Profile profile = (Profile) authentication.getPrincipal();
return programService.editMentorResponses(id, profile.getId(), responses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.sefglobal.scholarx.model.Mentee;
import org.sefglobal.scholarx.model.Mentor;
import org.sefglobal.scholarx.model.Program;
import org.sefglobal.scholarx.model.Question;
import org.sefglobal.scholarx.service.ProgramService;
import org.sefglobal.scholarx.util.EnrolmentState;
import org.sefglobal.scholarx.util.QuestionCategory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -68,4 +70,12 @@ public List<Mentee> getAllMenteesByProgramId(@PathVariable long id)
throws ResourceNotFoundException {
return programService.getAllMenteesByProgramId(id);
}

@PostMapping("/{id}/questions/{category}")
@ResponseStatus(HttpStatus.OK)
public List<Question> addQuestions(@PathVariable long id,
@PathVariable QuestionCategory category,
@RequestBody List<Question> questions) throws ResourceNotFoundException {
return programService.addQuestions(id, category, questions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.sefglobal.scholarx.controller.admin;

import org.sefglobal.scholarx.exception.ResourceNotFoundException;
import org.sefglobal.scholarx.model.Question;
import org.sefglobal.scholarx.service.QuestionService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/questions")
public class QuestionController {
private final QuestionService questionService;

public QuestionController(QuestionService questionService) {
this.questionService = questionService;
}

@PutMapping
@ResponseStatus(HttpStatus.OK)
public List<Question> editQuestions(@RequestBody List<Question> questions) throws ResourceNotFoundException {
return questionService.editQuestions(questions);
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteQuestion(@PathVariable long id) throws ResourceNotFoundException{
questionService.deleteQuestion(id);
}
}
29 changes: 2 additions & 27 deletions src/main/java/org/sefglobal/scholarx/model/Mentor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
Expand All @@ -14,39 +13,15 @@
@JsonIgnoreProperties({"createdAt", "updatedAt", "mentees"})
public class Mentor extends EnrolledUser {

@Column
private String application;

@Column
private String prerequisites;
@OneToMany(mappedBy = "mentor")
private List<MentorResponse> mentorResponses;

public Mentor() {
}

public Mentor(String application, String prerequisites) {
this.application = application;
this.prerequisites = prerequisites;
}

@OneToMany(mappedBy = "mentor")
private List<Mentee> mentees = new ArrayList<>();

public String getApplication() {
return application;
}

public void setApplication(String application) {
this.application = application;
}

public String getPrerequisites() {
return prerequisites;
}

public void setPrerequisites(String prerequisites) {
this.prerequisites = prerequisites;
}

public List<Mentee> getMentees() {
return mentees;
}
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/sefglobal/scholarx/model/MentorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.sefglobal.scholarx.model;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "mentor_response")
public class MentorResponse implements Serializable {

@EmbeddedId
MentorResponseId id;

@ManyToOne(cascade = CascadeType.ALL)
@MapsId("questionId")
@JoinColumn(name = "question_id")
private Question question;

@ManyToOne(cascade = CascadeType.ALL)
@MapsId("mentorId")
@JoinColumn(name = "mentor_id")
private Mentor mentor;

@Column(nullable = false, columnDefinition = "TEXT")
private String response;

public MentorResponse() {}

public MentorResponse(Question question, Mentor mentor, String response) {
this.id = new MentorResponseId(question.getId(), mentor.getId());
this.question = question;
this.mentor = mentor;
this.response = response;
}

public MentorResponseId getId() {
return id;
}

public void setId(MentorResponseId id) {
this.id = id;
}

public Question getQuestion() {
return question;
}

public void setQuestion(Question question) {
this.question = question;
}

public Mentor getMentor() {
return mentor;
}

public void setMentor(Mentor mentor) {
this.mentor = mentor;
}

public String getResponse() {
return response;
}

public void setResponse(String response) {
this.response = response;
}
}
50 changes: 50 additions & 0 deletions src/main/java/org/sefglobal/scholarx/model/MentorResponseId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.sefglobal.scholarx.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;

@Embeddable
public class MentorResponseId implements Serializable {
@Column(name = "question_id")
private long questionId;
@Column(name = "mentor_id")
private long mentorId;

public MentorResponseId() {}

public MentorResponseId(long questionId, long mentorId) {
this.questionId = questionId;
this.mentorId = mentorId;
}

public void setQuestionId(long questionId) {
this.questionId = questionId;
}

public long getQuestionId() {
return questionId;
}

public void setMentorId(long mentorId) {
this.mentorId = mentorId;
}

public long getMentorId() {
return mentorId;
}

@Override
public int hashCode() {
return Objects.hash(questionId, mentorId);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
MentorResponseId id = (MentorResponseId) obj;
return id.mentorId == this.mentorId && id.questionId == this.questionId;
}
}
Loading

0 comments on commit d74aff9

Please sign in to comment.