Skip to content

Commit

Permalink
Merge pull request #3 from MERINGIT/Refactoring
Browse files Browse the repository at this point in the history
for conditional polymorphism and move method
  • Loading branch information
MERINGIT authored Nov 28, 2023
2 parents 95634bd + 709dacc commit 9018aa7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,6 @@ public DayPlan() {
breaks = new ArrayList();
}

//for conditionl polymorphism
public List<TimePeriod> timePeriodsWithBreaksExcluded() {
List<TimePeriod> timePeriodsWithBreaksExcluded = new ArrayList<>();
timePeriodsWithBreaksExcluded.add(getWorkingHours());
List<TimePeroid> breaks = getBreaks();

if (!breaks.isEmpty()) {
List<TimePeriodAdjuster> 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<TimePeroid> timePeroidsWithBreaksExcluded() {
ArrayList<TimePeroid> timePeroidsWithBreaksExcluded = new ArrayList<>();
Expand Down Expand Up @@ -129,9 +99,9 @@ private void updateTimePeroids(List<TimePeroid> timePeroidsWithBreaksExcluded, T
*/


/*public TimePeriod getWorkingHours() {
//return workingHours;
}*/
public TimePeroid getWorkingHours() {
return workingHours;
}

public void setWorkingHours(TimePeroid workingHours) {
this.workingHours = workingHours;
Expand All @@ -153,4 +123,29 @@ public void addBreak(TimePeroid breakToAdd) {
breaks.add(breakToAdd);
}

public List<TimePeroid> timePeroidsWithBreaksExcluded() {
List<TimePeroid> timePeriodsWithBreaksExcluded = new ArrayList<>();
timePeriodsWithBreaksExcluded.add(getWorkingHours());
List<TimePeroid> breaks = getBreaks();

if (!breaks.isEmpty()) {
List<TimePeriodAdjuster> 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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.slabiak.appointmentscheduler.model;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,26 @@ public List<Appointment> getAppointmentByProviderId(int providerId) {
public List<Appointment> getAppointmentsByProviderAtDay(int providerId, LocalDate day) {
return appointmentRepository.findByProviderIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1));
}
//Before renaming a method
// @Override
// public List<Appointment> getAppointmentsByCustomerAtDay(int providerId, LocalDate day) {
// return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1));
// }
//After renaming a method


public List<Appointment> getAppointmentsByCustomerAtDayAndTimePeriods(int providerId, LocalDate day) {
return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1));
}



public List<Appointment> getAppointmentsByCustomerAtDay(int providerId, LocalDate day) {
return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1));
}

@Override
public List<TimePeroid> getAvailableHours(int providerId, int customerId, int workId, LocalDate date) {
Provider p = userService.getProviderById(providerId);
WorkingPlan workingPlan = p.getWorkingPlan();
DayPlan selectedDay = workingPlan.getDay(date.getDayOfWeek().toString().toLowerCase());

List<Appointment> providerAppointments = getAppointmentsByProviderAtDay(providerId, date);
List<Appointment> customerAppointments = getAppointmentsByCustomerAtDayAndTimePeriods(customerId, date);
List<Appointment> customerAppointments = getAppointmentsByCustomerAtDay(customerId, date);

List<TimePeroid> availablePeroids = selectedDay.timePeroidsWithBreaksExcluded();
availablePeroids = excludeAppointmentsFromTimePeroids(availablePeroids, providerAppointments);
Expand All @@ -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) {
Expand Down

0 comments on commit 9018aa7

Please sign in to comment.