From 57809fc773340dde31fe83f84b8028040781481b Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Sat, 4 Nov 2023 19:28:11 -0300 Subject: [PATCH 1/8] Added new test cases and Jacoco --- pom.xml | 22 +++ .../controller/ExchangeControllerTest.java | 136 ++++++++++++++++++ .../controller/WorkControllerTest.java | 102 +++++++++++++ 3 files changed, 260 insertions(+) create mode 100644 src/test/java/com/example/slabiak/appointmentscheduler/controller/ExchangeControllerTest.java create mode 100644 src/test/java/com/example/slabiak/appointmentscheduler/controller/WorkControllerTest.java diff --git a/pom.xml b/pom.xml index b19fc7c..b64f5ee 100644 --- a/pom.xml +++ b/pom.xml @@ -185,6 +185,28 @@ + + org.jacoco + jacoco-maven-plugin + 0.8.5 + + + + prepare-agent + + + + + report + test + + report + + + + + + com.google.cloud.tools jib-maven-plugin diff --git a/src/test/java/com/example/slabiak/appointmentscheduler/controller/ExchangeControllerTest.java b/src/test/java/com/example/slabiak/appointmentscheduler/controller/ExchangeControllerTest.java new file mode 100644 index 0000000..ca5bca6 --- /dev/null +++ b/src/test/java/com/example/slabiak/appointmentscheduler/controller/ExchangeControllerTest.java @@ -0,0 +1,136 @@ +package com.example.slabiak.appointmentscheduler.controller; + +import com.example.slabiak.appointmentscheduler.controller.ExchangeController; +import com.example.slabiak.appointmentscheduler.entity.Appointment; +import com.example.slabiak.appointmentscheduler.security.CustomUserDetails; +import com.example.slabiak.appointmentscheduler.service.AppointmentService; +import com.example.slabiak.appointmentscheduler.service.ExchangeService; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +public class ExchangeControllerTest { + + private ExchangeController exchangeController; + + @Mock + private ExchangeService exchangeService; + + @Mock + private AppointmentService appointmentService; + + @Mock + private CustomUserDetails currentUser; + + @Mock + private Model model; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + exchangeController = new ExchangeController(exchangeService, appointmentService); + } + + @Test + public void testShowEligibleAppointmentsToExchange() { + // Arrange + int oldAppointmentId = 1; + List eligibleAppointments = new ArrayList<>(); + when(exchangeService.getEligibleAppointmentsForExchange(oldAppointmentId)).thenReturn(eligibleAppointments); + + // Act + String viewName = exchangeController.showEligibleAppointmentsToExchange(oldAppointmentId, model); + + // Assert + assertEquals("exchange/listProposals", viewName); + verify(model).addAttribute("appointmentId", oldAppointmentId); + verify(model).addAttribute("numberOfEligibleAppointments", eligibleAppointments.size()); + verify(model).addAttribute("eligibleAppointments", eligibleAppointments); + } + + @Test + public void testShowExchangeSummaryScreen_ExchangePossible() { + // Arrange + int oldAppointmentId = 1; + int newAppointmentId = 2; + when(exchangeService.checkIfExchangeIsPossible(oldAppointmentId, newAppointmentId, currentUser.getId())).thenReturn(true); + + // Act + String viewName = exchangeController.showExchangeSummaryScreen(oldAppointmentId, newAppointmentId, model, currentUser); + + // Assert + verify(model).addAttribute("oldAppointment", appointmentService.getAppointmentByIdWithAuthorization(oldAppointmentId)); + verify(model).addAttribute("newAppointment", appointmentService.getAppointmentById(newAppointmentId)); + assertEquals("exchange/exchangeSummary", viewName); + } + + @Test + public void testShowExchangeSummaryScreen_ExchangeNotPossible() { + // Arrange + int oldAppointmentId = 1; + int newAppointmentId = 2; + when(exchangeService.checkIfExchangeIsPossible(oldAppointmentId, newAppointmentId, currentUser.getId())).thenReturn(false); + + // Act + String viewName = exchangeController.showExchangeSummaryScreen(oldAppointmentId, newAppointmentId, model, currentUser); + + // Assert + verify(model, never()).addAttribute("oldAppointment", appointmentService.getAppointmentByIdWithAuthorization(oldAppointmentId)); + verify(model, never()).addAttribute("newAppointment", appointmentService.getAppointmentById(newAppointmentId)); + assertEquals("redirect:/appointments/all", viewName); + } + + + + @Test + public void testProcessExchangeRequest_Failure() { + // Arrange + int oldAppointmentId = 1; + int newAppointmentId = 2; + when(exchangeService.requestExchange(oldAppointmentId, newAppointmentId, currentUser.getId())).thenReturn(false); + + // Act + String viewName = exchangeController.processExchangeRequest(oldAppointmentId, newAppointmentId, model, currentUser); + + // Assert + verify(model).addAttribute("message", "Error! Exchange not sent!"); + assertEquals("exchange/requestConfirmation", viewName); + } + + @Test + public void testProcessExchangeAcceptation() { + // Arrange + int exchangeId = 1; + + // Act + String viewName = exchangeController.processExchangeAcceptation(exchangeId, model, currentUser); + + // Assert + verify(exchangeService).acceptExchange(exchangeId, currentUser.getId()); + assertEquals("redirect:/appointments/all", viewName); + } + + @Test + public void testProcessExchangeRejection() { + // Arrange + int exchangeId = 1; + + // Act + String viewName = exchangeController.processExchangeRejection(exchangeId, model, currentUser); + + // Assert + verify(exchangeService).rejectExchange(exchangeId, currentUser.getId()); + assertEquals("redirect:/appointments/all", viewName); + } +} + diff --git a/src/test/java/com/example/slabiak/appointmentscheduler/controller/WorkControllerTest.java b/src/test/java/com/example/slabiak/appointmentscheduler/controller/WorkControllerTest.java new file mode 100644 index 0000000..6b7cfcc --- /dev/null +++ b/src/test/java/com/example/slabiak/appointmentscheduler/controller/WorkControllerTest.java @@ -0,0 +1,102 @@ +package com.example.slabiak.appointmentscheduler.controller; + +import com.example.slabiak.appointmentscheduler.entity.Work; +import com.example.slabiak.appointmentscheduler.service.WorkService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.*; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.info.BuildProperties; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.web.servlet.ModelAndView; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class WorkControllerTest { + @InjectMocks + private WorkController workController; + + @Mock + private WorkService workService; + + @MockBean + private BuildProperties buildProperties; + + + + @BeforeEach + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void testShowAllWorks() { + // Create a list of works to be returned by the service + List works = new ArrayList<>(); + works.add(new Work()); + + // Create the WorkService mock + WorkService workService = Mockito.mock(WorkService.class); + + // Stub the service to return the list of works + Mockito.when(workService.getAllWorks()).thenReturn(works); + + // Create the model + Model model = mock(Model.class); + + Mockito.lenient().when(model.addAttribute(Mockito.eq("works"), Mockito.anyList())).thenReturn(model); + + // Create the controller + WorkController workController = new WorkController(workService); + + // Perform the test + String viewName = workController.showAllWorks(model); + + // Assert the view name and model attributes as needed + + assertEquals("works/list", viewName); + } + + + @Test + public void testShowFormForUpdateWork() { + // Arrange + Model model = mock(Model.class); + int workId = 1; + Work work1 = new Work(); + work1.setId(1);// Create a Work object + + // Act + String viewName = workController.showFormForUpdateWork(workId, model); + + // Assert + // Verify that the expected Work object is added to the model + assertEquals("works/createOrUpdateWorkForm", viewName); // Replace with the correct view name + } + + @Test + public void testShowFormForAddWork() { + // Arrange + Model model = mock(Model.class); + + // Act + String viewName = workController.showFormForAddWork(model); + + // Assert + ArgumentCaptor workCaptor = ArgumentCaptor.forClass(Work.class); + verify(model).addAttribute(eq("work"), workCaptor.capture()); + assertEquals("works/createOrUpdateWorkForm", viewName); // Replace with the correct view name + } + +} From ab35ba3d28b91997768db8210141d7acf925d945 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 21:57:29 -0400 Subject: [PATCH 2/8] Decompose conditional --- .../appointmentscheduler/model/DayPlan.java | 98 ++++++++++++++----- 1 file changed, 74 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java index 0be2dda..a0fdc86 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -13,41 +13,91 @@ public DayPlan() { breaks = new ArrayList(); } + //Before Refactoring for decompose conditional + +// public List timePeroidsWithBreaksExcluded() { +// ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); +// timePeroidsWithBreaksExcluded.add(getWorkingHours()); +// List breaks = getBreaks(); +// +// if (!breaks.isEmpty()) { +// ArrayList toAdd = new ArrayList(); +// for (TimePeroid break1 : breaks) { +// if (break1.getStart().isBefore(workingHours.getStart())) { +// break1.setStart(workingHours.getStart()); +// } +// if (break1.getEnd().isAfter(workingHours.getEnd())) { +// break1.setEnd(workingHours.getEnd()); +// } +// for (TimePeroid peroid : timePeroidsWithBreaksExcluded) { +// if (break1.getStart().equals(peroid.getStart()) && break1.getEnd().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { +// peroid.setStart(break1.getEnd()); +// } +// if (break1.getStart().isAfter(peroid.getStart()) && break1.getStart().isBefore(peroid.getEnd()) && break1.getEnd().equals(peroid.getEnd())) { +// peroid.setEnd(break1.getStart()); +// } +// if (break1.getStart().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { +// toAdd.add(new TimePeroid(peroid.getStart(), break1.getStart())); +// peroid.setStart(break1.getEnd()); +// } +// } +// } +// timePeroidsWithBreaksExcluded.addAll(toAdd); +// Collections.sort(timePeroidsWithBreaksExcluded); +// } +// +// +// return timePeroidsWithBreaksExcluded; +// } + + + //After Refactoring for Decompose conditional + public List timePeroidsWithBreaksExcluded() { ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); timePeroidsWithBreaksExcluded.add(getWorkingHours()); List breaks = getBreaks(); if (!breaks.isEmpty()) { - ArrayList toAdd = new ArrayList(); - for (TimePeroid break1 : breaks) { - if (break1.getStart().isBefore(workingHours.getStart())) { - break1.setStart(workingHours.getStart()); - } - if (break1.getEnd().isAfter(workingHours.getEnd())) { - break1.setEnd(workingHours.getEnd()); - } - for (TimePeroid peroid : timePeroidsWithBreaksExcluded) { - if (break1.getStart().equals(peroid.getStart()) && break1.getEnd().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { - peroid.setStart(break1.getEnd()); - } - if (break1.getStart().isAfter(peroid.getStart()) && break1.getStart().isBefore(peroid.getEnd()) && break1.getEnd().equals(peroid.getEnd())) { - peroid.setEnd(break1.getStart()); - } - if (break1.getStart().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { - toAdd.add(new TimePeroid(peroid.getStart(), break1.getStart())); - peroid.setStart(break1.getEnd()); - } - } - } - timePeroidsWithBreaksExcluded.addAll(toAdd); - Collections.sort(timePeroidsWithBreaksExcluded); + processBreaks(timePeroidsWithBreaksExcluded, breaks); } - return timePeroidsWithBreaksExcluded; } + private void processBreaks(List timePeroidsWithBreaksExcluded, List breaks) { + ArrayList toAdd = new ArrayList<>(); + for (TimePeroid break1 : breaks) { + adjustBreakStartAndEnd(break1); + updateTimePeroids(timePeroidsWithBreaksExcluded, break1, toAdd); + } + timePeroidsWithBreaksExcluded.addAll(toAdd); + Collections.sort(timePeroidsWithBreaksExcluded); + } + + private void adjustBreakStartAndEnd(TimePeroid break1) { + if (break1.getStart().isBefore(workingHours.getStart())) { + break1.setStart(workingHours.getStart()); + } + if (break1.getEnd().isAfter(workingHours.getEnd())) { + break1.setEnd(workingHours.getEnd()); + } + } + + private void updateTimePeroids(List timePeroidsWithBreaksExcluded, TimePeroid break1, List toAdd) { + for (TimePeroid peroid : timePeroidsWithBreaksExcluded) { + if (break1.getStart().equals(peroid.getStart()) && break1.getEnd().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { + peroid.setStart(break1.getEnd()); + } + if (break1.getStart().isAfter(peroid.getStart()) && break1.getStart().isBefore(peroid.getEnd()) && break1.getEnd().equals(peroid.getEnd())) { + peroid.setEnd(break1.getStart()); + } + if (break1.getStart().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { + toAdd.add(new TimePeroid(peroid.getStart(), break1.getStart())); + peroid.setStart(break1.getEnd()); + } + } + } public TimePeroid getWorkingHours() { return workingHours; } From 891dd9fd7978b70e345fd8d06a20d40e1f384ecb Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 22:18:50 -0400 Subject: [PATCH 3/8] Extract Method --- .../service/impl/EmailServiceImpl.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/EmailServiceImpl.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/EmailServiceImpl.java index 7569092..ebfd6e9 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/EmailServiceImpl.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/EmailServiceImpl.java @@ -39,7 +39,8 @@ public EmailServiceImpl(JavaMailSender javaMailSender, SpringTemplateEngine temp this.baseUrl = baseUrl; } - @Async + //Before Extract Method + /*@Async @Override public void sendEmail(String to, String subject, String templateName, Context templateContext, File attachment) { try { @@ -65,8 +66,42 @@ public void sendEmail(String to, String subject, String templateName, Context te log.error("Error while adding attachment to email, error is {}", e.getLocalizedMessage()); } + }*/ + + //After Extract Method + @Async + @Override + public void sendEmail(String to, String subject, String templateName, Context templateContext, File attachment) { + try { + MimeMessage message = createMimeMessageWithAttachment(to, subject, templateName, templateContext, attachment); + javaMailSender.send(message); + } catch (MessagingException e) { + log.error("Error while adding attachment to email, error is {}", e.getLocalizedMessage()); + } } + private MimeMessage createMimeMessageWithAttachment(String to, String subject, String templateName, Context templateContext, File attachment) throws MessagingException { + MimeMessage message = javaMailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, + MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, + StandardCharsets.UTF_8.name()); + + String html = templateEngine.process("email/" + templateName, templateContext); + + helper.setTo(to); + helper.setFrom("appointmentscheduler@gmail.com"); + helper.setSubject(subject); + helper.setText(html, true); + + if (attachment != null) { + helper.addAttachment("invoice", attachment); + } + + return message; + } + + + @Async @Override public void sendAppointmentFinishedNotification(Appointment appointment) { From c85c1fc4b7fd9cb8ff38dc2a045020bc543d7d21 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 22:19:29 -0400 Subject: [PATCH 4/8] Decompose Conditional --- .../service/AppointmentService.java | 2 +- .../service/impl/AppointmentServiceImpl.java | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/AppointmentService.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/AppointmentService.java index c90defa..1e51c72 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/AppointmentService.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/AppointmentService.java @@ -34,7 +34,7 @@ public interface AppointmentService { List getAppointmentsByProviderAtDay(int providerId, LocalDate day); - List getAppointmentsByCustomerAtDay(int providerId, LocalDate day); + List getAppointmentsByCustomerAtDayAndTimePeriods(int providerId, LocalDate day); List getConfirmedAppointmentsByCustomerId(int customerId); diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java index 98826d4..2b0fd90 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java @@ -87,12 +87,17 @@ public List getAppointmentByProviderId(int providerId) { public List getAppointmentsByProviderAtDay(int providerId, LocalDate day) { return appointmentRepository.findByProviderIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); } - - @Override - public List getAppointmentsByCustomerAtDay(int providerId, LocalDate day) { +//Before renaming a method +// @Override +// public List getAppointmentsByCustomerAtDay(int providerId, LocalDate day) { +// return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); +// } + //After renaming a method + public List getAppointmentsByCustomerAtDayAndTimePeriods(int providerId, LocalDate day) { return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); } + @Override public List getAvailableHours(int providerId, int customerId, int workId, LocalDate date) { Provider p = userService.getProviderById(providerId); @@ -100,7 +105,7 @@ public List getAvailableHours(int providerId, int customerId, int wo DayPlan selectedDay = workingPlan.getDay(date.getDayOfWeek().toString().toLowerCase()); List providerAppointments = getAppointmentsByProviderAtDay(providerId, date); - List customerAppointments = getAppointmentsByCustomerAtDay(customerId, date); + List customerAppointments = getAppointmentsByCustomerAtDayAndTimePeriods(customerId, date); List availablePeroids = selectedDay.timePeroidsWithBreaksExcluded(); availablePeroids = excludeAppointmentsFromTimePeroids(availablePeroids, providerAppointments); From 7f9d45a8c6e1c837202457a41fbd81c078e463cf Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 23:18:11 -0400 Subject: [PATCH 5/8] for conditional polymorphism and move method --- .../model/AdjusterAfterEnd.java | 11 ++ .../model/AdjusterBeforeStart.java | 13 +++ .../appointmentscheduler/model/DayPlan.java | 107 +++++++++++------- .../model/TimePeriod.java | 36 ++++++ .../model/TimePeriodAdjuster.java | 5 + .../service/RetailCustomerService.java | 23 ++++ .../service/UserService.java | 4 +- .../service/impl/RetailCustomerService.java | 69 +++++++++++ .../service/impl/UserServiceImpl.java | 32 +++--- 9 files changed, 241 insertions(+), 59 deletions(-) create mode 100644 src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java create mode 100644 src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java create mode 100644 src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java create mode 100644 src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java create mode 100644 src/main/java/com/example/slabiak/appointmentscheduler/service/RetailCustomerService.java create mode 100644 src/main/java/com/example/slabiak/appointmentscheduler/service/impl/RetailCustomerService.java diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java new file mode 100644 index 0000000..5f55dfa --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java @@ -0,0 +1,11 @@ +package com.example.slabiak.appointmentscheduler.model; + +class AdjusterAfterEnd implements TimePeriodAdjuster { + @Override + public TimePeriod adjust(TimePeriod original, TimePeriod breakPeriod) { + if (breakPeriod.getEnd().isAfter(original.getEnd())) { + original.setEnd(breakPeriod.getEnd()); + } + return original; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java new file mode 100644 index 0000000..aa424af --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java @@ -0,0 +1,13 @@ +package com.example.slabiak.appointmentscheduler.model; + + +class AdjusterBeforeStart implements TimePeriodAdjuster { + @Override + public TimePeriod adjust(TimePeriod original, TimePeriod breakPeriod) { + if (breakPeriod.getStart().isBefore(original.getStart())) { + original.setStart(breakPeriod.getStart()); + } + return original; + } +} + diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java index a0fdc86..3517062 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -1,6 +1,7 @@ package com.example.slabiak.appointmentscheduler.model; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -13,46 +14,69 @@ public DayPlan() { breaks = new ArrayList(); } - //Before Refactoring for decompose conditional - -// public List timePeroidsWithBreaksExcluded() { -// ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); -// timePeroidsWithBreaksExcluded.add(getWorkingHours()); -// List breaks = getBreaks(); -// -// if (!breaks.isEmpty()) { -// ArrayList toAdd = new ArrayList(); -// for (TimePeroid break1 : breaks) { -// if (break1.getStart().isBefore(workingHours.getStart())) { -// break1.setStart(workingHours.getStart()); -// } -// if (break1.getEnd().isAfter(workingHours.getEnd())) { -// break1.setEnd(workingHours.getEnd()); -// } -// for (TimePeroid peroid : timePeroidsWithBreaksExcluded) { -// if (break1.getStart().equals(peroid.getStart()) && break1.getEnd().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { -// peroid.setStart(break1.getEnd()); -// } -// if (break1.getStart().isAfter(peroid.getStart()) && break1.getStart().isBefore(peroid.getEnd()) && break1.getEnd().equals(peroid.getEnd())) { -// peroid.setEnd(break1.getStart()); -// } -// if (break1.getStart().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { -// toAdd.add(new TimePeroid(peroid.getStart(), break1.getStart())); -// peroid.setStart(break1.getEnd()); -// } -// } -// } -// timePeroidsWithBreaksExcluded.addAll(toAdd); -// Collections.sort(timePeroidsWithBreaksExcluded); -// } -// -// -// return timePeroidsWithBreaksExcluded; -// } - - - //After Refactoring for Decompose conditional +//for conditionl polymorphism + public List timePeriodsWithBreaksExcluded() { + List timePeriodsWithBreaksExcluded = new ArrayList<>(); + timePeriodsWithBreaksExcluded.add(getWorkingHours()); + List breaks = getBreaks(); + + if (!breaks.isEmpty()) { + List adjusters = Arrays.asList( + new AdjusterBeforeStart(), + new AdjusterAfterEnd() + // Add more adjusters as needed + ); + + for (TimePeroid breakPeriod : breaks) { + for (TimePeriod timePeriod : timePeriodsWithBreaksExcluded) { + for (TimePeriodAdjuster adjuster : adjusters) { + adjuster.adjust(timePeriod, breakPeriod); + } + } + } + + Collections.sort(timePeriodsWithBreaksExcluded); + } + return timePeriodsWithBreaksExcluded; + } + +/* public List timePeroidsWithBreaksExcluded() { + ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); + timePeroidsWithBreaksExcluded.add(getWorkingHours()); + List breaks = getBreaks(); + + if (!breaks.isEmpty()) { + ArrayList toAdd = new ArrayList(); + for (TimePeroid break1 : breaks) { + if (break1.getStart().isBefore(workingHours.getStart())) { + break1.setStart(workingHours.getStart()); + } + if (break1.getEnd().isAfter(workingHours.getEnd())) { + break1.setEnd(workingHours.getEnd()); + } + for (TimePeroid peroid : timePeroidsWithBreaksExcluded) { + if (break1.getStart().equals(peroid.getStart()) && break1.getEnd().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { + peroid.setStart(break1.getEnd()); + } + if (break1.getStart().isAfter(peroid.getStart()) && break1.getStart().isBefore(peroid.getEnd()) && break1.getEnd().equals(peroid.getEnd())) { + peroid.setEnd(break1.getStart()); + } + if (break1.getStart().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { + toAdd.add(new TimePeroid(peroid.getStart(), break1.getStart())); + peroid.setStart(break1.getEnd()); + } + } + } + timePeroidsWithBreaksExcluded.addAll(toAdd); + Collections.sort(timePeroidsWithBreaksExcluded); + } + + + return timePeroidsWithBreaksExcluded; + }*/ + +/* public List timePeroidsWithBreaksExcluded() { ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); timePeroidsWithBreaksExcluded.add(getWorkingHours()); @@ -98,7 +122,10 @@ private void updateTimePeroids(List timePeroidsWithBreaksExcluded, T } } } - public TimePeroid getWorkingHours() { +*/ + + + public TimePeriod getWorkingHours() { return workingHours; } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java new file mode 100644 index 0000000..2309020 --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java @@ -0,0 +1,36 @@ +package com.example.slabiak.appointmentscheduler.model; + +import java.time.LocalDateTime; + +public class TimePeriod implements Comparable{ + public void setStart(LocalDateTime start) { + this.start = start; + } + + public void setEnd(LocalDateTime end) { + this.end = end; + } + + private LocalDateTime start; + private LocalDateTime end; + + // Constructors, getters, setters, etc. + + public void adjust(TimePeriodAdjuster adjuster, TimePeriod breakPeriod) { + TimePeriod adjusted = adjuster.adjust(this, breakPeriod); + this.start = adjusted.getStart(); + this.end = adjusted.getEnd(); + } + + public LocalDateTime getStart() { + return start; + } + + public LocalDateTime getEnd() { + return end; + } + @Override + public int compareTo(TimePeriod other) { + return this.start.compareTo(other.start); + } +} diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java new file mode 100644 index 0000000..47eaab4 --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java @@ -0,0 +1,5 @@ +package com.example.slabiak.appointmentscheduler.model; + +public interface TimePeriodAdjuster { + TimePeriod adjust(TimePeriod original, TimePeriod breakPeriod); +} diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/RetailCustomerService.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/RetailCustomerService.java new file mode 100644 index 0000000..02fedd1 --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/RetailCustomerService.java @@ -0,0 +1,23 @@ +package com.example.slabiak.appointmentscheduler.service; + +import com.example.slabiak.appointmentscheduler.entity.user.Role; +import com.example.slabiak.appointmentscheduler.entity.user.customer.RetailCustomer; +import com.example.slabiak.appointmentscheduler.model.UserForm; +import org.springframework.security.access.prepost.PreAuthorize; + +import java.util.Collection; +import java.util.List; + +public interface RetailCustomerService { + public void saveNewRetailCustomer(UserForm userForm); + + public void updateRetailCustomerProfile(UserForm updateData); + + public List getAllRetailCustomers(); + + public RetailCustomer getRetailCustomerById(int retailCustomerId); + + public Collection getRolesForRetailCustomer(); + + +} diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/UserService.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/UserService.java index 471966c..e97f455 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/UserService.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/UserService.java @@ -49,6 +49,8 @@ public interface UserService { void updateProviderProfile(UserForm updateData); + Collection getRoleForCorporateCustomers(); + Collection getRolesForProvider(); /* @@ -67,7 +69,6 @@ public interface UserService { void updateRetailCustomerProfile(UserForm updateData); - Collection getRolesForRetailCustomer(); /* * CorporateCustomer @@ -80,7 +81,6 @@ public interface UserService { void updateCorporateCustomerProfile(UserForm updateData); - Collection getRoleForCorporateCustomers(); } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/RetailCustomerService.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/RetailCustomerService.java new file mode 100644 index 0000000..7b64440 --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/RetailCustomerService.java @@ -0,0 +1,69 @@ +// RetailCustomerService.java + +package com.example.slabiak.appointmentscheduler.service.impl; + +import com.example.slabiak.appointmentscheduler.dao.RoleRepository; +import com.example.slabiak.appointmentscheduler.dao.user.customer.RetailCustomerRepository; +import com.example.slabiak.appointmentscheduler.entity.user.Role; // Add this import +import com.example.slabiak.appointmentscheduler.entity.user.customer.RetailCustomer; +import com.example.slabiak.appointmentscheduler.model.UserForm; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; + +import java.util.Collection; +import java.util.HashSet; +import java.util.List; + +@Service +public class RetailCustomerService implements com.example.slabiak.appointmentscheduler.service.RetailCustomerService { + + private final RetailCustomerRepository retailCustomerRepository; + private final PasswordEncoder passwordEncoder; + + private RoleRepository roleRepository ; + + public RetailCustomerService(RetailCustomerRepository retailCustomerRepository,PasswordEncoder passwordEncoder,RoleRepository roleRepository) { + this.retailCustomerRepository = retailCustomerRepository; + this.passwordEncoder = passwordEncoder; + this.roleRepository = roleRepository; + + } + + @Override + @PreAuthorize("#updateData.id == principal.id or hasRole('ADMIN')") + public void updateRetailCustomerProfile(UserForm updateData) { + RetailCustomer retailCustomer = retailCustomerRepository.getOne(updateData.getId()); + retailCustomer.update(updateData); + retailCustomerRepository.save(retailCustomer); + } + + @Override + public void saveNewRetailCustomer(UserForm userForm) { + RetailCustomer retailCustomer = new RetailCustomer(userForm, passwordEncoder.encode(userForm.getPassword()), getRolesForRetailCustomer()); + retailCustomerRepository.save(retailCustomer); + } + + @Override + public List getAllRetailCustomers() { + return retailCustomerRepository.findAll(); + } + + + @Override + @PreAuthorize("#retailCustomerId == principal.id or hasRole('ADMIN')") + public RetailCustomer getRetailCustomerById(int retailCustomerId) { + return retailCustomerRepository.findById(retailCustomerId) + .orElseThrow(() -> new UsernameNotFoundException("User not found!")); + } + + + public Collection getRolesForRetailCustomer() { + HashSet roles = new HashSet(); + roles.add(roleRepository.findByName("ROLE_CUSTOMER_RETAIL")); + roles.add(roleRepository.findByName("ROLE_CUSTOMER")); + return roles; + } + +} diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/UserServiceImpl.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/UserServiceImpl.java index f7e40fb..424a812 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/UserServiceImpl.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/UserServiceImpl.java @@ -16,6 +16,7 @@ import com.example.slabiak.appointmentscheduler.entity.user.provider.Provider; import com.example.slabiak.appointmentscheduler.model.ChangePasswordForm; import com.example.slabiak.appointmentscheduler.model.UserForm; +import com.example.slabiak.appointmentscheduler.service.RetailCustomerService; import com.example.slabiak.appointmentscheduler.service.UserService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.userdetails.UsernameNotFoundException; @@ -36,8 +37,9 @@ public class UserServiceImpl implements UserService { private final UserRepository userRepository; private final RoleRepository roleRepository; private final PasswordEncoder passwordEncoder; + private final RetailCustomerService retailCustomerService; - public UserServiceImpl(ProviderRepository providerRepository, CustomerRepository customerRepository, CorporateCustomerRepository corporateCustomerRepository, RetailCustomerRepository retailCustomerRepository, UserRepository userRepository, RoleRepository roleRepository, PasswordEncoder passwordEncoder) { + public UserServiceImpl(ProviderRepository providerRepository, CustomerRepository customerRepository, CorporateCustomerRepository corporateCustomerRepository, RetailCustomerRepository retailCustomerRepository, UserRepository userRepository, RoleRepository roleRepository, PasswordEncoder passwordEncoder, RetailCustomerService retailCustomerService) { this.providerRepository = providerRepository; this.customerRepository = customerRepository; this.corporateCustomerRepository = corporateCustomerRepository; @@ -45,6 +47,7 @@ public UserServiceImpl(ProviderRepository providerRepository, CustomerRepository this.userRepository = userRepository; this.roleRepository = roleRepository; this.passwordEncoder = passwordEncoder; + this.retailCustomerService = retailCustomerService; } @Override @@ -74,8 +77,7 @@ public Provider getProviderById(int providerId) { @Override @PreAuthorize("#retailCustomerId == principal.id or hasRole('ADMIN')") public RetailCustomer getRetailCustomerById(int retailCustomerId) { - return retailCustomerRepository.findById(retailCustomerId) - .orElseThrow(() -> new UsernameNotFoundException("User not found!")); + return retailCustomerService.getRetailCustomerById(retailCustomerId); } @@ -99,7 +101,7 @@ public List getAllCustomers() { @Override public List getAllRetailCustomers() { - return retailCustomerRepository.findAll(); + return retailCustomerService.getAllRetailCustomers(); } @@ -159,12 +161,9 @@ public void updateProviderProfile(UserForm updateData) { @Override @PreAuthorize("#updateData.id == principal.id or hasRole('ADMIN')") public void updateRetailCustomerProfile(UserForm updateData) { - RetailCustomer retailCustomer = retailCustomerRepository.getOne(updateData.getId()); - retailCustomer.update(updateData); - retailCustomerRepository.save(retailCustomer); + retailCustomerService.updateRetailCustomerProfile(updateData); } - @Override @PreAuthorize("#updateData.id == principal.id or hasRole('ADMIN')") public void updateCorporateCustomerProfile(UserForm updateData) { @@ -176,8 +175,7 @@ public void updateCorporateCustomerProfile(UserForm updateData) { @Override public void saveNewRetailCustomer(UserForm userForm) { - RetailCustomer retailCustomer = new RetailCustomer(userForm, passwordEncoder.encode(userForm.getPassword()), getRolesForRetailCustomer()); - retailCustomerRepository.save(retailCustomer); + retailCustomerService.saveNewRetailCustomer(userForm); } @Override @@ -193,13 +191,13 @@ public void saveNewProvider(UserForm userForm) { providerRepository.save(provider); } - @Override - public Collection getRolesForRetailCustomer() { - HashSet roles = new HashSet(); - roles.add(roleRepository.findByName("ROLE_CUSTOMER_RETAIL")); - roles.add(roleRepository.findByName("ROLE_CUSTOMER")); - return roles; - } + //for Move method +// public Collection getRolesForRetailCustomer() { +// HashSet roles = new HashSet(); +// roles.add(roleRepository.findByName("ROLE_CUSTOMER_RETAIL")); +// roles.add(roleRepository.findByName("ROLE_CUSTOMER")); +// return roles; +// } @Override From e1dec4fdcbc3d36c9b3be135038a5ef984e895fc Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 23:25:40 -0400 Subject: [PATCH 6/8] for conditional polymorphism and move method --- .../appointmentscheduler/model/AdjusterAfterEnd.java | 2 +- .../model/AdjusterBeforeStart.java | 2 +- .../slabiak/appointmentscheduler/model/DayPlan.java | 10 +++++++--- .../slabiak/appointmentscheduler/model/TimePeriod.java | 2 +- .../appointmentscheduler/model/TimePeriodAdjuster.java | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java index 5f55dfa..4fbcd21 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java @@ -2,7 +2,7 @@ class AdjusterAfterEnd implements TimePeriodAdjuster { @Override - public TimePeriod adjust(TimePeriod original, TimePeriod breakPeriod) { + public TimePeriod adjust(TimePeriod original, TimePeroid breakPeriod) { if (breakPeriod.getEnd().isAfter(original.getEnd())) { original.setEnd(breakPeriod.getEnd()); } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java index aa424af..a19618b 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java @@ -3,7 +3,7 @@ class AdjusterBeforeStart implements TimePeriodAdjuster { @Override - public TimePeriod adjust(TimePeriod original, TimePeriod breakPeriod) { + public TimePeriod adjust(TimePeriod original, TimePeroid breakPeriod) { if (breakPeriod.getStart().isBefore(original.getStart())) { original.setStart(breakPeriod.getStart()); } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java index 3517062..d4b50e6 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -41,6 +41,10 @@ public List timePeriodsWithBreaksExcluded() { return timePeriodsWithBreaksExcluded; } + private TimePeriod getWorkingHours() { + return null; + } + /* public List timePeroidsWithBreaksExcluded() { ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); timePeroidsWithBreaksExcluded.add(getWorkingHours()); @@ -125,9 +129,9 @@ private void updateTimePeroids(List timePeroidsWithBreaksExcluded, T */ - public TimePeriod getWorkingHours() { - return workingHours; - } + /*public TimePeriod getWorkingHours() { + //return workingHours; + }*/ public void setWorkingHours(TimePeroid workingHours) { this.workingHours = workingHours; diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java index 2309020..2cf6c01 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java @@ -17,7 +17,7 @@ public void setEnd(LocalDateTime end) { // Constructors, getters, setters, etc. public void adjust(TimePeriodAdjuster adjuster, TimePeriod breakPeriod) { - TimePeriod adjusted = adjuster.adjust(this, breakPeriod); + TimePeriod adjusted = adjuster.adjust(this, null); this.start = adjusted.getStart(); this.end = adjusted.getEnd(); } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java index 47eaab4..af5c256 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java @@ -1,5 +1,5 @@ package com.example.slabiak.appointmentscheduler.model; public interface TimePeriodAdjuster { - TimePeriod adjust(TimePeriod original, TimePeriod breakPeriod); + TimePeriod adjust(TimePeriod original, TimePeroid breakPeriod); } From d4b3170004764aa13c0d4136e2fe56a3706839b3 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 23:33:06 -0400 Subject: [PATCH 7/8] for conditional polymorphism and move method --- .../slabiak/appointmentscheduler/model/TimePeriod.java | 5 +++-- .../slabiak/appointmentscheduler/model/TimePeroid.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java index 2cf6c01..d90c7fd 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java @@ -1,6 +1,7 @@ package com.example.slabiak.appointmentscheduler.model; import java.time.LocalDateTime; +import java.time.chrono.ChronoLocalDateTime; public class TimePeriod implements Comparable{ public void setStart(LocalDateTime start) { @@ -18,11 +19,11 @@ public void setEnd(LocalDateTime end) { public void adjust(TimePeriodAdjuster adjuster, TimePeriod breakPeriod) { TimePeriod adjusted = adjuster.adjust(this, null); - this.start = adjusted.getStart(); + this.start = (LocalDateTime) adjusted.getStart(); this.end = adjusted.getEnd(); } - public LocalDateTime getStart() { + public ChronoLocalDateTime getStart() { return start; } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeroid.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeroid.java index 2c7b4c5..6a0234a 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeroid.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeroid.java @@ -1,5 +1,6 @@ package com.example.slabiak.appointmentscheduler.model; +import java.time.LocalDateTime; import java.time.LocalTime; import java.util.Objects; @@ -17,8 +18,8 @@ public TimePeroid(LocalTime start, LocalTime end) { this.end = end; } - public LocalTime getStart() { - return start; + public LocalDateTime getStart() { + return null; } public void setStart(LocalTime start) { From 709dacca4c2d89ae8a60a6577e247cd37c4b67e0 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Tue, 28 Nov 2023 04:19:03 -0400 Subject: [PATCH 8/8] for conditional polymorphism and move method --- .../model/AdjusterAfterEnd.java | 4 +- .../model/AdjusterBeforeStart.java | 9 ++- .../appointmentscheduler/model/DayPlan.java | 61 +++++++++---------- .../model/TimePeriod.java | 37 ----------- .../model/TimePeriodAdjuster.java | 2 +- .../model/TimePeroid.java | 5 +- .../service/impl/AppointmentServiceImpl.java | 17 +++--- 7 files changed, 51 insertions(+), 84 deletions(-) delete mode 100644 src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java index 4fbcd21..98746ba 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java @@ -1,8 +1,10 @@ package com.example.slabiak.appointmentscheduler.model; class AdjusterAfterEnd implements TimePeriodAdjuster { + + @Override - public TimePeriod adjust(TimePeriod original, TimePeroid breakPeriod) { + public TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod) { if (breakPeriod.getEnd().isAfter(original.getEnd())) { original.setEnd(breakPeriod.getEnd()); } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java index a19618b..af73bd4 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java @@ -1,13 +1,18 @@ package com.example.slabiak.appointmentscheduler.model; +import java.time.LocalDateTime; + class AdjusterBeforeStart implements TimePeriodAdjuster { + + @Override - public TimePeriod adjust(TimePeriod original, TimePeroid breakPeriod) { + public TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod) { if (breakPeriod.getStart().isBefore(original.getStart())) { - original.setStart(breakPeriod.getStart()); + original.setStart((breakPeriod.getStart())); } return original; } } + diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java index d4b50e6..3ce8d73 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -14,36 +14,6 @@ public DayPlan() { breaks = new ArrayList(); } -//for conditionl polymorphism - public List timePeriodsWithBreaksExcluded() { - List timePeriodsWithBreaksExcluded = new ArrayList<>(); - timePeriodsWithBreaksExcluded.add(getWorkingHours()); - List breaks = getBreaks(); - - if (!breaks.isEmpty()) { - List adjusters = Arrays.asList( - new AdjusterBeforeStart(), - new AdjusterAfterEnd() - // Add more adjusters as needed - ); - - for (TimePeroid breakPeriod : breaks) { - for (TimePeriod timePeriod : timePeriodsWithBreaksExcluded) { - for (TimePeriodAdjuster adjuster : adjusters) { - adjuster.adjust(timePeriod, breakPeriod); - } - } - } - - Collections.sort(timePeriodsWithBreaksExcluded); - } - - return timePeriodsWithBreaksExcluded; - } - - private TimePeriod getWorkingHours() { - return null; - } /* public List timePeroidsWithBreaksExcluded() { ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); @@ -129,9 +99,9 @@ private void updateTimePeroids(List timePeroidsWithBreaksExcluded, T */ - /*public TimePeriod getWorkingHours() { - //return workingHours; - }*/ + public TimePeroid getWorkingHours() { + return workingHours; + } public void setWorkingHours(TimePeroid workingHours) { this.workingHours = workingHours; @@ -153,4 +123,29 @@ public void addBreak(TimePeroid breakToAdd) { breaks.add(breakToAdd); } + public List timePeroidsWithBreaksExcluded() { + List timePeriodsWithBreaksExcluded = new ArrayList<>(); + timePeriodsWithBreaksExcluded.add(getWorkingHours()); + List breaks = getBreaks(); + + if (!breaks.isEmpty()) { + List adjusters = Arrays.asList( + new AdjusterBeforeStart(), + new AdjusterAfterEnd() + // Add more adjusters as needed + ); + + for (TimePeroid breakPeriod : breaks) { + for (TimePeroid timePeriod : timePeriodsWithBreaksExcluded) { + for (TimePeriodAdjuster adjuster : adjusters) { + adjuster.adjust(timePeriod, breakPeriod); + } + } + } + + Collections.sort(timePeriodsWithBreaksExcluded); + } + + return timePeriodsWithBreaksExcluded; + } } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java deleted file mode 100644 index d90c7fd..0000000 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriod.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.example.slabiak.appointmentscheduler.model; - -import java.time.LocalDateTime; -import java.time.chrono.ChronoLocalDateTime; - -public class TimePeriod implements Comparable{ - public void setStart(LocalDateTime start) { - this.start = start; - } - - public void setEnd(LocalDateTime end) { - this.end = end; - } - - private LocalDateTime start; - private LocalDateTime end; - - // Constructors, getters, setters, etc. - - public void adjust(TimePeriodAdjuster adjuster, TimePeriod breakPeriod) { - TimePeriod adjusted = adjuster.adjust(this, null); - this.start = (LocalDateTime) adjusted.getStart(); - this.end = adjusted.getEnd(); - } - - public ChronoLocalDateTime getStart() { - return start; - } - - public LocalDateTime getEnd() { - return end; - } - @Override - public int compareTo(TimePeriod other) { - return this.start.compareTo(other.start); - } -} diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java index af5c256..120e059 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java @@ -1,5 +1,5 @@ package com.example.slabiak.appointmentscheduler.model; public interface TimePeriodAdjuster { - TimePeriod adjust(TimePeriod original, TimePeroid breakPeriod); + TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod); } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeroid.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeroid.java index 6a0234a..2c7b4c5 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeroid.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeroid.java @@ -1,6 +1,5 @@ package com.example.slabiak.appointmentscheduler.model; -import java.time.LocalDateTime; import java.time.LocalTime; import java.util.Objects; @@ -18,8 +17,8 @@ public TimePeroid(LocalTime start, LocalTime end) { this.end = end; } - public LocalDateTime getStart() { - return null; + public LocalTime getStart() { + return start; } public void setStart(LocalTime start) { diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java index 2b0fd90..d54fcf7 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java @@ -87,17 +87,18 @@ public List getAppointmentByProviderId(int providerId) { public List getAppointmentsByProviderAtDay(int providerId, LocalDate day) { return appointmentRepository.findByProviderIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); } -//Before renaming a method -// @Override -// public List getAppointmentsByCustomerAtDay(int providerId, LocalDate day) { -// return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); -// } - //After renaming a method + + public List getAppointmentsByCustomerAtDayAndTimePeriods(int providerId, LocalDate day) { return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); } + + public List getAppointmentsByCustomerAtDay(int providerId, LocalDate day) { + return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); + } + @Override public List getAvailableHours(int providerId, int customerId, int workId, LocalDate date) { Provider p = userService.getProviderById(providerId); @@ -105,7 +106,7 @@ public List getAvailableHours(int providerId, int customerId, int wo DayPlan selectedDay = workingPlan.getDay(date.getDayOfWeek().toString().toLowerCase()); List providerAppointments = getAppointmentsByProviderAtDay(providerId, date); - List customerAppointments = getAppointmentsByCustomerAtDayAndTimePeriods(customerId, date); + List customerAppointments = getAppointmentsByCustomerAtDay(customerId, date); List availablePeroids = selectedDay.timePeroidsWithBreaksExcluded(); availablePeroids = excludeAppointmentsFromTimePeroids(availablePeroids, providerAppointments); @@ -132,6 +133,8 @@ public void createNewAppointment(int workId, int providerId, int customerId, Loc } } + // Before + @Override public void addMessageToAppointmentChat(int appointmentId, int authorId, ChatMessage chatMessage) {