Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.booleanuk.cohorts.controllers;

import com.booleanuk.cohorts.models.*;
import com.booleanuk.cohorts.payload.request.CohortRequest;
import com.booleanuk.cohorts.payload.request.ProfileRequest;
import com.booleanuk.cohorts.payload.response.*;
import com.booleanuk.cohorts.repository.CohortRepository;
import com.booleanuk.cohorts.repository.CourseRepository;
import com.booleanuk.cohorts.repository.ProfileRepository;
import com.booleanuk.cohorts.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
Expand All @@ -20,6 +27,9 @@ public class CohortController {
@Autowired
private CohortRepository cohortRepository;

@Autowired
private CourseRepository courseRepository;

@Autowired
private UserRepository userRepository;

Expand Down Expand Up @@ -47,17 +57,89 @@ public ResponseEntity<Response> getCohortById(@PathVariable int id) {
}

@GetMapping("/teacher/{id}")
public ResponseEntity<?> getCohorstByUserId(@PathVariable int id) {
public ResponseEntity<?> getCohortByUserId(@PathVariable int id) {
User user = userRepository.findById(id).orElse(null);
if (user == null) return new ResponseEntity<>("User for id " + Integer.valueOf(id) + " not found", HttpStatus.NOT_FOUND);
if (user == null) return new ResponseEntity<>("User for id " + id + " not found", HttpStatus.NOT_FOUND);
Profile teacherProfile = profileRepository.findById(user.getProfile().getId()).orElse(null);
if (teacherProfile == null) return new ResponseEntity<>("Profile for user " + user.getEmail() +" not found", HttpStatus.NOT_FOUND);

CohortResponse cohortResponse = new CohortResponse();
Cohort cohort = teacherProfile.getCohort();
cohortResponse.set(cohort);

return new ResponseEntity<CohortResponse>(cohortResponse, HttpStatus.OK);
return new ResponseEntity<>(cohortResponse, HttpStatus.OK);
}


@PatchMapping("{id}")
public ResponseEntity<?> editCohortById(@PathVariable int id, @RequestBody CohortRequest cohortRequest){
Cohort cohort = cohortRepository.findById(id).orElse(null);
if (cohort == null){
return new ResponseEntity<>("Cohort not found", HttpStatus.NOT_FOUND);
}

List<Profile> profilesToInclude = cohortRequest.getProfileIds().stream()
.map(profileId -> profileRepository.findById(profileId)
.orElseThrow(() -> new RuntimeException("Profile with id " + profileId + " not found")))
.toList();

List<Course> courses = cohortRequest.getCourseIds().stream()
.map(courseId -> courseRepository.findById(courseId)
.orElseThrow(() -> new RuntimeException("Profile with id " + courseId + " not found")))
.collect(Collectors.toList());

if (cohortRequest.getName().isBlank()) {
return new ResponseEntity<>("Name cannot be blank", HttpStatus.BAD_REQUEST);
}

if (cohortRequest.getStart_date().isBlank() || cohortRequest.getEnd_date().isBlank()) {
return new ResponseEntity<>("Date cannot be blank", HttpStatus.BAD_REQUEST);
}

cohort.setCohort_courses(courses);

List<User> usersToInclude = userRepository.findAll().stream().filter(it ->
profilesToInclude.contains(it.getProfile())).toList();

List<User> usersToExclude = userRepository.findAll().stream().filter(it ->
it.getCohort().getId() == cohort.getId() && !(profilesToInclude.contains(it.getProfile()))).toList();

List<Profile> profilesToExclude = usersToExclude.stream().map(User::getProfile).toList().stream().filter(it ->
it.getCohort().getId() == cohort.getId() && !(profilesToInclude.contains(it))).toList();

cohort.setName(cohortRequest.getName());
cohort.setStartDate(LocalDate.parse(cohortRequest.getStart_date()));
cohort.setEndDate(LocalDate.parse(cohortRequest.getEnd_date()));

Cohort cohortRes = cohortRepository.findById(99).orElse(null);
if (cohortRes == null) {
return new ResponseEntity<>("Could not find RESERVE", HttpStatus.BAD_REQUEST);
}

for (User user: usersToExclude){
user.setCohort(cohortRes);
}

for (Profile profile: profilesToExclude){
profile.setCohort(cohortRes);
List<Profile> prevProf = cohortRes.getProfiles();
prevProf.add(profile);
cohortRes.setProfiles(prevProf);
}

for (User user: usersToInclude){
user.setCohort(cohort);
}
for (Profile prof : profilesToInclude){
prof.setCohort(cohort);
}
profileRepository.saveAll(profilesToInclude);
profileRepository.saveAll(profilesToExclude);
userRepository.saveAll(usersToInclude);
userRepository.saveAll(usersToExclude);
cohortRepository.save(cohortRes);

return new ResponseEntity<>(cohortRepository.save(cohort), HttpStatus.OK);
}

@PatchMapping("/teacher/{id}")
Expand All @@ -76,3 +158,4 @@ public ResponseEntity<?> addStudentToCohort(@PathVariable int id, @RequestBody P
return new ResponseEntity<>(profileRepository.save(profile), HttpStatus.OK);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import com.booleanuk.cohorts.repository.PostRepository;
import com.booleanuk.cohorts.repository.ProfileRepository;
import com.booleanuk.cohorts.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/com/booleanuk/cohorts/models/Cohort.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

/*
Expand All @@ -17,6 +19,7 @@
*/

@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "cohorts")
Expand All @@ -25,6 +28,8 @@ public class Cohort {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
private String name;

@ManyToMany
@JsonIgnoreProperties("cohorts")
Expand All @@ -39,15 +44,36 @@ public class Cohort {
@JsonIgnoreProperties("cohort")
private List<Profile> profiles;

@Column
private LocalDate startDate;

@Column
private LocalDate endDate;

public Cohort(int id) {
this.id = id;
}

public Cohort(LocalDate startDate, LocalDate endDate){
this.startDate = startDate;
this.endDate = endDate;
}

public Cohort(String name, LocalDate startDate, LocalDate endDate){
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
}

public Cohort(int id, String name, LocalDate startDate, LocalDate endDate) {
this.id = id;
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
}

@Override
public String toString(){

return "Cohort Id: " + this.id;
}
}
1 change: 0 additions & 1 deletion src/main/java/com/booleanuk/cohorts/models/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class Course {
private List<Cohort> cohorts;

public Course(String name) {
this.id = id;
this.name = name;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
package com.booleanuk.cohorts.payload.request;

import com.booleanuk.cohorts.models.Course;
import com.booleanuk.cohorts.models.Profile;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class CohortRequest {
}
private String name;
private List<Integer> courseIds;
private List<Integer> profileIds;
private String start_date;
private String end_date;

public CohortRequest(){}}

Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.booleanuk.cohorts.payload.request;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class CommentRequest {
@NotBlank
private String body;
Expand All @@ -14,20 +18,5 @@ public CommentRequest(String body, int userId) {
this.body = body;
this.userId = userId;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.booleanuk.cohorts.payload.request;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;

@Getter
public class CourseRequest {
@NotBlank
private String name;
Expand All @@ -12,7 +14,5 @@ public CourseRequest(String name){
this.name = name;
}

public String getName() { return name; }


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.booleanuk.cohorts.payload.request;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class PostRequest {
@NotBlank
private String content;
Expand All @@ -14,20 +18,5 @@ public PostRequest(String content, int userId) {
this.content = content;
this.userId = userId;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ public class ProfileRequest {

public ProfileRequest(){}

public int getCohort() { return cohort; }
public int getUserId() { return userId; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,8 @@ public class StudentRequest {
private String password;
private String bio;

public StudentRequest(){}

public String getPhoto() { return photo; }
public String getFirst_name() { return first_name; }
public String getLast_name() { return last_name; }
public String getUsername() { return username; }
public String getGithub_username() { return github_username; }

public String getEmail() { return email; }
public String getMobile() { return mobile; }
public String getPassword() { return password; }
public String getBio() { return bio; }
public StudentRequest() {
}

}

Loading