Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed update service methods and implemented cucumber tests #108

Merged
merged 16 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from 10 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
Expand Up @@ -58,6 +58,13 @@ public List<CoopDto> getAllCoops() {
return ControllerUtils.convertCoopListToDto(coops);
}

@GetMapping("/student/{id}")
public List<CoopDto> getCoopByStudentId(@PathVariable int id) {
Student s = studentService.getStudentById(id);
List<Coop> coops = coopService.getAllCoopsByStudent(s);
return ControllerUtils.convertCoopListToDto(coops);
}

@PostMapping("")
public CoopDto createCoop(@RequestBody CoopDto coopDto) {
Coop coop = new Coop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -110,8 +111,8 @@ public EmployerReportDto updateEmployerReport(
@RequestParam("status") String status,
@RequestParam("title") String title,
@RequestParam("coop_id") int coopId,
@RequestParam("report_sections") Set<ReportSectionDto> rsDtos,
@RequestParam("employer_id") int employerId) {
@RequestParam("employer_id") int employerId,
@RequestBody Set<ReportSectionDto> rsDtos) {
EmployerReport reportToUpdate = employerReportService.getEmployerReport(id);

Coop coop = coopService.getCoopById(coopId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package ca.mcgill.cooperator.controller;

import ca.mcgill.cooperator.dto.EmployerReportDto;
import ca.mcgill.cooperator.dto.ReportSectionDto;
import ca.mcgill.cooperator.dto.StudentReportDto;
import ca.mcgill.cooperator.model.EmployerReport;
import ca.mcgill.cooperator.model.ReportSection;
import ca.mcgill.cooperator.model.StudentReport;
import ca.mcgill.cooperator.service.EmployerReportService;
import ca.mcgill.cooperator.service.ReportSectionService;
import ca.mcgill.cooperator.service.StudentReportService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("report-sections")
public class ReportSectionController {

@Autowired ReportSectionService reportSectionService;
@Autowired StudentReportService studentReportService;
@Autowired EmployerReportService employerReportService;

@GetMapping("/{id}")
public ReportSectionDto getReportSectionById(@PathVariable int id) {
ReportSection reportSection = reportSectionService.getReportSection(id);
return ControllerUtils.convertToDto(reportSection);
}

@GetMapping("")
public List<ReportSectionDto> getAllReportSections() {
List<ReportSection> reportSections = reportSectionService.getAllReportSections();
return ControllerUtils.convertReportSectionListToDto(reportSections);
}

@PostMapping("")
public ReportSectionDto createReportSection(@RequestBody ReportSectionDto reportSectionDto) {
ReportSection reportSection =
reportSectionService.createReportSection(
reportSectionDto.getTitle(), reportSectionDto.getContent());
return ControllerUtils.convertToDto(reportSection);
}

@PutMapping("")
public ReportSectionDto updateReportSection(@RequestBody ReportSectionDto reportSectionDto) {
ReportSection reportSection =
reportSectionService.getReportSection(reportSectionDto.getId());
StudentReport studentReport = null;
EmployerReport employerReport = null;

if (reportSectionDto.getStudentReport() != null) {
StudentReportDto studentReportDto = reportSectionDto.getStudentReport();
studentReport = studentReportService.getStudentReport(studentReportDto.getId());
}

if (reportSectionDto.getEmployerReport() != null) {
EmployerReportDto employerReportDto = reportSectionDto.getEmployerReport();
employerReport = employerReportService.getEmployerReport(employerReportDto.getId());
}

reportSection =
reportSectionService.updateReportSection(
reportSection,
reportSectionDto.getTitle(),
reportSectionDto.getContent(),
studentReport,
employerReport);

return ControllerUtils.convertToDto(reportSection);
}

@DeleteMapping("/{id}")
public void deleteReportSection(@PathVariable int id) {
ReportSection reportSection = reportSectionService.getReportSection(id);
reportSectionService.deleteReportSection(reportSection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ca.mcgill.cooperator.model.Student;
import ca.mcgill.cooperator.model.StudentReport;
import ca.mcgill.cooperator.service.CoopService;
import ca.mcgill.cooperator.service.ReportSectionService;
import ca.mcgill.cooperator.service.StudentReportService;
import ca.mcgill.cooperator.service.StudentService;
import java.util.HashSet;
Expand All @@ -24,6 +25,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -37,6 +39,7 @@ public class StudentReportController {
@Autowired StudentReportService studentReportService;
@Autowired StudentService studentService;
@Autowired CoopService coopService;
@Autowired ReportSectionService reportSectionService;

/**
* Gets a StudentReport by ID
Expand Down Expand Up @@ -105,26 +108,27 @@ public StudentReportDto createStudentReport(
}

/**
* Updates a StudentReport
* Updates a StudentReport, put not the actual contents of report
susanmatuszewski marked this conversation as resolved.
Show resolved Hide resolved
*
* @param reportId
* @param file
* @param status
* @param title
* @param rsDtos
* @param coopId
* @return the updated StudentReport
susanmatuszewski marked this conversation as resolved.
Show resolved Hide resolved
*/
@PutMapping("/{reportId}")
@PutMapping("/{id}")
public StudentReportDto updateStudentReport(
@PathVariable int reportId,
@PathVariable int id,
@ModelAttribute("file") MultipartFile file,
@RequestParam("status") String status,
@RequestParam("title") String title,
@RequestParam("report_sections") Set<ReportSectionDto> rsDtos,
@RequestParam("coop_id") int coopId) {
StudentReport reportToUpdate = studentReportService.getStudentReport(reportId);
Set<ReportSection> sections = ControllerUtils.convertReportSectionSetToDomainObject(rsDtos);
@RequestParam("coop_id") int coopId,
@RequestBody Set<ReportSectionDto> rsDtos) {
StudentReport reportToUpdate = studentReportService.getStudentReport(id);

Set<ReportSection> sections = convertReportSectionSetToDomainObject(rsDtos);

Coop coop = coopService.getCoopById(coopId);
ReportStatus reportStatus = ReportStatus.valueOf(status);

Expand Down Expand Up @@ -154,4 +158,13 @@ public final ResponseEntity<Exception> handleAllExceptions(RuntimeException ex)
ex.printStackTrace();
return new ResponseEntity<Exception>(ex, HttpStatus.INTERNAL_SERVER_ERROR);
}

public Set<ReportSection> convertReportSectionSetToDomainObject(Set<ReportSectionDto> rsDtos) {
Set<ReportSection> reports = new HashSet<ReportSection>();
for (ReportSectionDto rsDto : rsDtos) {
ReportSection rs = reportSectionService.getReportSection(rsDto.getId());
reports.add(rs);
}
return reports;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import ca.mcgill.cooperator.model.Coop;
import ca.mcgill.cooperator.model.CoopStatus;
import ca.mcgill.cooperator.model.Student;
import java.util.List;
import org.springframework.data.repository.CrudRepository;

public interface CoopRepository extends CrudRepository<Coop, Integer> {
List<Coop> findByStatus(CoopStatus coopStatus);

List<Coop> findByStudent(String firstName);
List<Coop> findByStudent(Student s);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class ReportSectionDto {
private StudentReportDto studentReport;
private EmployerReportDto employerReport;

public ReportSectionDto() {}

public ReportSectionDto(
int id,
String title,
Expand Down
6 changes: 3 additions & 3 deletions Backend/src/main/java/ca/mcgill/cooperator/model/Coop.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public Set<StudentReport> getStudentReports() {
return this.studentReports;
}

public void setStudentReports(Set<StudentReport> studentReports) {
public void setStudentReports(Set<StudentReport> srs) {
if (this.studentReports == null) {
this.studentReports = studentReports;
this.studentReports = srs;
} else {
this.studentReports.clear();
this.studentReports.addAll(studentReports);
this.studentReports.addAll(srs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public enum ReportStatus {
COMPLETED,
INCOMPLETE,
UNDER_REVIEW,
SUBMITTED,
NOT_SUBMITTED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,40 @@ public Admin updateAdmin(
if (a == null) {
error.append("Admin to update cannot be null! ");
}
if (firstName == null || firstName.trim().length() == 0) {
if (firstName != null && firstName.trim().length() == 0) {
error.append("Admin first name cannot be empty! ");
}
if (lastName == null || lastName.trim().length() == 0) {
if (lastName != null && lastName.trim().length() == 0) {
error.append("Admin last name cannot be empty! ");
}
if (email == null || email.trim().length() == 0) {
if (email != null && email.trim().length() == 0) {
error.append("Admin email cannot be empty! ");
} else if (!ServiceUtils.isValidEmail(email)) {
error.append("Admin email must be a valid email! ");
}
if (sentNotifications == null) {
error.append("Admin sent notifications cannot be null!");
} else if (email != null && !ServiceUtils.isValidEmail(email)) {
error.append("Admin email must be a valid email!");
}
if (error.length() > 0) {
throw new IllegalArgumentException(error.toString().trim());
}
// set fields if they're not null

for (Notification n : sentNotifications) {
n.setSender(a);
notificationRepository.save(n);
if (firstName != null && firstName.trim().length() > 0) {
susanmatuszewski marked this conversation as resolved.
Show resolved Hide resolved
a.setFirstName(firstName.trim());
}
if (lastName != null && lastName.trim().length() > 0) {
a.setLastName(lastName.trim());
}
if (email != null && email.trim().length() > 0 && ServiceUtils.isValidEmail(email)) {
a.setEmail(email.trim());
}

if (sentNotifications != null) {
for (Notification n : sentNotifications) {
n.setSender(a);
notificationRepository.save(n);
}

a.setSentNotifications(sentNotifications);
}
a.setFirstName(firstName.trim());
a.setLastName(lastName.trim());
a.setEmail(email.trim());
a.setSentNotifications(sentNotifications);

return adminRepository.save(a);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,41 +165,57 @@ public Company updateCompany(
if (c == null) {
error.append("Company to update cannot be null! ");
}
if (name == null || name.trim().length() == 0) {
if (name != null && name.trim().length() == 0) {
error.append("Company name cannot be empty! ");
}
if (city == null || city.trim().length() == 0) {
if (city != null && city.trim().length() == 0) {
error.append("Company city cannot be empty! ");
}
if (region == null || region.trim().length() == 0) {
if (region != null && region.trim().length() == 0) {
error.append("Company region cannot be empty! ");
}
if (country == null || country.trim().length() == 0) {
if (country != null && country.trim().length() == 0) {
error.append("Company country cannot be empty! ");
}
// employees cannot be null but can be empty
if (employees == null) {
error.append("Company employees cannot be null! ");
}
if (companyExists(name, city, region, country)) {
error.append("Company with this name and location already exists!");
}
if (error.length() > 0) {
throw new IllegalArgumentException(error.toString().trim());
}

if (name == null) {
name = c.getName();
}
if (city == null) {
city = c.getCity();
}
if (region == null) {
region = c.getRegion();
}
if (country == null) {
country = c.getCountry();
}

if (companyExists(name, city, region, country)) {
String error_message = "Company with this name and location already exists!";
susanmatuszewski marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException(error_message);
}

c.setName(name.trim());
c.setCity(city);
c.setRegion(region);
c.setCountry(country);
c.setEmployees(employees);
c.setCity(city.trim());
c.setRegion(region.trim());
c.setCountry(country.trim());

if (employees != null) {
c.setEmployees(employees);
}

companyRepository.save(c);

for (EmployerContact employerContact : employees) {
// We do this in case a new employee does not have the Company field set
employerContact.setCompany(c);
employerContactRepository.save(employerContact);
if (employees != null) {
for (EmployerContact employerContact : employees) {
// We do this in case a new employee does not have the Company field set
employerContact.setCompany(c);
employerContactRepository.save(employerContact);
}
}

return companyRepository.save(c);
Expand Down
Loading