-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from McGill-ECSE321-Winter2021/dev
Dev
- Loading branch information
Showing
141 changed files
with
16,875 additions
and
799 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
SCRS-Backend/src/main/java/ca/mcgill/ecse321/scrs/controller/AppointmentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package ca.mcgill.ecse321.scrs.controller; | ||
|
||
import ca.mcgill.ecse321.scrs.dto.AppointmentDto; | ||
import ca.mcgill.ecse321.scrs.dto.TimeslotDto; | ||
import ca.mcgill.ecse321.scrs.model.Appointment; | ||
import ca.mcgill.ecse321.scrs.model.Customer; | ||
import ca.mcgill.ecse321.scrs.model.Timeslot; | ||
import ca.mcgill.ecse321.scrs.service.AppointmentService; | ||
import ca.mcgill.ecse321.scrs.service.CustomerService; | ||
import ca.mcgill.ecse321.scrs.service.TimeslotService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.sql.Date; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.Calendar; | ||
import java.util.List; | ||
|
||
import static ca.mcgill.ecse321.scrs.controller.Helper.*; | ||
|
||
@RestController | ||
@RequestMapping(path = "/api/appointment", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public class AppointmentController | ||
{ | ||
|
||
@Autowired | ||
AppointmentService appointmentService; | ||
@Autowired | ||
CustomerService customerService; | ||
@Autowired | ||
TimeslotService timeslotService; | ||
|
||
|
||
@GetMapping(path = {"/getall/{id}", "/getall/{id}/"}) | ||
public ResponseEntity<List<AppointmentDto>> getAllAppointments(@PathVariable String id) { | ||
if(id == null)return new ResponseEntity<>(null, HttpStatus.NOT_ACCEPTABLE); | ||
int ID = Integer.parseInt(id); | ||
|
||
Customer customer = customerService.getCustomerByID(ID); | ||
if(customer == null) return new ResponseEntity<>(null, HttpStatus.NOT_ACCEPTABLE); | ||
|
||
List<Appointment> list = appointmentService.getAppointmentsByCustomer(customer); | ||
List<AppointmentDto> dtoList = new ArrayList<>(); | ||
|
||
if(list != null){ | ||
for (Appointment appointment : list) | ||
{ | ||
dtoList.add(convertToDto(appointment)); | ||
} | ||
} | ||
return new ResponseEntity<>(dtoList, HttpStatus.OK); | ||
} | ||
|
||
@GetMapping(path = {"/notifications/{id}", "/notifications/{id}/"}) | ||
public ResponseEntity<List<AppointmentDto>> notifications(@PathVariable String id) { | ||
if(id == null)return new ResponseEntity<>(null, HttpStatus.NOT_ACCEPTABLE); | ||
int ID = Integer.parseInt(id); | ||
|
||
Customer customer = customerService.getCustomerByID(ID); | ||
if(customer == null) return new ResponseEntity<>(null, HttpStatus.NOT_ACCEPTABLE); | ||
|
||
List<Appointment> list = appointmentService.getAppointmentsByCustomer(customer); | ||
|
||
if(list != null){ | ||
//finding the same date next week | ||
Date now = new Date(LocalDate.now().toEpochDay()); | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(now); | ||
calendar.add(Calendar.DATE, 7); | ||
Date nextWeek = new Date(calendar.getTimeInMillis()); | ||
|
||
List<AppointmentDto> notificationList = new ArrayList<>(); | ||
for (Appointment appointment : list) | ||
{ | ||
List<Timeslot> timeslots = appointment.getTimeslots(); | ||
ArrayList<Integer> newTimeslots = new ArrayList<>(); | ||
for (Timeslot timeslot : timeslots) | ||
{ | ||
if (timeslot.getStartDate().compareTo(now) >= 0 && timeslot.getStartDate().compareTo(nextWeek) <= 0) | ||
{ | ||
newTimeslots.add(timeslot.getTimeSlotID()); | ||
} | ||
} | ||
if (!newTimeslots.isEmpty()) | ||
{ | ||
AppointmentDto appointmentDto = new AppointmentDto(appointment.getAppointmentID(), appointment.getAppointmentType().toString(), appointment.getService(), appointment.getNote(), appointment.getRating(), appointment.getFeedback(), appointment.getPaid(), appointment.getCustomer().getScrsUserId(), newTimeslots); | ||
notificationList.add(appointmentDto); | ||
} | ||
} | ||
|
||
return new ResponseEntity<>(notificationList, HttpStatus.OK); | ||
}else return new ResponseEntity<>(null, HttpStatus.OK); | ||
} | ||
|
||
@PostMapping(value = {"/book", "/book/"}) | ||
public ResponseEntity<AppointmentDto> bookAppointment(@RequestBody AppointmentDto appointmentDto) | ||
{ | ||
if (appointmentDto == null) | ||
{ | ||
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED); | ||
} | ||
try { | ||
List<Timeslot> timeslots = timeslotService.getTimeslotsById(appointmentDto.getTimeslotsId()); | ||
if (timeslots == null) | ||
{ | ||
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED); | ||
} | ||
Customer customer = customerService.getCustomerByID(appointmentDto.getCustomerId()); | ||
Appointment appointment = appointmentService.createAppointment(appointmentDto.getAppointmentType(), | ||
appointmentDto.getService(), appointmentDto.getNote(), appointmentDto.getPaymentStatus(), | ||
customer, timeslots.toArray(new Timeslot[0])); | ||
return new ResponseEntity<>(convertToDto(appointment), HttpStatus.OK); | ||
} catch (Exception e) { | ||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
@PutMapping(value = {"/pay", "/pay/"}) | ||
public ResponseEntity<AppointmentDto> payAppointment(@RequestParam(name = "appointmentId") int appointmentId) | ||
{ | ||
try | ||
{ | ||
Appointment appointment = appointmentService.getAppointmentById(appointmentId); | ||
if (appointment == null) { | ||
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED); | ||
} | ||
appointment.setPaid(true); | ||
appointment = appointmentService.modifyAppointment(convertToDto(appointment)); | ||
return new ResponseEntity<>(convertToDto(appointment), HttpStatus.OK); | ||
} catch (Exception e) { | ||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
@PutMapping(value = {"/rate", "/rate/"}) | ||
public ResponseEntity<AppointmentDto> rateAppointment(@RequestParam(name = "appointmentId") int appointmentId, @RequestParam(name = "rating") int rating) | ||
{ | ||
if (rating > 10 || rating < 0) | ||
{ | ||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
try { | ||
Appointment appointment = appointmentService.rateAppointment(appointmentId, rating); | ||
return new ResponseEntity<>(convertToDto(appointment), HttpStatus.OK); | ||
} catch (Exception e) | ||
{ | ||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
@PutMapping(value = {"/modifyAppointment", "/modifyAppointment/"}) | ||
public ResponseEntity<AppointmentDto> modifyAppointment(@RequestBody AppointmentDto appointmentDto) | ||
{ | ||
if (appointmentDto == null) | ||
{ | ||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
try { | ||
Appointment modifiedAppt = appointmentService.modifyAppointment(appointmentDto); | ||
return new ResponseEntity<>(convertToDto(modifiedAppt), HttpStatus.OK); | ||
} catch (Exception e) | ||
{ | ||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
SCRS-Backend/src/main/java/ca/mcgill/ecse321/scrs/controller/AssistantController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package ca.mcgill.ecse321.scrs.controller; | ||
|
||
import ca.mcgill.ecse321.scrs.dto.AssistantDto; | ||
import ca.mcgill.ecse321.scrs.dto.CustomerDto; | ||
import ca.mcgill.ecse321.scrs.model.Assistant; | ||
import ca.mcgill.ecse321.scrs.service.AssistantService; | ||
import ca.mcgill.ecse321.scrs.service.SCRSUserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import static ca.mcgill.ecse321.scrs.controller.Helper.*; | ||
|
||
@RestController | ||
@RequestMapping(path = "/api/assistant") | ||
public class AssistantController | ||
{ | ||
@Autowired | ||
AssistantService assistantService; | ||
@Autowired | ||
SCRSUserService scrsUserService; | ||
|
||
@PostMapping(value = {"/create", "/create/"}) | ||
public ResponseEntity<AssistantDto> createAssistant(@RequestBody Assistant assistant) | ||
{ | ||
if (assistant == null) | ||
{ | ||
return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.EXPECTATION_FAILED); | ||
// Invalid assistant. Please submit a valid assistant account to be created. | ||
} | ||
if (assistantService.getAssistantByEmail(assistant.getEmail()) != null) | ||
{ | ||
return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.ALREADY_REPORTED); | ||
// Email already in use, please try a different email address. | ||
} | ||
Assistant newAssistant = assistantService.createAssistant(assistant.getEmail(), assistant.getName(), hash(assistant.getPassword()), assistant.getPhone()); | ||
return new ResponseEntity<>(convertToDto(newAssistant), HttpStatus.OK); | ||
} | ||
|
||
@PutMapping(value = {"/update", "/update/"}) | ||
public ResponseEntity<AssistantDto> updateAssistant(@RequestBody Assistant assistant, @CookieValue(value = "id", defaultValue = "-1") String ID) | ||
{ | ||
int id = Integer.parseInt(ID); | ||
if (id == -1) | ||
{ | ||
// TODO handle no login error with cookies (uncomment next line) | ||
//return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.UNAUTHORIZED); | ||
// Please login to modify an assistant account. | ||
} | ||
if (assistant == null) | ||
{ | ||
return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.EXPECTATION_FAILED); | ||
// Invalid assistant | ||
} | ||
if (!isAdmin(scrsUserService.getSCRSUserByID(id))) //does not have permission to edit. | ||
{ | ||
// TODO handle bad login error with cookies (uncomment next line) | ||
//return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.UNAUTHORIZED); | ||
// You do not have permission to edit an admin account. | ||
} | ||
if (assistantService.getAssistantByID(assistant.getScrsUserId()) == null) | ||
{ | ||
return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.NOT_ACCEPTABLE); | ||
// No such assistant found. | ||
} | ||
Assistant updatedAssistant = assistantService.updateAssistantInfo(assistant); | ||
return new ResponseEntity<>(convertToDto(updatedAssistant), HttpStatus.OK); | ||
} | ||
|
||
@DeleteMapping(value = {"/delete/{id}", "/delete/{id}/"}) | ||
public ResponseEntity<AssistantDto> deleteAssistant(@PathVariable String id, @CookieValue(value = "id", defaultValue = "-1") String ID) | ||
{ | ||
int assistantID = Integer.parseInt(id); | ||
int idCookie = Integer.parseInt(ID); | ||
if (idCookie == -1) | ||
{ | ||
// TODO handle no login error with cookies (uncomment next line) | ||
//return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.UNAUTHORIZED); | ||
//Please login to delete an assistant account. | ||
} | ||
Assistant assistant = assistantService.getAssistantByID(assistantID); | ||
if (assistant == null) | ||
{ | ||
return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.NOT_ACCEPTABLE); | ||
// Invalid assistant. Please submit a valid assistant account to be deleted. | ||
} | ||
if (!isAdmin(scrsUserService.getSCRSUserByID(idCookie))) //does not have permission to edit. | ||
{ | ||
// TODO handle bad login error with cookies (uncomment next line) | ||
//return new ResponseEntity<AssistantDto>(new AssistantDto(), HttpStatus.UNAUTHORIZED); | ||
// You do not have permission to edit this account. | ||
} | ||
return new ResponseEntity<>(convertToDto(assistantService.deleteAssistant(assistant)), HttpStatus.OK); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
SCRS-Backend/src/main/java/ca/mcgill/ecse321/scrs/controller/CookieController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ca.mcgill.ecse321.scrs.controller; | ||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public class CookieController | ||
{ | ||
|
||
@GetMapping("/") | ||
public String readCookie(@CookieValue(value = "id", defaultValue = "-1") String id){ | ||
return id; | ||
} | ||
} |
Oops, something went wrong.