Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #50

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@
</executions>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>


<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.slabiak.appointmentscheduler.model;

class AdjusterAfterEnd implements TimePeriodAdjuster {


@Override
public TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod) {
if (breakPeriod.getEnd().isAfter(original.getEnd())) {
original.setEnd(breakPeriod.getEnd());
}
return original;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.slabiak.appointmentscheduler.model;


import java.time.LocalDateTime;

class AdjusterBeforeStart implements TimePeriodAdjuster {


@Override
public TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod) {
if (breakPeriod.getStart().isBefore(original.getStart())) {
original.setStart((breakPeriod.getStart()));
}
return original;
}
}


Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,7 +14,8 @@ public DayPlan() {
breaks = new ArrayList();
}

public List<TimePeroid> timePeroidsWithBreaksExcluded() {

/* public List<TimePeroid> timePeroidsWithBreaksExcluded() {
ArrayList<TimePeroid> timePeroidsWithBreaksExcluded = new ArrayList<>();
timePeroidsWithBreaksExcluded.add(getWorkingHours());
List<TimePeroid> breaks = getBreaks();
Expand Down Expand Up @@ -46,8 +48,57 @@ public List<TimePeroid> timePeroidsWithBreaksExcluded() {


return timePeroidsWithBreaksExcluded;
}*/

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

if (!breaks.isEmpty()) {
processBreaks(timePeroidsWithBreaksExcluded, breaks);
}

return timePeroidsWithBreaksExcluded;
}

private void processBreaks(List<TimePeroid> timePeroidsWithBreaksExcluded, List<TimePeroid> breaks) {
ArrayList<TimePeroid> 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<TimePeroid> timePeroidsWithBreaksExcluded, TimePeroid break1, List<TimePeroid> 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;
}
Expand All @@ -72,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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.slabiak.appointmentscheduler.model;

public interface TimePeriodAdjuster {
TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface AppointmentService {

List<Appointment> getAppointmentsByProviderAtDay(int providerId, LocalDate day);

List<Appointment> getAppointmentsByCustomerAtDay(int providerId, LocalDate day);
List<Appointment> getAppointmentsByCustomerAtDayAndTimePeriods(int providerId, LocalDate day);

List<Appointment> getConfirmedAppointmentsByCustomerId(int customerId);

Expand Down
Original file line number Diff line number Diff line change
@@ -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<RetailCustomer> getAllRetailCustomers();

public RetailCustomer getRetailCustomerById(int retailCustomerId);

public Collection<Role> getRolesForRetailCustomer();


}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public interface UserService {

void updateProviderProfile(UserForm updateData);

Collection<Role> getRoleForCorporateCustomers();

Collection<Role> getRolesForProvider();

/*
Expand All @@ -67,7 +69,6 @@ public interface UserService {

void updateRetailCustomerProfile(UserForm updateData);

Collection<Role> getRolesForRetailCustomer();

/*
* CorporateCustomer
Expand All @@ -80,7 +81,6 @@ public interface UserService {

void updateCorporateCustomerProfile(UserForm updateData);

Collection<Role> getRoleForCorporateCustomers();


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ public List<Appointment> getAppointmentsByProviderAtDay(int providerId, LocalDat
return appointmentRepository.findByProviderIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1));
}

@Override

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));
}
Expand Down Expand Up @@ -127,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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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("[email protected]");
helper.setSubject(subject);
helper.setText(html, true);

if (attachment != null) {
helper.addAttachment("invoice", attachment);
}

return message;
}



@Async
@Override
public void sendAppointmentFinishedNotification(Appointment appointment) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RetailCustomer> 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<Role> getRolesForRetailCustomer() {
HashSet<Role> roles = new HashSet();
roles.add(roleRepository.findByName("ROLE_CUSTOMER_RETAIL"));
roles.add(roleRepository.findByName("ROLE_CUSTOMER"));
return roles;
}

}
Loading
Loading