| 1 | package com.finki.icare.controller;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.AddSlotRequest;
|
|---|
| 4 | import com.finki.icare.dto.ConsultationSlotsDTO;
|
|---|
| 5 | import com.finki.icare.service.ConsultationSlotService;
|
|---|
| 6 | import com.finki.icare.utils.AuthUtils;
|
|---|
| 7 | import lombok.RequiredArgsConstructor;
|
|---|
| 8 | import org.springframework.format.annotation.DateTimeFormat;
|
|---|
| 9 | import org.springframework.http.ResponseEntity;
|
|---|
| 10 | import org.springframework.security.core.Authentication;
|
|---|
| 11 | import org.springframework.web.bind.annotation.*;
|
|---|
| 12 |
|
|---|
| 13 | import java.time.LocalDate;
|
|---|
| 14 |
|
|---|
| 15 | @RestController
|
|---|
| 16 | @RequestMapping("/api/consultation-slots")
|
|---|
| 17 | @CrossOrigin(origins = "http://localhost:3000")
|
|---|
| 18 | @RequiredArgsConstructor
|
|---|
| 19 | public class ConsultationSlotController {
|
|---|
| 20 |
|
|---|
| 21 | private final ConsultationSlotService consultationSlotService;
|
|---|
| 22 |
|
|---|
| 23 | @GetMapping("/{therapistId}")
|
|---|
| 24 | public ResponseEntity<ConsultationSlotsDTO> getSlots(
|
|---|
| 25 | @PathVariable Integer therapistId,
|
|---|
| 26 | Authentication authentication
|
|---|
| 27 | ) {
|
|---|
| 28 | String userType = AuthUtils.getUserType(authentication);
|
|---|
| 29 | ConsultationSlotsDTO slots = consultationSlotService.getTherapistSlots(therapistId, userType);
|
|---|
| 30 | return ResponseEntity.ok(slots);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | @PostMapping("/{therapistId}")
|
|---|
| 34 | public ResponseEntity<ConsultationSlotsDTO> addSlot(
|
|---|
| 35 | @PathVariable Integer therapistId,
|
|---|
| 36 | @RequestBody AddSlotRequest request,
|
|---|
| 37 | Authentication authentication
|
|---|
| 38 | ) {
|
|---|
| 39 | Integer currentUserId = AuthUtils.getUserId(authentication);
|
|---|
| 40 | String userType = AuthUtils.getUserType(authentication);
|
|---|
| 41 | ConsultationSlotsDTO slots = consultationSlotService.addSlot(therapistId, request, currentUserId, userType);
|
|---|
| 42 | return ResponseEntity.ok(slots);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | @DeleteMapping("/{therapistId}/{date}")
|
|---|
| 46 | public ResponseEntity<ConsultationSlotsDTO> removeSlot(
|
|---|
| 47 | @PathVariable Integer therapistId,
|
|---|
| 48 | @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
|
|---|
| 49 | Authentication authentication
|
|---|
| 50 | ) {
|
|---|
| 51 | Integer currentUserId = AuthUtils.getUserId(authentication);
|
|---|
| 52 | String userType = AuthUtils.getUserType(authentication);
|
|---|
| 53 | ConsultationSlotsDTO slots = consultationSlotService.removeSlot(therapistId, date, currentUserId, userType);
|
|---|
| 54 | return ResponseEntity.ok(slots);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|