From eb43c781d03886a8be5d4914f973391b18796953 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 02:54:15 -0300 Subject: [PATCH 01/32] initial commit --- .../example/slabiak/appointmentscheduler/model/DayPlan.java | 2 ++ .../appointmentscheduler/service/impl/EmailServiceImpl.java | 2 +- src/main/resources/application.properties | 4 ++-- 3 files changed, 5 insertions(+), 3 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..c508f67 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -3,7 +3,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +@JsonIgnoreProperties(ignoreUnknown = true) public class DayPlan { private TimePeroid workingHours; 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..0be5fa3 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 @@ -51,7 +51,7 @@ public void sendEmail(String to, String subject, String templateName, Context te String html = templateEngine.process("email/" + templateName, templateContext); helper.setTo(to); - helper.setFrom("appointmentscheduler@gmail.com"); + helper.setFrom("health.sync19@gmail.com"); helper.setSubject(subject); helper.setText(html, true); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6c61b62..c2a8264 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,8 +3,8 @@ # spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.datasource.url=jdbc:mysql://localhost:3306/appointmentscheduler?useSSL=false&serverTimezone=UTC -spring.datasource.username=user -spring.datasource.password=password +spring.datasource.username=root +spring.datasource.password=Merin@123 spring.datasource.hikari.maximum-pool-size=7 # # mail properties From 40de5ea6d41719bb82a97b8e32d8de6def309ab2 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Sat, 4 Nov 2023 19:28:11 -0300 Subject: [PATCH 02/32] 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 ea26af934a3d26df17f872b0746ab89ab4a33ffe Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 21:57:29 -0400 Subject: [PATCH 03/32] 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 c508f67..3687151 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -15,41 +15,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 86a58ecf36eecdd03bd92c0b3c91b0bf1f52d0cf Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 22:18:50 -0400 Subject: [PATCH 04/32] 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 0be5fa3..a258c34 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 7f5730e6664e1ba811addc4cf4dcb9e6c3d2b959 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 22:19:29 -0400 Subject: [PATCH 05/32] 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 12a9f88b33c25573f7d85819f8152cf7a7a646c2 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 23:18:11 -0400 Subject: [PATCH 06/32] 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 3687151..a615edf 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; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -15,46 +16,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()); @@ -100,7 +124,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 3c9be7e5a7707d62a5dbce8311348c3b5225c7eb Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 23:25:40 -0400 Subject: [PATCH 07/32] 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 a615edf..eae6a39 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -43,6 +43,10 @@ public List timePeriodsWithBreaksExcluded() { return timePeriodsWithBreaksExcluded; } + private TimePeriod getWorkingHours() { + return null; + } + /* public List timePeroidsWithBreaksExcluded() { ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); timePeroidsWithBreaksExcluded.add(getWorkingHours()); @@ -127,9 +131,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 2d59df8281db5016e2ef9a8fa4bb868a38ff4de9 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Mon, 27 Nov 2023 23:33:06 -0400 Subject: [PATCH 08/32] 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 b9b329070523c5187e8e1f37fde1558671fc725f Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Tue, 28 Nov 2023 04:19:03 -0400 Subject: [PATCH 09/32] 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 eae6a39..e7f3a2b 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -16,36 +16,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<>(); @@ -131,9 +101,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; @@ -155,4 +125,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) { From 3cc5b8182ff46ac93a63f0f67db75e06cda57591 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 18:57:58 -0300 Subject: [PATCH 10/32] for github actions --- .github/workflows/deploy.yml | 47 ++++++++++++++++++++++++++++++++++++ README.md | 2 +- appspec.yml | 0 scripts/deploy.sh | 6 +++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 appspec.yml create mode 100644 scripts/deploy.sh diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..52e5fff --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,47 @@ +name: Deploy Spring Boot Application + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Set Up JDK + uses: actions/setup-java@v3 + with: + java-version: '17' + + - name: Build with Maven (skip tests) + run: mvn clean package -DskipTests + + - name: Upload Artifact + uses: actions/upload-artifact@v3 + with: + name: spring-boot-application + path: target/*.jar + + - name: Deploy to S3 + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_REGION: 'us-east-1' + run: | + aws s3 cp target/*.jar s3://your-bucket/spring-boot-application.jar + + - name: Deploy to EC2 + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_REGION: 'us-east-1' + run: | + aws deploy create-deployment \ + --application-name your-codedeploy-app \ + --deployment-group-name your-deployment-group \ + --s3-location bucket=your-bucket,key=spring-boot-application.jar,bundleType=jar diff --git a/README.md b/README.md index 8ba93ed..a5946af 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ You can use the following credentials with live demo: | `corporate customer` | customer_c |qwerty123 | | `retail customer` | customer_r |qwerty123 | -## Blog +## Blog,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 This application is being described in [devoxify.com](https://devoxify.com/) blog. If you are interested in how this project was created, what issues were encoutered and how they were solved I highly encourage you to visit this blog. diff --git a/appspec.yml b/appspec.yml new file mode 100644 index 0000000..e69de29 diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..91effdb --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# Stop the existing application if it's running +pkill -f 'java -jar /home/ec2-user/spring-boot-application.jar' + +# Start the new application +nohup java -jar /home/ec2-user/spring-boot-application.jar > /home/ec2-user/app.log 2>&1 & From 06cbac7f865b383b47186e3b4cb0b638ef569c27 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 19:00:13 -0300 Subject: [PATCH 11/32] for github actions --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a5946af..8ba93ed 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ You can use the following credentials with live demo: | `corporate customer` | customer_c |qwerty123 | | `retail customer` | customer_r |qwerty123 | -## Blog,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 +## Blog This application is being described in [devoxify.com](https://devoxify.com/) blog. If you are interested in how this project was created, what issues were encoutered and how they were solved I highly encourage you to visit this blog. From 5cb6d2e7b67fedd88f86d3bf99943b42710905e8 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 19:37:50 -0300 Subject: [PATCH 12/32] for github actions --- .github/workflows/deploy.yml | 10 ++++++---- appspec.yml | 10 ++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 52e5fff..9bcbd37 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -31,17 +31,19 @@ jobs: env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }} AWS_REGION: 'us-east-1' run: | - aws s3 cp target/*.jar s3://your-bucket/spring-boot-application.jar + aws s3 cp target/*.jar s3://s3bucketmerin12/spring-boot-application.jar - name: Deploy to EC2 env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }} AWS_REGION: 'us-east-1' run: | aws deploy create-deployment \ - --application-name your-codedeploy-app \ - --deployment-group-name your-deployment-group \ - --s3-location bucket=your-bucket,key=spring-boot-application.jar,bundleType=jar + --application-name aws-demo-ec2-code-deploy \ + --deployment-group-name aws-ec2-code-deploy-group \ + --s3-location bucket=s3bucketmerin12,key=spring-boot-application.jar,bundleType=jar diff --git a/appspec.yml b/appspec.yml index e69de29..e2928d8 100644 --- a/appspec.yml +++ b/appspec.yml @@ -0,0 +1,10 @@ +version: 0.0 +os: linux +files: + - source: /spring-boot-application.jar + destination: /home/ec2-user/spring-boot-application.jar +hooks: + AfterInstall: + - location: scripts/deploy.sh + timeout: 300 + runas: ec2-user From b7c9ed9c46fe67e319c84403fc4e20d0b48f4161 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 19:41:15 -0300 Subject: [PATCH 13/32] for github actions --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 9bcbd37..637b718 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -3,7 +3,7 @@ name: Deploy Spring Boot Application on: push: branches: - - main + - develop jobs: deploy: From c8488c6c7c5e9e86425e7dc09efd33550550dbec Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 19:43:49 -0300 Subject: [PATCH 14/32] for github actions --- .github/workflows/deploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 637b718..5fc257c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -17,6 +17,7 @@ jobs: uses: actions/setup-java@v3 with: java-version: '17' + distribution: 'temurin' # You can change to 'zulu', 'adopt', etc. if preferred - name: Build with Maven (skip tests) run: mvn clean package -DskipTests From 9cc8275f9f907c0840d74aba2bf74a54ee5c4f41 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 20:01:57 -0300 Subject: [PATCH 15/32] for github actions --- pom.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b64f5ee..7aee71d 100644 --- a/pom.xml +++ b/pom.xml @@ -110,7 +110,7 @@ org.projectlombok lombok - 1.18.8 + 1.18.26 provided @@ -228,7 +228,6 @@ - From f72e301eea4c5e6b206113b33666fd644add96e7 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 20:08:23 -0300 Subject: [PATCH 16/32] for github actions --- .github/workflows/deploy.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 5fc257c..c23a52c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -22,11 +22,15 @@ jobs: - name: Build with Maven (skip tests) run: mvn clean package -DskipTests + - name: Create ZIP Archive + run: | + zip spring-boot-application.zip target/*.jar + - name: Upload Artifact uses: actions/upload-artifact@v3 with: name: spring-boot-application - path: target/*.jar + path: spring-boot-application.zip - name: Deploy to S3 env: @@ -35,7 +39,7 @@ jobs: AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }} AWS_REGION: 'us-east-1' run: | - aws s3 cp target/*.jar s3://s3bucketmerin12/spring-boot-application.jar + aws s3 cp spring-boot-application.zip s3://s3bucketmerin12/spring-boot-application.zip - name: Deploy to EC2 env: @@ -47,4 +51,4 @@ jobs: aws deploy create-deployment \ --application-name aws-demo-ec2-code-deploy \ --deployment-group-name aws-ec2-code-deploy-group \ - --s3-location bucket=s3bucketmerin12,key=spring-boot-application.jar,bundleType=jar + --s3-location bucket=s3bucketmerin12,key=spring-boot-application.zip,bundleType=zip From 6738e20d7bc5dc159d429af56683cdbfc158bf04 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 20:22:04 -0300 Subject: [PATCH 17/32] for github actions --- .github/workflows/deploy.yml | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index c23a52c..9bbf426 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -17,20 +17,24 @@ jobs: uses: actions/setup-java@v3 with: java-version: '17' - distribution: 'temurin' # You can change to 'zulu', 'adopt', etc. if preferred + distribution: 'temurin' - name: Build with Maven (skip tests) run: mvn clean package -DskipTests - - name: Create ZIP Archive + - name: Zip Deployment Artifacts run: | - zip spring-boot-application.zip target/*.jar + mkdir deployment + cp target/*.jar deployment/ + cp appspec.yml deployment/ + cd deployment + zip -r ../deployment-package.zip . - - name: Upload Artifact + - name: Upload Deployment Package uses: actions/upload-artifact@v3 with: - name: spring-boot-application - path: spring-boot-application.zip + name: deployment-package + path: deployment-package.zip - name: Deploy to S3 env: @@ -39,7 +43,7 @@ jobs: AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }} AWS_REGION: 'us-east-1' run: | - aws s3 cp spring-boot-application.zip s3://s3bucketmerin12/spring-boot-application.zip + aws s3 cp deployment-package.zip s3://s3bucketmerin12/deployment-package.zip - name: Deploy to EC2 env: @@ -51,4 +55,4 @@ jobs: aws deploy create-deployment \ --application-name aws-demo-ec2-code-deploy \ --deployment-group-name aws-ec2-code-deploy-group \ - --s3-location bucket=s3bucketmerin12,key=spring-boot-application.zip,bundleType=zip + --s3-location bucket=s3bucketmerin12,key=deployment-package.zip,bundleType=zip From 2d6887057a832e70e55df7b5153f5b1e91d1f52c Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 20:33:49 -0300 Subject: [PATCH 18/32] for github actions --- .github/workflows/deploy.yml | 4 ++-- appspec.yml | 5 +++-- scripts/deploy.sh | 11 ++++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 9bbf426..94faa12 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -22,10 +22,10 @@ jobs: - name: Build with Maven (skip tests) run: mvn clean package -DskipTests - - name: Zip Deployment Artifacts + - name: Prepare Deployment Package run: | mkdir deployment - cp target/*.jar deployment/ + cp target/appointmentscheduler-1.0.5.jar deployment/ cp appspec.yml deployment/ cd deployment zip -r ../deployment-package.zip . diff --git a/appspec.yml b/appspec.yml index e2928d8..de3b9a7 100644 --- a/appspec.yml +++ b/appspec.yml @@ -1,8 +1,9 @@ version: 0.0 os: linux files: - - source: /spring-boot-application.jar - destination: /home/ec2-user/spring-boot-application.jar + - source: / + destination: /home/ec2-user/app + hooks: AfterInstall: - location: scripts/deploy.sh diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 91effdb..e5abd47 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,6 +1,11 @@ #!/bin/bash -# Stop the existing application if it's running -pkill -f 'java -jar /home/ec2-user/spring-boot-application.jar' + +# Stop the currently running application, if any +if pgrep -f "appointmentscheduler-1.0.5.jar" > /dev/null; then + echo "Stopping existing application..." + pkill -f "appointmentscheduler-1.0.5.jar" +fi # Start the new application -nohup java -jar /home/ec2-user/spring-boot-application.jar > /home/ec2-user/app.log 2>&1 & +echo "Starting new application..." +nohup java -jar /home/ec2-user/app/appointmentscheduler-1.0.5.jar > /home/ec2-user/app/application.log 2>&1 & From c6a06dc0070fdb2f7941653f3690f83975bf9273 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 20:39:55 -0300 Subject: [PATCH 19/32] for github actions --- .github/workflows/deploy.yml | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 94faa12..315487d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -22,19 +22,20 @@ jobs: - name: Build with Maven (skip tests) run: mvn clean package -DskipTests - - name: Prepare Deployment Package + - name: Package as ZIP run: | - mkdir deployment - cp target/appointmentscheduler-1.0.5.jar deployment/ - cp appspec.yml deployment/ - cd deployment - zip -r ../deployment-package.zip . - - - name: Upload Deployment Package + mkdir -p deploy/scripts + cp target/*.jar deploy/ + cp appspec.yml deploy/ + cp scripts/deploy.sh deploy/scripts/ + cd deploy + zip -r deployment.zip ./* + + - name: Upload Artifact uses: actions/upload-artifact@v3 with: - name: deployment-package - path: deployment-package.zip + name: deployment + path: deploy/deployment.zip - name: Deploy to S3 env: @@ -43,7 +44,7 @@ jobs: AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }} AWS_REGION: 'us-east-1' run: | - aws s3 cp deployment-package.zip s3://s3bucketmerin12/deployment-package.zip + aws s3 cp deploy/deployment.zip s3://s3bucketmerin12/deployment.zip - name: Deploy to EC2 env: @@ -55,4 +56,4 @@ jobs: aws deploy create-deployment \ --application-name aws-demo-ec2-code-deploy \ --deployment-group-name aws-ec2-code-deploy-group \ - --s3-location bucket=s3bucketmerin12,key=deployment-package.zip,bundleType=zip + --s3-location bucket=s3bucketmerin12,key=deployment.zip,bundleType=zip From ffd2c1e163156f20c7707086aecafa66c77c8d97 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 21:02:45 -0300 Subject: [PATCH 20/32] for github actions --- appspec.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/appspec.yml b/appspec.yml index de3b9a7..7f04032 100644 --- a/appspec.yml +++ b/appspec.yml @@ -1,5 +1,6 @@ version: 0.0 os: linux + files: - source: / destination: /home/ec2-user/app @@ -8,4 +9,4 @@ hooks: AfterInstall: - location: scripts/deploy.sh timeout: 300 - runas: ec2-user + runas: root From a053fefdfbefef44e7c83da32e6b8e1c03e9e903 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Wed, 24 Jul 2024 21:54:58 -0300 Subject: [PATCH 21/32] for github actions --- src/main/resources/application.properties | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c2a8264..97c5bfc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,9 +2,9 @@ # JDBC properties # spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect -spring.datasource.url=jdbc:mysql://localhost:3306/appointmentscheduler?useSSL=false&serverTimezone=UTC -spring.datasource.username=root -spring.datasource.password=Merin@123 +spring.datasource.url=jdbc:mysql://database.cuxdfh83jv2m.us-east-1.rds.amazonaws.com:3306/appointmentscheduler?useSSL=false&serverTimezone=UTC +spring.datasource.username=admin +spring.datasource.password=merin123 spring.datasource.hikari.maximum-pool-size=7 # # mail properties From 7899c2827feee9939dc768130043aba6dddf319a Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Fri, 2 Aug 2024 05:26:55 -0300 Subject: [PATCH 22/32] for codedeploy --- pom.xml | 2 ++ src/main/resources/application.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7aee71d..8a24e0b 100644 --- a/pom.xml +++ b/pom.xml @@ -140,6 +140,8 @@ selenium-remote-driver 3.141.59 + + org.seleniumhq.selenium selenium-chrome-driver diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 97c5bfc..2a972c3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,7 @@ # JDBC properties # spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect -spring.datasource.url=jdbc:mysql://database.cuxdfh83jv2m.us-east-1.rds.amazonaws.com:3306/appointmentscheduler?useSSL=false&serverTimezone=UTC +spring.datasource.url=appointmentschedulerrds-instance-1.cuxdfh83jv2m.us-east-1.rds.amazonaws.com/appointmentscheduler?useSSL=false&serverTimezone=UTC spring.datasource.username=admin spring.datasource.password=merin123 spring.datasource.hikari.maximum-pool-size=7 From f5034b29f1553ef984f2742603b33763a586d11b Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Fri, 2 Aug 2024 05:43:46 -0300 Subject: [PATCH 23/32] for codedeploy --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 315487d..dc78e47 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -55,5 +55,5 @@ jobs: run: | aws deploy create-deployment \ --application-name aws-demo-ec2-code-deploy \ - --deployment-group-name aws-ec2-code-deploy-group \ + --deployment-group-name AppointmentScheduler \ --s3-location bucket=s3bucketmerin12,key=deployment.zip,bundleType=zip From f563aad0e8a86279d8486e9f9fb0298c35ba68bb Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Fri, 2 Aug 2024 06:14:47 -0300 Subject: [PATCH 24/32] for codedeploy --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2a972c3..8a75222 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,7 @@ # JDBC properties # spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect -spring.datasource.url=appointmentschedulerrds-instance-1.cuxdfh83jv2m.us-east-1.rds.amazonaws.com/appointmentscheduler?useSSL=false&serverTimezone=UTC +spring.datasource.url=jdbc:mysql://appointmentschedulerrds-instance-1.cuxdfh83jv2m.us-east-1.rds.amazonaws.com:3306/appointmentscheduler?useSSL=false&serverTimezone=UTC spring.datasource.username=admin spring.datasource.password=merin123 spring.datasource.hikari.maximum-pool-size=7 From 1d34203d14d840d30093b12d11438002a395a4bc Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Fri, 2 Aug 2024 06:45:29 -0300 Subject: [PATCH 25/32] for codedeploy --- src/main/resources/application.properties | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8a75222..ada73ab 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -21,4 +21,6 @@ spring.mail.properties.mail.smtp.starttls.enable=true # app.jwtSecret=secret -base.url=http://localhost:8080 \ No newline at end of file +#base.url=http://localhost:8080 + +server.port=80 From 299ffcd23e49c145519c21235c92802cd0c45da6 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Fri, 2 Aug 2024 06:55:29 -0300 Subject: [PATCH 26/32] for codedeploy --- src/main/resources/application.properties | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ada73ab..96b27ff 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -20,7 +20,6 @@ spring.mail.properties.mail.smtp.starttls.enable=true # JWT token secret in Base64 # app.jwtSecret=secret - -#base.url=http://localhost:8080 +base.url=http://localhost:8080 server.port=80 From 366376741d933d5257e960164eecb0ce14396c43 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Fri, 2 Aug 2024 07:07:03 -0300 Subject: [PATCH 27/32] for codedeploy --- src/main/resources/application.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 96b27ff..9d4e733 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,4 +22,5 @@ spring.mail.properties.mail.smtp.starttls.enable=true app.jwtSecret=secret base.url=http://localhost:8080 -server.port=80 +server.port=8080 +server.address=0.0.0.0 From b831dd157568a43996b57d8018769892b0aca199 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Sat, 3 Aug 2024 02:11:40 -0300 Subject: [PATCH 28/32] for codedeploy --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index dc78e47..53777a7 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -55,5 +55,5 @@ jobs: run: | aws deploy create-deployment \ --application-name aws-demo-ec2-code-deploy \ - --deployment-group-name AppointmentScheduler \ + --deployment-group-name AppointmentSchedulerCodeDeploy \ --s3-location bucket=s3bucketmerin12,key=deployment.zip,bundleType=zip From 38a6b5d78e87c0ef2d12b2f8b3e8a107c67206d6 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Sat, 3 Aug 2024 14:37:42 -0300 Subject: [PATCH 29/32] for codedeploy --- scripts/deploy.sh | 2 +- src/main/resources/application.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index e5abd47..9be0fec 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -8,4 +8,4 @@ fi # Start the new application echo "Starting new application..." -nohup java -jar /home/ec2-user/app/appointmentscheduler-1.0.5.jar > /home/ec2-user/app/application.log 2>&1 & +nohup java -jar /home/ec2-user/app/appointmentscheduler-1.0.5.jar > /home/ec2-user/app/application.log 2>&1 & \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9d4e733..c4ae748 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,5 +22,5 @@ spring.mail.properties.mail.smtp.starttls.enable=true app.jwtSecret=secret base.url=http://localhost:8080 -server.port=8080 +server.port=80 server.address=0.0.0.0 From 31fb4d70f0172266426d85bba74fe3b30bb2a903 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Sat, 3 Aug 2024 15:18:16 -0300 Subject: [PATCH 30/32] for codedeploy --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c4ae748..9d4e733 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,5 +22,5 @@ spring.mail.properties.mail.smtp.starttls.enable=true app.jwtSecret=secret base.url=http://localhost:8080 -server.port=80 +server.port=8080 server.address=0.0.0.0 From 54b1459e726296381825b788bca6088a744e2b12 Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Sat, 3 Aug 2024 15:30:49 -0300 Subject: [PATCH 31/32] for codedeploy --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9d4e733..3840444 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,5 +22,5 @@ spring.mail.properties.mail.smtp.starttls.enable=true app.jwtSecret=secret base.url=http://localhost:8080 -server.port=8080 +#server.port=8080 server.address=0.0.0.0 From 0f7b9ceac5092c99e78f21b5bc5ec0113ba82fea Mon Sep 17 00:00:00 2001 From: Merin Mary Saju Date: Sat, 3 Aug 2024 16:42:31 -0300 Subject: [PATCH 32/32] for codedeploy --- src/main/resources/application.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3840444..e26aec7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -24,3 +24,4 @@ base.url=http://localhost:8080 #server.port=8080 server.address=0.0.0.0 +logging.level.root=DEBUG